buffer.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Package alloc provides a light-weight memory allocation mechanism.
  2. package alloc
  3. import (
  4. "io"
  5. )
  6. const (
  7. defaultOffset = 16
  8. )
  9. // BytesWriter is a writer that writes contents into the given buffer.
  10. type BytesWriter func([]byte) int
  11. // Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles
  12. // the buffer into an internal buffer pool, in order to recreate a buffer more
  13. // quickly.
  14. type Buffer struct {
  15. head []byte
  16. pool Pool
  17. start int
  18. end int
  19. }
  20. // CreateBuffer creates a new Buffer object based on given container and parent pool.
  21. func CreateBuffer(container []byte, parent Pool) *Buffer {
  22. b := new(Buffer)
  23. b.head = container
  24. b.pool = parent
  25. b.start = defaultOffset
  26. b.end = defaultOffset
  27. return b
  28. }
  29. // Release recycles the buffer into an internal buffer pool.
  30. func (b *Buffer) Release() {
  31. if b == nil || b.head == nil {
  32. return
  33. }
  34. if b.pool != nil {
  35. b.pool.Free(b)
  36. }
  37. b.head = nil
  38. b.pool = nil
  39. }
  40. // Clear clears the content of the buffer, results an empty buffer with
  41. // Len() = 0.
  42. func (b *Buffer) Clear() {
  43. b.start = defaultOffset
  44. b.end = defaultOffset
  45. }
  46. // Reset resets this Buffer into its original state.
  47. func (b *Buffer) Reset() {
  48. b.start = defaultOffset
  49. b.end = len(b.head)
  50. }
  51. // AppendBytes appends one or more bytes to the end of the buffer.
  52. func (b *Buffer) AppendBytes(bytes ...byte) {
  53. b.Append(bytes)
  54. }
  55. // Append appends a byte array to the end of the buffer.
  56. func (b *Buffer) Append(data []byte) {
  57. nBytes := copy(b.head[b.end:], data)
  58. b.end += nBytes
  59. }
  60. // AppendFunc appends the content of a BytesWriter to the buffer.
  61. func (b *Buffer) AppendFunc(writer BytesWriter) {
  62. nBytes := writer(b.head[b.end:])
  63. b.end += nBytes
  64. }
  65. // Prepend prepends bytes in front of the buffer. Caller must ensure total bytes prepended is
  66. // no more than 16 bytes.
  67. func (b *Buffer) Prepend(data []byte) {
  68. b.SliceBack(len(data))
  69. copy(b.head[b.start:], data)
  70. }
  71. // PrependBytes prepends all data in front of the buffer.
  72. func (b *Buffer) PrependBytes(data ...byte) {
  73. b.Prepend(data)
  74. }
  75. func (b *Buffer) PrependFunc(offset int, writer BytesWriter) {
  76. b.SliceBack(offset)
  77. writer(b.head[b.start:])
  78. }
  79. // Byte returns the bytes at index.
  80. func (b *Buffer) Byte(index int) byte {
  81. return b.head[b.start+index]
  82. }
  83. // SetByte sets the byte value at index.
  84. func (b *Buffer) SetByte(index int, value byte) {
  85. b.head[b.start+index] = value
  86. }
  87. // Bytes returns the content bytes of this Buffer.
  88. func (b *Buffer) Bytes() []byte {
  89. return b.head[b.start:b.end]
  90. }
  91. // BytesRange returns a slice of this buffer with given from and to bounary.
  92. func (b *Buffer) BytesRange(from, to int) []byte {
  93. if from < 0 {
  94. from += b.Len()
  95. }
  96. if to < 0 {
  97. to += b.Len()
  98. }
  99. return b.head[b.start+from : b.start+to]
  100. }
  101. func (b *Buffer) BytesFrom(from int) []byte {
  102. if from < 0 {
  103. from += b.Len()
  104. }
  105. return b.head[b.start+from : b.end]
  106. }
  107. func (b *Buffer) BytesTo(to int) []byte {
  108. if to < 0 {
  109. to += b.Len()
  110. }
  111. return b.head[b.start : b.start+to]
  112. }
  113. // Slice cuts the buffer at the given position.
  114. func (b *Buffer) Slice(from, to int) {
  115. if from < 0 {
  116. from += b.Len()
  117. }
  118. if to < 0 {
  119. to += b.Len()
  120. }
  121. if to < from {
  122. panic("Invalid slice")
  123. }
  124. b.end = b.start + to
  125. b.start += from
  126. }
  127. // SliceFrom cuts the buffer at the given position.
  128. func (b *Buffer) SliceFrom(from int) {
  129. if from < 0 {
  130. from += b.Len()
  131. }
  132. b.start += from
  133. }
  134. // SliceBack extends the Buffer to its front by offset bytes.
  135. // Caller must ensure cumulated offset is no more than 16.
  136. func (b *Buffer) SliceBack(offset int) {
  137. b.start -= offset
  138. if b.start < 0 {
  139. panic("Negative buffer offset.")
  140. }
  141. }
  142. // Len returns the length of the buffer content.
  143. func (b *Buffer) Len() int {
  144. if b == nil {
  145. return 0
  146. }
  147. return b.end - b.start
  148. }
  149. // IsEmpty returns true if the buffer is empty.
  150. func (b *Buffer) IsEmpty() bool {
  151. return b.Len() == 0
  152. }
  153. // IsFull returns true if the buffer has no more room to grow.
  154. func (b *Buffer) IsFull() bool {
  155. return b.end == len(b.head)
  156. }
  157. // Write implements Write method in io.Writer.
  158. func (b *Buffer) Write(data []byte) (int, error) {
  159. nBytes := copy(b.head[b.end:], data)
  160. b.end += nBytes
  161. return nBytes, nil
  162. }
  163. // Read implements io.Reader.Read().
  164. func (b *Buffer) Read(data []byte) (int, error) {
  165. if b.Len() == 0 {
  166. return 0, io.EOF
  167. }
  168. nBytes := copy(data, b.head[b.start:b.end])
  169. if nBytes == b.Len() {
  170. b.Clear()
  171. } else {
  172. b.start += nBytes
  173. }
  174. return nBytes, nil
  175. }
  176. func (b *Buffer) FillFrom(reader io.Reader) (int, error) {
  177. nBytes, err := reader.Read(b.head[b.end:])
  178. b.end += nBytes
  179. return nBytes, err
  180. }
  181. func (b *Buffer) FillFullFrom(reader io.Reader, amount int) (int, error) {
  182. nBytes, err := io.ReadFull(reader, b.head[b.end:b.end+amount])
  183. b.end += nBytes
  184. return nBytes, err
  185. }
  186. func (b *Buffer) String() string {
  187. return string(b.head[b.start:b.end])
  188. }
  189. // NewBuffer creates a Buffer with 8K bytes of arbitrary content.
  190. func NewBuffer() *Buffer {
  191. return mediumPool.Allocate()
  192. }
  193. // NewSmallBuffer returns a buffer with 2K bytes capacity.
  194. func NewSmallBuffer() *Buffer {
  195. return smallPool.Allocate()
  196. }
  197. // NewLocalBuffer creates and returns a buffer on current thread.
  198. func NewLocalBuffer(size int) *Buffer {
  199. return CreateBuffer(make([]byte, size), nil)
  200. }