Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

byte_input.go 3.2KB

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