buffer.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.offset += from
  62. b.Value = b.Value[from:to]
  63. return b
  64. }
  65. // SliceFrom cuts the buffer at the given position.
  66. func (b *Buffer) SliceFrom(from int) *Buffer {
  67. b.offset += from
  68. b.Value = b.Value[from:]
  69. return b
  70. }
  71. // SliceBack extends the Buffer to its front by offset bytes.
  72. // Caller must ensure cumulated offset is no more than 16.
  73. func (b *Buffer) SliceBack(offset int) *Buffer {
  74. newoffset := b.offset - offset
  75. if newoffset < 0 {
  76. newoffset = 0
  77. }
  78. b.Value = b.head[newoffset : b.offset+len(b.Value)]
  79. b.offset = newoffset
  80. return b
  81. }
  82. // Len returns the length of the buffer content.
  83. func (b *Buffer) Len() int {
  84. if b == nil {
  85. return 0
  86. }
  87. return len(b.Value)
  88. }
  89. func (b *Buffer) IsEmpty() bool {
  90. return b.Len() == 0
  91. }
  92. // IsFull returns true if the buffer has no more room to grow.
  93. func (b *Buffer) IsFull() bool {
  94. return len(b.Value) == cap(b.Value)
  95. }
  96. // Write implements Write method in io.Writer.
  97. func (b *Buffer) Write(data []byte) (int, error) {
  98. b.Append(data)
  99. return len(data), nil
  100. }
  101. // Read implements io.Reader.Read().
  102. func (b *Buffer) Read(data []byte) (int, error) {
  103. if b.Len() == 0 {
  104. return 0, io.EOF
  105. }
  106. nBytes := copy(data, b.Value)
  107. if nBytes == b.Len() {
  108. b.Clear()
  109. } else {
  110. b.Value = b.Value[nBytes:]
  111. b.offset += nBytes
  112. }
  113. return nBytes, nil
  114. }
  115. func (b *Buffer) FillFrom(reader io.Reader) (int, error) {
  116. begin := b.Len()
  117. b.Value = b.Value[:cap(b.Value)]
  118. nBytes, err := reader.Read(b.Value[begin:])
  119. if err == nil {
  120. b.Value = b.Value[:begin+nBytes]
  121. }
  122. return nBytes, err
  123. }
  124. func (b *Buffer) String() string {
  125. return string(b.Value)
  126. }
  127. // NewSmallBuffer creates a Buffer with 1K bytes of arbitrary content.
  128. func NewSmallBuffer() *Buffer {
  129. return smallPool.Allocate()
  130. }
  131. // NewBuffer creates a Buffer with 8K bytes of arbitrary content.
  132. func NewBuffer() *Buffer {
  133. return mediumPool.Allocate()
  134. }
  135. // NewLargeBuffer creates a Buffer with 64K bytes of arbitrary content.
  136. func NewLargeBuffer() *Buffer {
  137. return largePool.Allocate()
  138. }
  139. func NewBufferWithSize(size int) *Buffer {
  140. if size <= SmallBufferSize {
  141. return NewSmallBuffer()
  142. }
  143. if size <= BufferSize {
  144. return NewBuffer()
  145. }
  146. return NewLargeBuffer()
  147. }