buffer.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. func (b *Buffer) AppendString(s string) *Buffer {
  45. b.Value = append(b.Value, s...)
  46. return b
  47. }
  48. // Prepend prepends bytes in front of the buffer. Caller must ensure total bytes prepended is
  49. // no more than 16 bytes.
  50. func (b *Buffer) Prepend(data []byte) *Buffer {
  51. b.SliceBack(len(data))
  52. copy(b.Value, data)
  53. return b
  54. }
  55. // Bytes returns the content bytes of this Buffer.
  56. func (b *Buffer) Bytes() []byte {
  57. return b.Value
  58. }
  59. // Slice cuts the buffer at the given position.
  60. func (b *Buffer) Slice(from, to int) *Buffer {
  61. b.Value = b.Value[from:to]
  62. return b
  63. }
  64. // SliceFrom cuts the buffer at the given position.
  65. func (b *Buffer) SliceFrom(from int) *Buffer {
  66. b.Value = b.Value[from:]
  67. return b
  68. }
  69. // SliceBack extends the Buffer to its front by offset bytes.
  70. // Caller must ensure cumulated offset is no more than 16.
  71. func (b *Buffer) SliceBack(offset int) *Buffer {
  72. newoffset := b.offset - offset
  73. if newoffset < 0 {
  74. newoffset = 0
  75. }
  76. b.Value = b.head[newoffset : b.offset+len(b.Value)]
  77. b.offset = newoffset
  78. return b
  79. }
  80. // Len returns the length of the buffer content.
  81. func (b *Buffer) Len() int {
  82. if b == nil {
  83. return 0
  84. }
  85. return len(b.Value)
  86. }
  87. func (b *Buffer) IsEmpty() bool {
  88. return b.Len() == 0
  89. }
  90. // IsFull returns true if the buffer has no more room to grow.
  91. func (b *Buffer) IsFull() bool {
  92. return len(b.Value) == cap(b.Value)
  93. }
  94. // Write implements Write method in io.Writer.
  95. func (b *Buffer) Write(data []byte) (int, error) {
  96. b.Append(data)
  97. return len(data), nil
  98. }
  99. // Read implements io.Reader.Read().
  100. func (b *Buffer) Read(data []byte) (int, error) {
  101. if b.Len() == 0 {
  102. return 0, io.EOF
  103. }
  104. nBytes := copy(data, b.Value)
  105. if nBytes == b.Len() {
  106. b.Value = b.Value[:0]
  107. } else {
  108. b.Value = b.Value[nBytes:]
  109. }
  110. return nBytes, nil
  111. }
  112. func (b *Buffer) FillFrom(reader io.Reader) (int, error) {
  113. begin := b.Len()
  114. b.Value = b.Value[:cap(b.Value)]
  115. nBytes, err := reader.Read(b.Value[begin:])
  116. b.Value = b.Value[:begin+nBytes]
  117. return nBytes, err
  118. }
  119. func (b *Buffer) String() string {
  120. return string(b.Value)
  121. }
  122. // NewSmallBuffer creates a Buffer with 1K bytes of arbitrary content.
  123. func NewSmallBuffer() *Buffer {
  124. return smallPool.Allocate()
  125. }
  126. // NewBuffer creates a Buffer with 8K bytes of arbitrary content.
  127. func NewBuffer() *Buffer {
  128. return mediumPool.Allocate()
  129. }
  130. // NewLargeBuffer creates a Buffer with 64K bytes of arbitrary content.
  131. func NewLargeBuffer() *Buffer {
  132. return largePool.Allocate()
  133. }