buffer.go 3.7 KB

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