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