buffer.go 4.0 KB

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