bytebuffer.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package bytebufferpool
  2. import "io"
  3. // ByteBuffer provides byte buffer, which can be used for minimizing
  4. // memory allocations.
  5. //
  6. // ByteBuffer may be used with functions appending data to the given []byte
  7. // slice. See example code for details.
  8. //
  9. // Use Get for obtaining an empty byte buffer.
  10. type ByteBuffer struct {
  11. // B is a byte buffer to use in append-like workloads.
  12. // See example code for details.
  13. B []byte
  14. }
  15. // Len returns the size of the byte buffer.
  16. func (b *ByteBuffer) Len() int {
  17. return len(b.B)
  18. }
  19. // ReadFrom implements io.ReaderFrom.
  20. //
  21. // The function appends all the data read from r to b.
  22. func (b *ByteBuffer) ReadFrom(r io.Reader) (int64, error) {
  23. p := b.B
  24. nStart := int64(len(p))
  25. nMax := int64(cap(p))
  26. n := nStart
  27. if nMax == 0 {
  28. nMax = 64
  29. p = make([]byte, nMax)
  30. } else {
  31. p = p[:nMax]
  32. }
  33. for {
  34. if n == nMax {
  35. nMax *= 2
  36. bNew := make([]byte, nMax)
  37. copy(bNew, p)
  38. p = bNew
  39. }
  40. nn, err := r.Read(p[n:])
  41. n += int64(nn)
  42. if err != nil {
  43. b.B = p[:n]
  44. n -= nStart
  45. if err == io.EOF {
  46. return n, nil
  47. }
  48. return n, err
  49. }
  50. }
  51. }
  52. // WriteTo implements io.WriterTo.
  53. func (b *ByteBuffer) WriteTo(w io.Writer) (int64, error) {
  54. n, err := w.Write(b.B)
  55. return int64(n), err
  56. }
  57. // Bytes returns b.B, i.e. all the bytes accumulated in the buffer.
  58. //
  59. // The purpose of this function is bytes.Buffer compatibility.
  60. func (b *ByteBuffer) Bytes() []byte {
  61. return b.B
  62. }
  63. // Write implements io.Writer - it appends p to ByteBuffer.B
  64. func (b *ByteBuffer) Write(p []byte) (int, error) {
  65. b.B = append(b.B, p...)
  66. return len(p), nil
  67. }
  68. // WriteByte appends the byte c to the buffer.
  69. //
  70. // The purpose of this function is bytes.Buffer compatibility.
  71. //
  72. // The function always returns nil.
  73. func (b *ByteBuffer) WriteByte(c byte) error {
  74. b.B = append(b.B, c)
  75. return nil
  76. }
  77. // WriteString appends s to ByteBuffer.B.
  78. func (b *ByteBuffer) WriteString(s string) (int, error) {
  79. b.B = append(b.B, s...)
  80. return len(s), nil
  81. }
  82. // Set sets ByteBuffer.B to p.
  83. func (b *ByteBuffer) Set(p []byte) {
  84. b.B = append(b.B[:0], p...)
  85. }
  86. // SetString sets ByteBuffer.B to s.
  87. func (b *ByteBuffer) SetString(s string) {
  88. b.B = append(b.B[:0], s...)
  89. }
  90. // String returns string representation of ByteBuffer.B.
  91. func (b *ByteBuffer) String() string {
  92. return string(b.B)
  93. }
  94. // Reset makes ByteBuffer.B empty.
  95. func (b *ByteBuffer) Reset() {
  96. b.B = b.B[:0]
  97. }