buffer.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package buf
  2. import (
  3. "io"
  4. "github.com/v2fly/v2ray-core/v5/common/bytespool"
  5. )
  6. const (
  7. // Size of a regular buffer.
  8. Size = 2048
  9. )
  10. var pool = bytespool.GetPool(Size)
  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. v []byte
  16. start int32
  17. end int32
  18. unmanaged bool
  19. }
  20. // New creates a Buffer with 0 length and 2K capacity.
  21. func New() *Buffer {
  22. return &Buffer{
  23. v: pool.Get().([]byte),
  24. }
  25. }
  26. // FromBytes creates a Buffer with an existed bytearray
  27. func FromBytes(data []byte) *Buffer {
  28. return &Buffer{
  29. v: data,
  30. end: int32(len(data)),
  31. unmanaged: true,
  32. }
  33. }
  34. // StackNew creates a new Buffer object on stack.
  35. // This method is for buffers that is released in the same function.
  36. func StackNew() Buffer {
  37. return Buffer{
  38. v: pool.Get().([]byte),
  39. }
  40. }
  41. // Release recycles the buffer into an internal buffer pool.
  42. func (b *Buffer) Release() {
  43. if b == nil || b.v == nil || b.unmanaged {
  44. return
  45. }
  46. p := b.v
  47. b.v = nil
  48. b.Clear()
  49. pool.Put(p) // nolint: staticcheck
  50. }
  51. // Clear clears the content of the buffer, results an empty buffer with
  52. // Len() = 0.
  53. func (b *Buffer) Clear() {
  54. b.start = 0
  55. b.end = 0
  56. }
  57. // Byte returns the bytes at index.
  58. func (b *Buffer) Byte(index int32) byte {
  59. return b.v[b.start+index]
  60. }
  61. // SetByte sets the byte value at index.
  62. func (b *Buffer) SetByte(index int32, value byte) {
  63. b.v[b.start+index] = value
  64. }
  65. // Bytes returns the content bytes of this Buffer.
  66. func (b *Buffer) Bytes() []byte {
  67. return b.v[b.start:b.end]
  68. }
  69. // Extend increases the buffer size by n bytes, and returns the extended part.
  70. // It panics if result size is larger than buf.Size.
  71. func (b *Buffer) Extend(n int32) []byte {
  72. end := b.end + n
  73. if end > int32(len(b.v)) {
  74. panic("extending out of bound")
  75. }
  76. ext := b.v[b.end:end]
  77. b.end = end
  78. return ext
  79. }
  80. // BytesRange returns a slice of this buffer with given from and to boundary.
  81. func (b *Buffer) BytesRange(from, to int32) []byte {
  82. if from < 0 {
  83. from += b.Len()
  84. }
  85. if to < 0 {
  86. to += b.Len()
  87. }
  88. return b.v[b.start+from : b.start+to]
  89. }
  90. // BytesFrom returns a slice of this Buffer starting from the given position.
  91. func (b *Buffer) BytesFrom(from int32) []byte {
  92. if from < 0 {
  93. from += b.Len()
  94. }
  95. return b.v[b.start+from : b.end]
  96. }
  97. // BytesTo returns a slice of this Buffer from start to the given position.
  98. func (b *Buffer) BytesTo(to int32) []byte {
  99. if to < 0 {
  100. to += b.Len()
  101. }
  102. return b.v[b.start : b.start+to]
  103. }
  104. // Resize cuts the buffer at the given position.
  105. func (b *Buffer) Resize(from, to int32) {
  106. if from < 0 {
  107. from += b.Len()
  108. }
  109. if to < 0 {
  110. to += b.Len()
  111. }
  112. if to < from {
  113. panic("Invalid slice")
  114. }
  115. b.end = b.start + to
  116. b.start += from
  117. }
  118. // Advance cuts the buffer at the given position.
  119. func (b *Buffer) Advance(from int32) {
  120. if from < 0 {
  121. from += b.Len()
  122. }
  123. b.start += from
  124. }
  125. // Len returns the length of the buffer content.
  126. func (b *Buffer) Len() int32 {
  127. if b == nil {
  128. return 0
  129. }
  130. return b.end - b.start
  131. }
  132. // IsEmpty returns true if the buffer is empty.
  133. func (b *Buffer) IsEmpty() bool {
  134. return b.Len() == 0
  135. }
  136. // IsFull returns true if the buffer has no more room to grow.
  137. func (b *Buffer) IsFull() bool {
  138. return b != nil && b.end == int32(len(b.v))
  139. }
  140. // Write implements Write method in io.Writer.
  141. func (b *Buffer) Write(data []byte) (int, error) {
  142. nBytes := copy(b.v[b.end:], data)
  143. b.end += int32(nBytes)
  144. return nBytes, nil
  145. }
  146. // WriteByte writes a single byte into the buffer.
  147. func (b *Buffer) WriteByte(v byte) error {
  148. if b.IsFull() {
  149. return newError("buffer full")
  150. }
  151. b.v[b.end] = v
  152. b.end++
  153. return nil
  154. }
  155. // WriteString implements io.StringWriter.
  156. func (b *Buffer) WriteString(s string) (int, error) {
  157. return b.Write([]byte(s))
  158. }
  159. // ReadByte implements io.ByteReader
  160. func (b *Buffer) ReadByte() (byte, error) {
  161. if b.start == b.end {
  162. return 0, io.EOF
  163. }
  164. nb := b.v[b.start]
  165. b.start++
  166. return nb, nil
  167. }
  168. // ReadBytes implements bufio.Reader.ReadBytes
  169. func (b *Buffer) ReadBytes(length int32) ([]byte, error) {
  170. if b.end-b.start < length {
  171. return nil, io.EOF
  172. }
  173. nb := b.v[b.start : b.start+length]
  174. b.start += length
  175. return nb, nil
  176. }
  177. // Read implements io.Reader.Read().
  178. func (b *Buffer) Read(data []byte) (int, error) {
  179. if b.Len() == 0 {
  180. return 0, io.EOF
  181. }
  182. nBytes := copy(data, b.v[b.start:b.end])
  183. if int32(nBytes) == b.Len() {
  184. b.Clear()
  185. } else {
  186. b.start += int32(nBytes)
  187. }
  188. return nBytes, nil
  189. }
  190. // ReadFrom implements io.ReaderFrom.
  191. func (b *Buffer) ReadFrom(reader io.Reader) (int64, error) {
  192. n, err := reader.Read(b.v[b.end:])
  193. b.end += int32(n)
  194. return int64(n), err
  195. }
  196. // ReadFullFrom reads exact size of bytes from given reader, or until error occurs.
  197. func (b *Buffer) ReadFullFrom(reader io.Reader, size int32) (int64, error) {
  198. end := b.end + size
  199. if end > int32(len(b.v)) {
  200. v := end
  201. return 0, newError("out of bound: ", v)
  202. }
  203. n, err := io.ReadFull(reader, b.v[b.end:end])
  204. b.end += int32(n)
  205. return int64(n), err
  206. }
  207. // String returns the string form of this Buffer.
  208. func (b *Buffer) String() string {
  209. return string(b.Bytes())
  210. }