buffer.go 4.7 KB

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