buffer.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Package alloc provides a light-weight memory allocation mechanism.
  2. package buf
  3. import (
  4. "io"
  5. )
  6. // BytesWriter is a writer that writes contents into the given buffer.
  7. type BytesWriter func([]byte) int
  8. // Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles
  9. // the buffer into an internal buffer pool, in order to recreate a buffer more
  10. // quickly.
  11. type Buffer struct {
  12. v []byte
  13. pool Pool
  14. start int
  15. end int
  16. }
  17. // CreateBuffer creates a new Buffer object based on given container and parent pool.
  18. func CreateBuffer(container []byte, parent Pool) *Buffer {
  19. b := new(Buffer)
  20. b.v = container
  21. b.pool = parent
  22. b.start = 0
  23. b.end = 0
  24. return b
  25. }
  26. // Release recycles the buffer into an internal buffer pool.
  27. func (b *Buffer) Release() {
  28. if b == nil || b.v == nil {
  29. return
  30. }
  31. if b.pool != nil {
  32. b.pool.Free(b)
  33. }
  34. b.v = nil
  35. b.pool = nil
  36. }
  37. // Clear clears the content of the buffer, results an empty buffer with
  38. // Len() = 0.
  39. func (b *Buffer) Clear() {
  40. b.start = 0
  41. b.end = 0
  42. }
  43. // AppendBytes appends one or more bytes to the end of the buffer.
  44. func (b *Buffer) AppendBytes(bytes ...byte) {
  45. b.Append(bytes)
  46. }
  47. // Append appends a byte array to the end of the buffer.
  48. func (b *Buffer) Append(data []byte) {
  49. nBytes := copy(b.v[b.end:], data)
  50. b.end += nBytes
  51. }
  52. // AppendFunc appends the content of a BytesWriter to the buffer.
  53. func (b *Buffer) AppendFunc(writer BytesWriter) {
  54. nBytes := writer(b.v[b.end:])
  55. b.end += nBytes
  56. }
  57. // Byte returns the bytes at index.
  58. func (b *Buffer) Byte(index int) byte {
  59. return b.v[b.start+index]
  60. }
  61. // SetByte sets the byte value at index.
  62. func (b *Buffer) SetByte(index int, value byte) {
  63. b.v[b.start+index] = value
  64. }
  65. // Bytes returns the content bytes of this Buffer.
  66. func (b *Buffer) Bytes() []byte {
  67. return b.v[b.start:b.end]
  68. }
  69. func (b *Buffer) SetBytesFunc(writer BytesWriter) {
  70. b.start = 0
  71. b.end = b.start + writer(b.v[b.start:])
  72. }
  73. // BytesRange returns a slice of this buffer with given from and to bounary.
  74. func (b *Buffer) BytesRange(from, to int) []byte {
  75. if from < 0 {
  76. from += b.Len()
  77. }
  78. if to < 0 {
  79. to += b.Len()
  80. }
  81. return b.v[b.start+from : b.start+to]
  82. }
  83. func (b *Buffer) BytesFrom(from int) []byte {
  84. if from < 0 {
  85. from += b.Len()
  86. }
  87. return b.v[b.start+from : b.end]
  88. }
  89. func (b *Buffer) BytesTo(to int) []byte {
  90. if to < 0 {
  91. to += b.Len()
  92. }
  93. return b.v[b.start : b.start+to]
  94. }
  95. // Slice cuts the buffer at the given position.
  96. func (b *Buffer) Slice(from, to int) {
  97. if from < 0 {
  98. from += b.Len()
  99. }
  100. if to < 0 {
  101. to += b.Len()
  102. }
  103. if to < from {
  104. panic("Invalid slice")
  105. }
  106. b.end = b.start + to
  107. b.start += from
  108. }
  109. // SliceFrom cuts the buffer at the given position.
  110. func (b *Buffer) SliceFrom(from int) {
  111. if from < 0 {
  112. from += b.Len()
  113. }
  114. b.start += from
  115. }
  116. // Len returns the length of the buffer content.
  117. func (b *Buffer) Len() int {
  118. if b == nil {
  119. return 0
  120. }
  121. return b.end - b.start
  122. }
  123. // IsEmpty returns true if the buffer is empty.
  124. func (b *Buffer) IsEmpty() bool {
  125. return b.Len() == 0
  126. }
  127. // IsFull returns true if the buffer has no more room to grow.
  128. func (b *Buffer) IsFull() bool {
  129. return b.end == len(b.v)
  130. }
  131. // Write implements Write method in io.Writer.
  132. func (b *Buffer) Write(data []byte) (int, error) {
  133. nBytes := copy(b.v[b.end:], data)
  134. b.end += nBytes
  135. return nBytes, nil
  136. }
  137. // Read implements io.Reader.Read().
  138. func (b *Buffer) Read(data []byte) (int, error) {
  139. if b.Len() == 0 {
  140. return 0, io.EOF
  141. }
  142. nBytes := copy(data, b.v[b.start:b.end])
  143. if nBytes == b.Len() {
  144. b.Clear()
  145. } else {
  146. b.start += nBytes
  147. }
  148. return nBytes, nil
  149. }
  150. func (b *Buffer) FillFrom(reader io.Reader) (int, error) {
  151. nBytes, err := reader.Read(b.v[b.end:])
  152. b.end += nBytes
  153. return nBytes, err
  154. }
  155. func (b *Buffer) FillFullFrom(reader io.Reader, amount int) (int, error) {
  156. nBytes, err := io.ReadFull(reader, b.v[b.end:b.end+amount])
  157. b.end += nBytes
  158. return nBytes, err
  159. }
  160. func (b *Buffer) String() string {
  161. return string(b.Bytes())
  162. }
  163. // NewBuffer creates a Buffer with 8K bytes of arbitrary content.
  164. func NewBuffer() *Buffer {
  165. return mediumPool.Allocate()
  166. }
  167. // NewSmallBuffer returns a buffer with 2K bytes capacity.
  168. func NewSmallBuffer() *Buffer {
  169. return smallPool.Allocate()
  170. }
  171. // NewLocalBuffer creates and returns a buffer on current thread.
  172. func NewLocalBuffer(size int) *Buffer {
  173. return CreateBuffer(make([]byte, size), nil)
  174. }