buffer.go 4.3 KB

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