buffer.go 4.5 KB

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