buffer.go 4.4 KB

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