buffer.go 4.8 KB

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