buffer.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package utils
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "io"
  6. )
  7. type IOBuffer struct {
  8. buf []byte // contents are the bytes buf[off : len(buf)]
  9. off int // read at &buf[off], write at &buf[len(buf)]
  10. }
  11. func (b *IOBuffer) Next(n int) []byte {
  12. m := b.Len()
  13. if n > m {
  14. n = m
  15. }
  16. data := b.buf[b.off : b.off+n]
  17. b.off += n
  18. return data
  19. }
  20. func (b *IOBuffer) Uint16() (uint16, error) {
  21. if b.Len() > 1 {
  22. return binary.BigEndian.Uint16(b.Next(2)), nil
  23. }
  24. return 0, io.EOF
  25. }
  26. func (b *IOBuffer) Skip(n int) (err error) {
  27. _, err = b.ReadN(n)
  28. return
  29. }
  30. func (b *IOBuffer) Uint32() (uint32, error) {
  31. if b.Len() > 3 {
  32. return binary.BigEndian.Uint32(b.Next(4)), nil
  33. }
  34. return 0, io.EOF
  35. }
  36. func (b *IOBuffer) ReadN(length int) ([]byte, error) {
  37. if b.Len() >= length {
  38. return b.Next(length), nil
  39. }
  40. return nil, io.EOF
  41. }
  42. //func (b *IOBuffer) Read(buf []byte) (n int, err error) {
  43. // var ret []byte
  44. // ret, err = b.ReadN(len(buf))
  45. // copy(buf, ret)
  46. // return len(ret), err
  47. //}
  48. // empty reports whether the unread portion of the buffer is empty.
  49. func (b *IOBuffer) empty() bool { return b.Len() <= b.off }
  50. func (b *IOBuffer) ReadByte() (byte, error) {
  51. if b.empty() {
  52. // Buffer is empty, reset to recover space.
  53. b.Reset()
  54. return 0, io.EOF
  55. }
  56. c := b.buf[b.off]
  57. b.off++
  58. return c, nil
  59. }
  60. func (b *IOBuffer) Reset() {
  61. b.buf = b.buf[:0]
  62. b.off = 0
  63. }
  64. func (b *IOBuffer) Len() int { return len(b.buf) - b.off }
  65. // tryGrowByReslice is a inlineable version of grow for the fast-case where the
  66. // internal buffer only needs to be resliced.
  67. // It returns the index where bytes should be written and whether it succeeded.
  68. func (b *IOBuffer) tryGrowByReslice(n int) (int, bool) {
  69. if l := len(b.buf); n <= cap(b.buf)-l {
  70. b.buf = b.buf[:l+n]
  71. return l, true
  72. }
  73. return 0, false
  74. }
  75. var ErrTooLarge = errors.New("IOBuffer: too large")
  76. func (b *IOBuffer) Write(p []byte) (n int, err error) {
  77. l := copy(b.buf, b.buf[b.off:])
  78. b.buf = append(b.buf[:l], p...)
  79. b.off = 0
  80. // println(b.buf, b.off, b.buf[b.off], b.buf[b.off+1], b.buf[b.off+2], b.buf[b.off+3])
  81. return len(p), nil
  82. // defer func() {
  83. // if recover() != nil {
  84. // panic(ErrTooLarge)
  85. // }
  86. // }()
  87. // l := len(p)
  88. // oldLen := len(b.buf)
  89. // m, ok := b.tryGrowByReslice(l)
  90. // if !ok {
  91. // m = oldLen - b.off
  92. // buf := append(append(([]byte)(nil), b.buf[b.off:]...), p...)
  93. // b.off = 0
  94. // b.buf = buf
  95. // }
  96. // return copy(b.buf[m:], p), nil
  97. }