buffer.go 4.4 KB

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