buffer.go 4.4 KB

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