buffer.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package alloc
  2. import (
  3. "io"
  4. )
  5. const (
  6. defaultOffset = 16
  7. )
  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. head []byte
  13. pool *BufferPool
  14. Value []byte
  15. offset int
  16. }
  17. // Release recycles the buffer into an internal buffer pool.
  18. func (b *Buffer) Release() {
  19. if b == nil {
  20. return
  21. }
  22. b.pool.Free(b)
  23. b.head = nil
  24. b.Value = nil
  25. b.pool = nil
  26. }
  27. // Clear clears the content of the buffer, results an empty buffer with
  28. // Len() = 0.
  29. func (b *Buffer) Clear() *Buffer {
  30. b.offset = defaultOffset
  31. b.Value = b.head[b.offset:b.offset]
  32. return b
  33. }
  34. // AppendBytes appends one or more bytes to the end of the buffer.
  35. func (b *Buffer) AppendBytes(bytes ...byte) *Buffer {
  36. b.Value = append(b.Value, bytes...)
  37. return b
  38. }
  39. // Append appends a byte array to the end of the buffer.
  40. func (b *Buffer) Append(data []byte) *Buffer {
  41. b.Value = append(b.Value, data...)
  42. return b
  43. }
  44. // Prepend prepends bytes in front of the buffer. Caller must ensure total bytes prepended is
  45. // no more than 16 bytes.
  46. func (b *Buffer) Prepend(data []byte) *Buffer {
  47. b.SliceBack(len(data))
  48. copy(b.Value, data)
  49. return b
  50. }
  51. // Bytes returns the content bytes of this Buffer.
  52. func (b *Buffer) Bytes() []byte {
  53. return b.Value
  54. }
  55. // Slice cuts the buffer at the given position.
  56. func (b *Buffer) Slice(from, to int) *Buffer {
  57. b.Value = b.Value[from:to]
  58. return b
  59. }
  60. // SliceFrom cuts the buffer at the given position.
  61. func (b *Buffer) SliceFrom(from int) *Buffer {
  62. b.Value = b.Value[from:]
  63. return b
  64. }
  65. // SliceBack extends the Buffer to its front by offset bytes.
  66. // Caller must ensure cumulated offset is no more than 16.
  67. func (b *Buffer) SliceBack(offset int) *Buffer {
  68. newoffset := b.offset - offset
  69. if newoffset < 0 {
  70. newoffset = 0
  71. }
  72. b.Value = b.head[newoffset : b.offset+len(b.Value)]
  73. b.offset = newoffset
  74. return b
  75. }
  76. // Len returns the length of the buffer content.
  77. func (b *Buffer) Len() int {
  78. if b == nil {
  79. return 0
  80. }
  81. return len(b.Value)
  82. }
  83. func (b *Buffer) IsEmpty() bool {
  84. return b.Len() == 0
  85. }
  86. // IsFull returns true if the buffer has no more room to grow.
  87. func (b *Buffer) IsFull() bool {
  88. return len(b.Value) == cap(b.Value)
  89. }
  90. // Write implements Write method in io.Writer.
  91. func (b *Buffer) Write(data []byte) (int, error) {
  92. b.Append(data)
  93. return len(data), nil
  94. }
  95. // Read implements io.Reader.Read().
  96. func (b *Buffer) Read(data []byte) (int, error) {
  97. if b.Len() == 0 {
  98. return 0, io.EOF
  99. }
  100. nBytes := copy(data, b.Value)
  101. if nBytes == b.Len() {
  102. b.Value = b.Value[:0]
  103. } else {
  104. b.Value = b.Value[nBytes:]
  105. }
  106. return nBytes, nil
  107. }
  108. func (b *Buffer) FillFrom(reader io.Reader) (int, error) {
  109. begin := b.Len()
  110. b.Value = b.Value[:cap(b.Value)]
  111. nBytes, err := reader.Read(b.Value[begin:])
  112. b.Value = b.Value[:begin+nBytes]
  113. return nBytes, err
  114. }
  115. // NewSmallBuffer creates a Buffer with 1K bytes of arbitrary content.
  116. func NewSmallBuffer() *Buffer {
  117. return smallPool.Allocate()
  118. }
  119. // NewBuffer creates a Buffer with 8K bytes of arbitrary content.
  120. func NewBuffer() *Buffer {
  121. return mediumPool.Allocate()
  122. }
  123. // NewLargeBuffer creates a Buffer with 64K bytes of arbitrary content.
  124. func NewLargeBuffer() *Buffer {
  125. return largePool.Allocate()
  126. }