buffer.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. return b.v[b.start : b.start+to]
  97. }
  98. // Resize cuts the buffer at the given position.
  99. func (b *Buffer) Resize(from, to int32) {
  100. if from < 0 {
  101. from += b.Len()
  102. }
  103. if to < 0 {
  104. to += b.Len()
  105. }
  106. if to < from {
  107. panic("Invalid slice")
  108. }
  109. b.end = b.start + to
  110. b.start += from
  111. }
  112. // Advance cuts the buffer at the given position.
  113. func (b *Buffer) Advance(from int32) {
  114. if from < 0 {
  115. from += b.Len()
  116. }
  117. b.start += from
  118. }
  119. // Len returns the length of the buffer content.
  120. func (b *Buffer) Len() int32 {
  121. if b == nil {
  122. return 0
  123. }
  124. return b.end - b.start
  125. }
  126. // IsEmpty returns true if the buffer is empty.
  127. func (b *Buffer) IsEmpty() bool {
  128. return b.Len() == 0
  129. }
  130. // IsFull returns true if the buffer has no more room to grow.
  131. func (b *Buffer) IsFull() bool {
  132. return b != nil && b.end == int32(len(b.v))
  133. }
  134. // Write implements Write method in io.Writer.
  135. func (b *Buffer) Write(data []byte) (int, error) {
  136. nBytes := copy(b.v[b.end:], data)
  137. b.end += int32(nBytes)
  138. return nBytes, nil
  139. }
  140. // WriteByte writes a single byte into the buffer.
  141. func (b *Buffer) WriteByte(v byte) error {
  142. if b.IsFull() {
  143. return newError("buffer full")
  144. }
  145. b.v[b.end] = v
  146. b.end++
  147. return nil
  148. }
  149. // WriteString implements io.StringWriter.
  150. func (b *Buffer) WriteString(s string) (int, error) {
  151. return b.Write([]byte(s))
  152. }
  153. // Read implements io.Reader.Read().
  154. func (b *Buffer) Read(data []byte) (int, error) {
  155. if b.Len() == 0 {
  156. return 0, io.EOF
  157. }
  158. nBytes := copy(data, b.v[b.start:b.end])
  159. if int32(nBytes) == b.Len() {
  160. b.Clear()
  161. } else {
  162. b.start += int32(nBytes)
  163. }
  164. return nBytes, nil
  165. }
  166. // ReadFrom implements io.ReaderFrom.
  167. func (b *Buffer) ReadFrom(reader io.Reader) (int64, error) {
  168. n, err := reader.Read(b.v[b.end:])
  169. b.end += int32(n)
  170. return int64(n), err
  171. }
  172. // ReadFullFrom reads exact size of bytes from given reader, or until error occurs.
  173. func (b *Buffer) ReadFullFrom(reader io.Reader, size int32) (int64, error) {
  174. end := b.end + size
  175. if end > int32(len(b.v)) {
  176. v := end
  177. return 0, newError("out of bound: ", v)
  178. }
  179. n, err := io.ReadFull(reader, b.v[b.end:end])
  180. b.end += int32(n)
  181. return int64(n), err
  182. }
  183. // String returns the string form of this Buffer.
  184. func (b *Buffer) String() string {
  185. return string(b.Bytes())
  186. }