buffer.go 4.9 KB

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