buffer.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. v []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.v = 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.v == nil {
  32. return
  33. }
  34. if b.pool != nil {
  35. b.pool.Free(b)
  36. }
  37. b.v = 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.v)
  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.v[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.v[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.v[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.v[b.start:])
  78. }
  79. // Byte returns the bytes at index.
  80. func (b *Buffer) Byte(index int) byte {
  81. return b.v[b.start+index]
  82. }
  83. // SetByte sets the byte value at index.
  84. func (b *Buffer) SetByte(index int, value byte) {
  85. b.v[b.start+index] = value
  86. }
  87. // Bytes returns the content bytes of this Buffer.
  88. func (b *Buffer) Bytes() []byte {
  89. return b.v[b.start:b.end]
  90. }
  91. func (b *Buffer) SetBytesFunc(writer BytesWriter) {
  92. b.start = defaultOffset
  93. b.end = b.start + writer(b.v[b.start:])
  94. }
  95. // BytesRange returns a slice of this buffer with given from and to bounary.
  96. func (b *Buffer) BytesRange(from, to int) []byte {
  97. if from < 0 {
  98. from += b.Len()
  99. }
  100. if to < 0 {
  101. to += b.Len()
  102. }
  103. return b.v[b.start+from : b.start+to]
  104. }
  105. func (b *Buffer) BytesFrom(from int) []byte {
  106. if from < 0 {
  107. from += b.Len()
  108. }
  109. return b.v[b.start+from : b.end]
  110. }
  111. func (b *Buffer) BytesTo(to int) []byte {
  112. if to < 0 {
  113. to += b.Len()
  114. }
  115. return b.v[b.start : b.start+to]
  116. }
  117. // Slice cuts the buffer at the given position.
  118. func (b *Buffer) Slice(from, to int) {
  119. if from < 0 {
  120. from += b.Len()
  121. }
  122. if to < 0 {
  123. to += b.Len()
  124. }
  125. if to < from {
  126. panic("Invalid slice")
  127. }
  128. b.end = b.start + to
  129. b.start += from
  130. }
  131. // SliceFrom cuts the buffer at the given position.
  132. func (b *Buffer) SliceFrom(from int) {
  133. if from < 0 {
  134. from += b.Len()
  135. }
  136. b.start += from
  137. }
  138. // SliceBack extends the Buffer to its front by offset bytes.
  139. // Caller must ensure cumulated offset is no more than 16.
  140. func (b *Buffer) SliceBack(offset int) {
  141. b.start -= offset
  142. if b.start < 0 {
  143. panic("Negative buffer offset.")
  144. }
  145. }
  146. // Len returns the length of the buffer content.
  147. func (b *Buffer) Len() int {
  148. if b == nil {
  149. return 0
  150. }
  151. return b.end - b.start
  152. }
  153. // IsEmpty returns true if the buffer is empty.
  154. func (b *Buffer) IsEmpty() bool {
  155. return b.Len() == 0
  156. }
  157. // IsFull returns true if the buffer has no more room to grow.
  158. func (b *Buffer) IsFull() bool {
  159. return b.end == len(b.v)
  160. }
  161. // Write implements Write method in io.Writer.
  162. func (b *Buffer) Write(data []byte) (int, error) {
  163. nBytes := copy(b.v[b.end:], data)
  164. b.end += nBytes
  165. return nBytes, nil
  166. }
  167. // Read implements io.Reader.Read().
  168. func (b *Buffer) Read(data []byte) (int, error) {
  169. if b.Len() == 0 {
  170. return 0, io.EOF
  171. }
  172. nBytes := copy(data, b.v[b.start:b.end])
  173. if nBytes == b.Len() {
  174. b.Clear()
  175. } else {
  176. b.start += nBytes
  177. }
  178. return nBytes, nil
  179. }
  180. func (b *Buffer) FillFrom(reader io.Reader) (int, error) {
  181. nBytes, err := reader.Read(b.v[b.end:])
  182. b.end += nBytes
  183. return nBytes, err
  184. }
  185. func (b *Buffer) FillFullFrom(reader io.Reader, amount int) (int, error) {
  186. nBytes, err := io.ReadFull(reader, b.v[b.end:b.end+amount])
  187. b.end += nBytes
  188. return nBytes, err
  189. }
  190. func (b *Buffer) String() string {
  191. return string(b.v[b.start:b.end])
  192. }
  193. // NewBuffer creates a Buffer with 8K bytes of arbitrary content.
  194. func NewBuffer() *Buffer {
  195. return mediumPool.Allocate()
  196. }
  197. // NewSmallBuffer returns a buffer with 2K bytes capacity.
  198. func NewSmallBuffer() *Buffer {
  199. return smallPool.Allocate()
  200. }
  201. // NewLocalBuffer creates and returns a buffer on current thread.
  202. func NewLocalBuffer(size int) *Buffer {
  203. return CreateBuffer(make([]byte, size), nil)
  204. }