buffer.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // Supplier is a writer that writes contents into the given buffer.
  11. type Supplier func([]byte) (int, error)
  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. }
  20. // Release recycles the buffer into an internal buffer pool.
  21. func (b *Buffer) Release() {
  22. if b == nil || b.v == nil {
  23. return
  24. }
  25. pool.Put(b.v)
  26. b.v = nil
  27. b.Clear()
  28. }
  29. // Clear clears the content of the buffer, results an empty buffer with
  30. // Len() = 0.
  31. func (b *Buffer) Clear() {
  32. b.start = 0
  33. b.end = 0
  34. }
  35. // AppendSupplier appends the content of a BytesWriter to the buffer.
  36. func (b *Buffer) AppendSupplier(writer Supplier) error {
  37. nBytes, err := writer(b.v[b.end:])
  38. b.end += int32(nBytes)
  39. return err
  40. }
  41. // Byte returns the bytes at index.
  42. func (b *Buffer) Byte(index int32) byte {
  43. return b.v[b.start+index]
  44. }
  45. // SetByte sets the byte value at index.
  46. func (b *Buffer) SetByte(index int32, value byte) {
  47. b.v[b.start+index] = value
  48. }
  49. // Bytes returns the content bytes of this Buffer.
  50. func (b *Buffer) Bytes() []byte {
  51. return b.v[b.start:b.end]
  52. }
  53. // Reset resets the content of the Buffer with a supplier.
  54. func (b *Buffer) Reset(writer Supplier) error {
  55. nBytes, err := writer(b.v)
  56. b.start = 0
  57. b.end = int32(nBytes)
  58. if b.end > int32(len(b.v)) {
  59. b.end = int32(len(b.v))
  60. }
  61. return err
  62. }
  63. // BytesRange returns a slice of this buffer with given from and to boundary.
  64. func (b *Buffer) BytesRange(from, to int32) []byte {
  65. if from < 0 {
  66. from += b.Len()
  67. }
  68. if to < 0 {
  69. to += b.Len()
  70. }
  71. return b.v[b.start+from : b.start+to]
  72. }
  73. // BytesFrom returns a slice of this Buffer starting from the given position.
  74. func (b *Buffer) BytesFrom(from int32) []byte {
  75. if from < 0 {
  76. from += b.Len()
  77. }
  78. return b.v[b.start+from : b.end]
  79. }
  80. // BytesTo returns a slice of this Buffer from start to the given position.
  81. func (b *Buffer) BytesTo(to int32) []byte {
  82. if to < 0 {
  83. to += b.Len()
  84. }
  85. return b.v[b.start : b.start+to]
  86. }
  87. // Resize cuts the buffer at the given position.
  88. func (b *Buffer) Resize(from, to int32) {
  89. if from < 0 {
  90. from += b.Len()
  91. }
  92. if to < 0 {
  93. to += b.Len()
  94. }
  95. if to < from {
  96. panic("Invalid slice")
  97. }
  98. b.end = b.start + to
  99. b.start += from
  100. }
  101. // Advance cuts the buffer at the given position.
  102. func (b *Buffer) Advance(from int32) {
  103. if from < 0 {
  104. from += b.Len()
  105. }
  106. b.start += from
  107. }
  108. // Len returns the length of the buffer content.
  109. func (b *Buffer) Len() int32 {
  110. if b == nil {
  111. return 0
  112. }
  113. return b.end - b.start
  114. }
  115. // IsEmpty returns true if the buffer is empty.
  116. func (b *Buffer) IsEmpty() bool {
  117. return b.Len() == 0
  118. }
  119. // IsFull returns true if the buffer has no more room to grow.
  120. func (b *Buffer) IsFull() bool {
  121. return b.end == int32(len(b.v))
  122. }
  123. // Write implements Write method in io.Writer.
  124. func (b *Buffer) Write(data []byte) (int, error) {
  125. nBytes := copy(b.v[b.end:], data)
  126. b.end += int32(nBytes)
  127. return nBytes, nil
  128. }
  129. // WriteBytes appends one or more bytes to the end of the buffer.
  130. func (b *Buffer) WriteBytes(bytes ...byte) (int, error) {
  131. return b.Write(bytes)
  132. }
  133. // Read implements io.Reader.Read().
  134. func (b *Buffer) Read(data []byte) (int, error) {
  135. if b.Len() == 0 {
  136. return 0, io.EOF
  137. }
  138. nBytes := copy(data, b.v[b.start:b.end])
  139. if int32(nBytes) == b.Len() {
  140. b.Clear()
  141. } else {
  142. b.start += int32(nBytes)
  143. }
  144. return nBytes, nil
  145. }
  146. // String returns the string form of this Buffer.
  147. func (b *Buffer) String() string {
  148. return string(b.Bytes())
  149. }
  150. var pool = bytespool.GetPool(Size)
  151. // New creates a Buffer with 0 length and 2K capacity.
  152. func New() *Buffer {
  153. return &Buffer{
  154. v: pool.Get().([]byte),
  155. }
  156. }