buffer.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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
  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(v uint16) *Buffer {
  69. b.Value = serial.Uint16ToBytes(v, b.Value)
  70. return b
  71. }
  72. func (b *Buffer) AppendUint32(v uint32) *Buffer {
  73. b.Value = serial.Uint32ToBytes(v, 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(v uint16) *Buffer {
  87. b.SliceBack(2)
  88. serial.Uint16ToBytes(v, b.Value[:0])
  89. return b
  90. }
  91. func (b *Buffer) PrependUint32(v uint32) *Buffer {
  92. b.SliceBack(4)
  93. serial.Uint32ToBytes(v, 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. // Bytes returns the content bytes of this Buffer.
  102. func (b *Buffer) Bytes() []byte {
  103. return b.Value
  104. }
  105. // Slice cuts the buffer at the given position.
  106. func (b *Buffer) Slice(from, to int) *Buffer {
  107. b.offset += from
  108. b.Value = b.Value[from:to]
  109. return b
  110. }
  111. // SliceFrom cuts the buffer at the given position.
  112. func (b *Buffer) SliceFrom(from int) *Buffer {
  113. b.offset += from
  114. b.Value = b.Value[from:]
  115. return b
  116. }
  117. // SliceBack extends the Buffer to its front by offset bytes.
  118. // Caller must ensure cumulated offset is no more than 16.
  119. func (b *Buffer) SliceBack(offset int) *Buffer {
  120. newoffset := b.offset - offset
  121. if newoffset < 0 {
  122. panic("Negative buffer offset.")
  123. }
  124. b.Value = b.head[newoffset : b.offset+len(b.Value)]
  125. b.offset = newoffset
  126. return b
  127. }
  128. // Len returns the length of the buffer content.
  129. func (b *Buffer) Len() int {
  130. if b == nil {
  131. return 0
  132. }
  133. return len(b.Value)
  134. }
  135. func (b *Buffer) IsEmpty() bool {
  136. return b.Len() == 0
  137. }
  138. // IsFull returns true if the buffer has no more room to grow.
  139. func (b *Buffer) IsFull() bool {
  140. return len(b.Value) == cap(b.Value)
  141. }
  142. // Write implements Write method in io.Writer.
  143. func (b *Buffer) Write(data []byte) (int, error) {
  144. b.Append(data)
  145. return len(data), nil
  146. }
  147. // Read implements io.Reader.Read().
  148. func (b *Buffer) Read(data []byte) (int, error) {
  149. if b.Len() == 0 {
  150. return 0, io.EOF
  151. }
  152. nBytes := copy(data, b.Value)
  153. if nBytes == b.Len() {
  154. b.Clear()
  155. } else {
  156. b.Value = b.Value[nBytes:]
  157. b.offset += nBytes
  158. }
  159. return nBytes, nil
  160. }
  161. func (b *Buffer) FillFrom(reader io.Reader) (int, error) {
  162. begin := b.Len()
  163. b.Value = b.Value[:cap(b.Value)]
  164. nBytes, err := reader.Read(b.Value[begin:])
  165. if err == nil {
  166. b.Value = b.Value[:begin+nBytes]
  167. }
  168. return nBytes, err
  169. }
  170. func (b *Buffer) String() string {
  171. return string(b.Value)
  172. }
  173. // NewBuffer creates a Buffer with 8K bytes of arbitrary content.
  174. func NewBuffer() *Buffer {
  175. return mediumPool.Allocate()
  176. }
  177. // NewLargeBuffer creates a Buffer with 64K bytes of arbitrary content.
  178. //func NewLargeBuffer() *Buffer {
  179. // return largePool.Allocate()
  180. //}
  181. //func NewBufferWithSize(size int) *Buffer {
  182. // if size <= BufferSize {
  183. // return NewBuffer()
  184. // }
  185. //
  186. // return NewLargeBuffer()
  187. //}
  188. func NewLocalBuffer(size int) *Buffer {
  189. return CreateBuffer(make([]byte, size), nil)
  190. }