buffer.go 4.4 KB

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