buffer.go 3.8 KB

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