buffer.go 4.0 KB

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