buffer.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Package buf provides a light-weight memory allocation mechanism.
  2. package buf
  3. import (
  4. "io"
  5. )
  6. // Supplier is a writer that writes contents into the given buffer.
  7. type Supplier func([]byte) (int, error)
  8. // Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles
  9. // the buffer into an internal buffer pool, in order to recreate a buffer more
  10. // quickly.
  11. type Buffer struct {
  12. v []byte
  13. pool Pool
  14. start int
  15. end int
  16. }
  17. // Release recycles the buffer into an internal buffer pool.
  18. func (b *Buffer) Release() {
  19. if b == nil || b.v == nil {
  20. return
  21. }
  22. if b.pool != nil {
  23. b.pool.Free(b)
  24. }
  25. b.v = nil
  26. b.pool = nil
  27. }
  28. // Clear clears the content of the buffer, results an empty buffer with
  29. // Len() = 0.
  30. func (b *Buffer) Clear() {
  31. b.start = 0
  32. b.end = 0
  33. }
  34. // AppendBytes appends one or more bytes to the end of the buffer.
  35. func (b *Buffer) AppendBytes(bytes ...byte) {
  36. b.Append(bytes)
  37. }
  38. // Append appends a byte array to the end of the buffer.
  39. func (b *Buffer) Append(data []byte) {
  40. nBytes := copy(b.v[b.end:], data)
  41. b.end += nBytes
  42. }
  43. // AppendSupplier appends the content of a BytesWriter to the buffer.
  44. func (b *Buffer) AppendSupplier(writer Supplier) error {
  45. nBytes, err := writer(b.v[b.end:])
  46. b.end += nBytes
  47. return err
  48. }
  49. // Byte returns the bytes at index.
  50. func (b *Buffer) Byte(index int) byte {
  51. return b.v[b.start+index]
  52. }
  53. // SetByte sets the byte value at index.
  54. func (b *Buffer) SetByte(index int, value byte) {
  55. b.v[b.start+index] = value
  56. }
  57. // Bytes returns the content bytes of this Buffer.
  58. func (b *Buffer) Bytes() []byte {
  59. return b.v[b.start:b.end]
  60. }
  61. // Reset resets the content of the Buffer with a supplier.
  62. func (b *Buffer) Reset(writer Supplier) error {
  63. b.start = 0
  64. nBytes, err := writer(b.v[b.start:])
  65. b.end = b.start + nBytes
  66. return err
  67. }
  68. // BytesRange returns a slice of this buffer with given from and to bounary.
  69. func (b *Buffer) BytesRange(from, to int) []byte {
  70. if from < 0 {
  71. from += b.Len()
  72. }
  73. if to < 0 {
  74. to += b.Len()
  75. }
  76. return b.v[b.start+from : b.start+to]
  77. }
  78. // BytesFrom returns a slice of this Buffer starting from the given position.
  79. func (b *Buffer) BytesFrom(from int) []byte {
  80. if from < 0 {
  81. from += b.Len()
  82. }
  83. return b.v[b.start+from : b.end]
  84. }
  85. // BytesTo returns a slice of this Buffer from start to the given position.
  86. func (b *Buffer) BytesTo(to int) []byte {
  87. if to < 0 {
  88. to += b.Len()
  89. }
  90. return b.v[b.start : b.start+to]
  91. }
  92. // Slice cuts the buffer at the given position.
  93. func (b *Buffer) Slice(from, to int) {
  94. if from < 0 {
  95. from += b.Len()
  96. }
  97. if to < 0 {
  98. to += b.Len()
  99. }
  100. if to < from {
  101. panic("Invalid slice")
  102. }
  103. b.end = b.start + to
  104. b.start += from
  105. }
  106. // SliceFrom cuts the buffer at the given position.
  107. func (b *Buffer) SliceFrom(from int) {
  108. if from < 0 {
  109. from += b.Len()
  110. }
  111. b.start += from
  112. }
  113. // Len returns the length of the buffer content.
  114. func (b *Buffer) Len() int {
  115. if b == nil {
  116. return 0
  117. }
  118. return b.end - b.start
  119. }
  120. // IsEmpty returns true if the buffer is empty.
  121. func (b *Buffer) IsEmpty() bool {
  122. return b.Len() == 0
  123. }
  124. // IsFull returns true if the buffer has no more room to grow.
  125. func (b *Buffer) IsFull() bool {
  126. return b.end == len(b.v)
  127. }
  128. // Write implements Write method in io.Writer.
  129. func (b *Buffer) Write(data []byte) (int, error) {
  130. nBytes := copy(b.v[b.end:], data)
  131. b.end += nBytes
  132. return nBytes, nil
  133. }
  134. // Read implements io.Reader.Read().
  135. func (b *Buffer) Read(data []byte) (int, error) {
  136. if b.Len() == 0 {
  137. return 0, io.EOF
  138. }
  139. nBytes := copy(data, b.v[b.start:b.end])
  140. if nBytes == b.Len() {
  141. b.Clear()
  142. } else {
  143. b.start += nBytes
  144. }
  145. return nBytes, nil
  146. }
  147. // String returns the string form of this Buffer.
  148. func (b *Buffer) String() string {
  149. return string(b.Bytes())
  150. }
  151. // New creates a Buffer with 8K bytes of arbitrary content.
  152. func New() *Buffer {
  153. return mediumPool.Allocate()
  154. }
  155. // NewSmall returns a buffer with 2K bytes capacity.
  156. func NewSmall() *Buffer {
  157. return smallPool.Allocate()
  158. }
  159. // NewLocal creates and returns a buffer on current thread.
  160. func NewLocal(size int) *Buffer {
  161. return &Buffer{
  162. v: make([]byte, size),
  163. pool: nil,
  164. }
  165. }