buffer.go 4.3 KB

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