buffer.go 5.0 KB

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