buffer.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. bytespool.Free(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. return err
  59. }
  60. // BytesRange returns a slice of this buffer with given from and to boundary.
  61. func (b *Buffer) BytesRange(from, to int32) []byte {
  62. if from < 0 {
  63. from += b.Len()
  64. }
  65. if to < 0 {
  66. to += b.Len()
  67. }
  68. return b.v[b.start+from : b.start+to]
  69. }
  70. // BytesFrom returns a slice of this Buffer starting from the given position.
  71. func (b *Buffer) BytesFrom(from int32) []byte {
  72. if from < 0 {
  73. from += b.Len()
  74. }
  75. return b.v[b.start+from : b.end]
  76. }
  77. // BytesTo returns a slice of this Buffer from start to the given position.
  78. func (b *Buffer) BytesTo(to int32) []byte {
  79. if to < 0 {
  80. to += b.Len()
  81. }
  82. return b.v[b.start : b.start+to]
  83. }
  84. // Resize cuts the buffer at the given position.
  85. func (b *Buffer) Resize(from, to int32) {
  86. if from < 0 {
  87. from += b.Len()
  88. }
  89. if to < 0 {
  90. to += b.Len()
  91. }
  92. if to < from {
  93. panic("Invalid slice")
  94. }
  95. b.end = b.start + to
  96. b.start += from
  97. }
  98. // Advance cuts the buffer at the given position.
  99. func (b *Buffer) Advance(from int32) {
  100. if from < 0 {
  101. from += b.Len()
  102. }
  103. b.start += from
  104. }
  105. // Len returns the length of the buffer content.
  106. func (b *Buffer) Len() int32 {
  107. if b == nil {
  108. return 0
  109. }
  110. return b.end - b.start
  111. }
  112. // IsEmpty returns true if the buffer is empty.
  113. func (b *Buffer) IsEmpty() bool {
  114. return b.Len() == 0
  115. }
  116. // IsFull returns true if the buffer has no more room to grow.
  117. func (b *Buffer) IsFull() bool {
  118. return b.end == int32(len(b.v))
  119. }
  120. // Write implements Write method in io.Writer.
  121. func (b *Buffer) Write(data []byte) (int, error) {
  122. nBytes := copy(b.v[b.end:], data)
  123. b.end += int32(nBytes)
  124. return nBytes, nil
  125. }
  126. // WriteBytes appends one or more bytes to the end of the buffer.
  127. func (b *Buffer) WriteBytes(bytes ...byte) (int, error) {
  128. return b.Write(bytes)
  129. }
  130. // Read implements io.Reader.Read().
  131. func (b *Buffer) Read(data []byte) (int, error) {
  132. if b.Len() == 0 {
  133. return 0, io.EOF
  134. }
  135. nBytes := copy(data, b.v[b.start:b.end])
  136. if int32(nBytes) == b.Len() {
  137. b.Clear()
  138. } else {
  139. b.start += int32(nBytes)
  140. }
  141. return nBytes, nil
  142. }
  143. // String returns the string form of this Buffer.
  144. func (b *Buffer) String() string {
  145. return string(b.Bytes())
  146. }
  147. // New creates a Buffer with 0 length and 2K capacity.
  148. func New() *Buffer {
  149. return &Buffer{
  150. v: bytespool.Alloc(Size),
  151. }
  152. }