buffer.go 4.5 KB

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