buffer.go 4.8 KB

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