buffer.go 3.8 KB

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