buffer.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. start int32
  14. end int32
  15. }
  16. // Release recycles the buffer into an internal buffer pool.
  17. func (b *Buffer) Release() {
  18. if b == nil || b.v == nil {
  19. return
  20. }
  21. freeBytes(b.v)
  22. b.v = nil
  23. b.start = 0
  24. b.end = 0
  25. }
  26. // Clear clears the content of the buffer, results an empty buffer with
  27. // Len() = 0.
  28. func (b *Buffer) Clear() {
  29. b.start = 0
  30. b.end = 0
  31. }
  32. // AppendBytes appends one or more bytes to the end of the buffer.
  33. func (b *Buffer) AppendBytes(bytes ...byte) int {
  34. return b.Append(bytes)
  35. }
  36. // Append appends a byte array to the end of the buffer.
  37. func (b *Buffer) Append(data []byte) int {
  38. nBytes := copy(b.v[b.end:], data)
  39. b.end += int32(nBytes)
  40. return nBytes
  41. }
  42. // AppendSupplier appends the content of a BytesWriter to the buffer.
  43. func (b *Buffer) AppendSupplier(writer Supplier) error {
  44. nBytes, err := writer(b.v[b.end:])
  45. b.end += int32(nBytes)
  46. return err
  47. }
  48. // Byte returns the bytes at index.
  49. func (b *Buffer) Byte(index int) byte {
  50. return b.v[b.start+int32(index)]
  51. }
  52. // SetByte sets the byte value at index.
  53. func (b *Buffer) SetByte(index int, value byte) {
  54. b.v[b.start+int32(index)] = value
  55. }
  56. // Bytes returns the content bytes of this Buffer.
  57. func (b *Buffer) Bytes() []byte {
  58. return b.v[b.start:b.end]
  59. }
  60. // Reset resets the content of the Buffer with a supplier.
  61. func (b *Buffer) Reset(writer Supplier) error {
  62. nBytes, err := writer(b.v)
  63. b.start = 0
  64. b.end = int32(nBytes)
  65. return err
  66. }
  67. // BytesRange returns a slice of this buffer with given from and to boundary.
  68. func (b *Buffer) BytesRange(from, to int) []byte {
  69. if from < 0 {
  70. from += b.Len()
  71. }
  72. if to < 0 {
  73. to += b.Len()
  74. }
  75. return b.v[b.start+int32(from) : b.start+int32(to)]
  76. }
  77. // BytesFrom returns a slice of this Buffer starting from the given position.
  78. func (b *Buffer) BytesFrom(from int) []byte {
  79. if from < 0 {
  80. from += b.Len()
  81. }
  82. return b.v[b.start+int32(from) : b.end]
  83. }
  84. // BytesTo returns a slice of this Buffer from start to the given position.
  85. func (b *Buffer) BytesTo(to int) []byte {
  86. if to < 0 {
  87. to += b.Len()
  88. }
  89. return b.v[b.start : b.start+int32(to)]
  90. }
  91. // Slice cuts the buffer at the given position.
  92. func (b *Buffer) Slice(from, to int) {
  93. if from < 0 {
  94. from += b.Len()
  95. }
  96. if to < 0 {
  97. to += b.Len()
  98. }
  99. if to < from {
  100. panic("Invalid slice")
  101. }
  102. b.end = b.start + int32(to)
  103. b.start += int32(from)
  104. }
  105. // SliceFrom cuts the buffer at the given position.
  106. func (b *Buffer) SliceFrom(from int) {
  107. if from < 0 {
  108. from += b.Len()
  109. }
  110. b.start += int32(from)
  111. }
  112. // Len returns the length of the buffer content.
  113. func (b *Buffer) Len() int {
  114. if b == nil {
  115. return 0
  116. }
  117. return int(b.end - b.start)
  118. }
  119. // IsEmpty returns true if the buffer is empty.
  120. func (b *Buffer) IsEmpty() bool {
  121. return b.Len() == 0
  122. }
  123. // IsFull returns true if the buffer has no more room to grow.
  124. func (b *Buffer) IsFull() bool {
  125. return b.end == int32(len(b.v))
  126. }
  127. // Write implements Write method in io.Writer.
  128. func (b *Buffer) Write(data []byte) (int, error) {
  129. nBytes := copy(b.v[b.end:], data)
  130. b.end += int32(nBytes)
  131. return nBytes, nil
  132. }
  133. // Read implements io.Reader.Read().
  134. func (b *Buffer) Read(data []byte) (int, error) {
  135. if b.Len() == 0 {
  136. return 0, io.EOF
  137. }
  138. nBytes := copy(data, b.v[b.start:b.end])
  139. if nBytes == b.Len() {
  140. b.Clear()
  141. } else {
  142. b.start += int32(nBytes)
  143. }
  144. return nBytes, nil
  145. }
  146. // String returns the string form of this Buffer.
  147. func (b *Buffer) String() string {
  148. return string(b.Bytes())
  149. }
  150. // New creates a Buffer with 0 length and 2K capacity.
  151. func New() *Buffer {
  152. return &Buffer{
  153. v: pool[0].Get().([]byte),
  154. }
  155. }
  156. // NewSize creates and returns a buffer with 0 length and at least the given capacity.
  157. func NewSize(size uint32) *Buffer {
  158. return &Buffer{
  159. v: newBytes(size),
  160. }
  161. }