buffer.go 4.6 KB

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