buffer.go 4.0 KB

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