buffer.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. // WriteByte writes a single byte into the buffer.
  123. func (b *Buffer) WriteByte(v byte) error {
  124. if b.IsFull() {
  125. return newError("buffer full")
  126. }
  127. b.v[b.end] = v
  128. b.end++
  129. return nil
  130. }
  131. // WriteString implements io.StringWriter.
  132. func (b *Buffer) WriteString(s string) (int, error) {
  133. return b.Write([]byte(s))
  134. }
  135. // Read implements io.Reader.Read().
  136. func (b *Buffer) Read(data []byte) (int, error) {
  137. if b.Len() == 0 {
  138. return 0, io.EOF
  139. }
  140. nBytes := copy(data, b.v[b.start:b.end])
  141. if int32(nBytes) == b.Len() {
  142. b.Clear()
  143. } else {
  144. b.start += int32(nBytes)
  145. }
  146. return nBytes, nil
  147. }
  148. // ReadFrom implements io.ReaderFrom.
  149. func (b *Buffer) ReadFrom(reader io.Reader) (int64, error) {
  150. n, err := reader.Read(b.v[b.end:])
  151. b.end += int32(n)
  152. return int64(n), err
  153. }
  154. // ReadFullFrom reads exact size of bytes from given reader, or until error occurs.
  155. func (b *Buffer) ReadFullFrom(reader io.Reader, size int32) (int64, error) {
  156. end := b.end + size
  157. if end > int32(len(b.v)) {
  158. return 0, newError("out of bound: ", end)
  159. }
  160. n, err := io.ReadFull(reader, b.v[b.end:end])
  161. b.end += int32(n)
  162. return int64(n), err
  163. }
  164. // String returns the string form of this Buffer.
  165. func (b *Buffer) String() string {
  166. return string(b.Bytes())
  167. }
  168. var pool = bytespool.GetPool(Size)
  169. // New creates a Buffer with 0 length and 2K capacity.
  170. func New() *Buffer {
  171. return &Buffer{
  172. v: pool.Get().([]byte),
  173. }
  174. }