byte_input.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package internal
  2. import (
  3. "encoding/binary"
  4. "io"
  5. )
  6. // ByteInput typed interface around io.Reader or raw bytes
  7. type ByteInput interface {
  8. // Next returns a slice containing the next n bytes from the buffer,
  9. // advancing the buffer as if the bytes had been returned by Read.
  10. Next(n int) ([]byte, error)
  11. // ReadUInt32 reads uint32 with LittleEndian order
  12. ReadUInt32() (uint32, error)
  13. // ReadUInt16 reads uint16 with LittleEndian order
  14. ReadUInt16() (uint16, error)
  15. // GetReadBytes returns read bytes
  16. GetReadBytes() int64
  17. // SkipBytes skips exactly n bytes
  18. SkipBytes(n int) error
  19. }
  20. // NewByteInputFromReader creates reader wrapper
  21. func NewByteInputFromReader(reader io.Reader) ByteInput {
  22. return &ByteInputAdapter{
  23. r: reader,
  24. readBytes: 0,
  25. }
  26. }
  27. // NewByteInput creates raw bytes wrapper
  28. func NewByteInput(buf []byte) ByteInput {
  29. return &ByteBuffer{
  30. buf: buf,
  31. off: 0,
  32. }
  33. }
  34. // ByteBuffer raw bytes wrapper
  35. type ByteBuffer struct {
  36. buf []byte
  37. off int
  38. }
  39. // Next returns a slice containing the next n bytes from the reader
  40. // If there are fewer bytes than the given n, io.ErrUnexpectedEOF will be returned
  41. func (b *ByteBuffer) Next(n int) ([]byte, error) {
  42. m := len(b.buf) - b.off
  43. if n > m {
  44. return nil, io.ErrUnexpectedEOF
  45. }
  46. data := b.buf[b.off : b.off+n]
  47. b.off += n
  48. return data, nil
  49. }
  50. // ReadUInt32 reads uint32 with LittleEndian order
  51. func (b *ByteBuffer) ReadUInt32() (uint32, error) {
  52. if len(b.buf)-b.off < 4 {
  53. return 0, io.ErrUnexpectedEOF
  54. }
  55. v := binary.LittleEndian.Uint32(b.buf[b.off:])
  56. b.off += 4
  57. return v, nil
  58. }
  59. // ReadUInt16 reads uint16 with LittleEndian order
  60. func (b *ByteBuffer) ReadUInt16() (uint16, error) {
  61. if len(b.buf)-b.off < 2 {
  62. return 0, io.ErrUnexpectedEOF
  63. }
  64. v := binary.LittleEndian.Uint16(b.buf[b.off:])
  65. b.off += 2
  66. return v, nil
  67. }
  68. // GetReadBytes returns read bytes
  69. func (b *ByteBuffer) GetReadBytes() int64 {
  70. return int64(b.off)
  71. }
  72. // SkipBytes skips exactly n bytes
  73. func (b *ByteBuffer) SkipBytes(n int) error {
  74. m := len(b.buf) - b.off
  75. if n > m {
  76. return io.ErrUnexpectedEOF
  77. }
  78. b.off += n
  79. return nil
  80. }
  81. // Reset resets the given buffer with a new byte slice
  82. func (b *ByteBuffer) Reset(buf []byte) {
  83. b.buf = buf
  84. b.off = 0
  85. }
  86. // ByteInputAdapter reader wrapper
  87. type ByteInputAdapter struct {
  88. r io.Reader
  89. readBytes int
  90. }
  91. // Next returns a slice containing the next n bytes from the buffer,
  92. // advancing the buffer as if the bytes had been returned by Read.
  93. func (b *ByteInputAdapter) Next(n int) ([]byte, error) {
  94. buf := make([]byte, n)
  95. m, err := io.ReadAtLeast(b.r, buf, n)
  96. b.readBytes += m
  97. if err != nil {
  98. return nil, err
  99. }
  100. return buf, nil
  101. }
  102. // ReadUInt32 reads uint32 with LittleEndian order
  103. func (b *ByteInputAdapter) ReadUInt32() (uint32, error) {
  104. buf, err := b.Next(4)
  105. if err != nil {
  106. return 0, err
  107. }
  108. return binary.LittleEndian.Uint32(buf), nil
  109. }
  110. // ReadUInt16 reads uint16 with LittleEndian order
  111. func (b *ByteInputAdapter) ReadUInt16() (uint16, error) {
  112. buf, err := b.Next(2)
  113. if err != nil {
  114. return 0, err
  115. }
  116. return binary.LittleEndian.Uint16(buf), nil
  117. }
  118. // GetReadBytes returns read bytes
  119. func (b *ByteInputAdapter) GetReadBytes() int64 {
  120. return int64(b.readBytes)
  121. }
  122. // SkipBytes skips exactly n bytes
  123. func (b *ByteInputAdapter) SkipBytes(n int) error {
  124. _, err := b.Next(n)
  125. return err
  126. }
  127. // Reset resets the given buffer with a new stream
  128. func (b *ByteInputAdapter) Reset(stream io.Reader) {
  129. b.r = stream
  130. b.readBytes = 0
  131. }