buffer.go 5.1 KB

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