buffer.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Package buf provides a light-weight memory allocation mechanism.
  2. package buf
  3. import (
  4. "io"
  5. )
  6. // Supplier is a writer that writes contents into the given buffer.
  7. type Supplier func([]byte) (int, error)
  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. // Release recycles the buffer into an internal buffer pool.
  18. func (b *Buffer) Release() {
  19. if b == nil || b.v == nil {
  20. return
  21. }
  22. if b.pool != nil {
  23. b.pool.Free(b)
  24. }
  25. b.v = nil
  26. b.pool = nil
  27. b.start = 0
  28. b.end = 0
  29. }
  30. // Clear clears the content of the buffer, results an empty buffer with
  31. // Len() = 0.
  32. func (b *Buffer) Clear() {
  33. b.start = 0
  34. b.end = 0
  35. }
  36. // AppendBytes appends one or more bytes to the end of the buffer.
  37. func (b *Buffer) AppendBytes(bytes ...byte) int {
  38. return b.Append(bytes)
  39. }
  40. // Append appends a byte array to the end of the buffer.
  41. func (b *Buffer) Append(data []byte) int {
  42. nBytes := copy(b.v[b.end:], data)
  43. b.end += nBytes
  44. return nBytes
  45. }
  46. // AppendSupplier appends the content of a BytesWriter to the buffer.
  47. func (b *Buffer) AppendSupplier(writer Supplier) error {
  48. nBytes, err := writer(b.v[b.end:])
  49. b.end += nBytes
  50. return err
  51. }
  52. // Byte returns the bytes at index.
  53. func (b *Buffer) Byte(index int) byte {
  54. return b.v[b.start+index]
  55. }
  56. // SetByte sets the byte value at index.
  57. func (b *Buffer) SetByte(index int, value byte) {
  58. b.v[b.start+index] = value
  59. }
  60. // Bytes returns the content bytes of this Buffer.
  61. func (b *Buffer) Bytes() []byte {
  62. return b.v[b.start:b.end]
  63. }
  64. // Reset resets the content of the Buffer with a supplier.
  65. func (b *Buffer) Reset(writer Supplier) error {
  66. b.start = 0
  67. nBytes, err := writer(b.v)
  68. b.end = nBytes
  69. return err
  70. }
  71. // BytesRange returns a slice of this buffer with given from and to bounary.
  72. func (b *Buffer) BytesRange(from, to int) []byte {
  73. if from < 0 {
  74. from += b.Len()
  75. }
  76. if to < 0 {
  77. to += b.Len()
  78. }
  79. return b.v[b.start+from : b.start+to]
  80. }
  81. // BytesFrom returns a slice of this Buffer starting from the given position.
  82. func (b *Buffer) BytesFrom(from int) []byte {
  83. if from < 0 {
  84. from += b.Len()
  85. }
  86. return b.v[b.start+from : b.end]
  87. }
  88. // BytesTo returns a slice of this Buffer from start to the given position.
  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. // String returns the string form of this Buffer.
  151. func (b *Buffer) String() string {
  152. return string(b.Bytes())
  153. }
  154. // New creates a Buffer with 0 length and 8K capacity.
  155. func New() *Buffer {
  156. return mediumPool.Allocate()
  157. }
  158. // NewLocal creates and returns a buffer with 0 length and given capacity on current thread.
  159. func NewLocal(size int) *Buffer {
  160. return &Buffer{
  161. v: make([]byte, size),
  162. pool: nil,
  163. }
  164. }