buffer.go 4.6 KB

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