buffer.go 4.5 KB

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