reader.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright ©2021 The star-tex Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package iobuf
  5. import (
  6. "encoding/binary"
  7. "fmt"
  8. "io"
  9. )
  10. type Reader struct {
  11. p []byte
  12. c int
  13. }
  14. func NewReader(p []byte) *Reader {
  15. return &Reader{p: p}
  16. }
  17. func (r *Reader) Len() int { return len(r.p) }
  18. func (r *Reader) Pos() int { return r.c }
  19. func (r *Reader) SetPos(p int) { r.c = p }
  20. func (r *Reader) Bytes() []byte { return r.p[r.c:] }
  21. func (r *Reader) PeekU8() uint8 {
  22. return r.p[r.c]
  23. }
  24. func (r *Reader) Read(p []byte) (int, error) {
  25. if r.c >= len(r.p) {
  26. return 0, io.EOF
  27. }
  28. n := copy(p, r.p[r.c:])
  29. r.c += n
  30. return n, nil
  31. }
  32. func (r *Reader) ReadByte() (byte, error) {
  33. if r.c >= len(r.p) {
  34. return 0, io.EOF
  35. }
  36. v := r.p[r.c]
  37. r.c++
  38. return v, nil
  39. }
  40. func (r *Reader) Seek(offset int64, whence int) (int64, error) {
  41. switch whence {
  42. case io.SeekStart:
  43. r.c = int(offset)
  44. case io.SeekCurrent:
  45. r.c += int(offset)
  46. case io.SeekEnd:
  47. r.c = len(r.p) - int(offset)
  48. default:
  49. return 0, fmt.Errorf("rbytes: invalid whence")
  50. }
  51. if r.c < 0 {
  52. return 0, fmt.Errorf("rbytes: negative position")
  53. }
  54. return int64(r.c), nil
  55. }
  56. func (r *Reader) ReadU8() uint8 {
  57. c := r.c
  58. r.c++
  59. return r.p[c]
  60. }
  61. func (r *Reader) ReadU16() uint16 {
  62. var (
  63. beg = r.c
  64. end = r.c + 2
  65. buf = r.p[beg:end]
  66. )
  67. r.c = end
  68. return binary.BigEndian.Uint16(buf)
  69. }
  70. func (r *Reader) ReadU24() uint32 {
  71. var (
  72. beg = r.c
  73. end = r.c + 3
  74. buf = r.p[beg:end]
  75. )
  76. r.c = end
  77. return uint32(buf[0])<<16 | uint32(buf[1])<<8 | uint32(buf[2])
  78. }
  79. func (r *Reader) ReadU32() uint32 {
  80. var (
  81. beg = r.c
  82. end = r.c + 4
  83. buf = r.p[beg:end]
  84. )
  85. r.c = end
  86. return binary.BigEndian.Uint32(buf)
  87. }
  88. func (r *Reader) ReadI8() int8 {
  89. return int8(r.ReadU8())
  90. }
  91. func (r *Reader) ReadI16() int16 {
  92. return int16(r.ReadU16())
  93. }
  94. func (r *Reader) ReadI24() int32 {
  95. var (
  96. beg = r.c
  97. end = r.c + 3
  98. buf = r.p[beg:end]
  99. )
  100. r.c = end
  101. if buf[0] < 128 {
  102. return int32(uint32(buf[0])<<16 | uint32(buf[1])<<8 | uint32(buf[2]))
  103. }
  104. return int32((uint32(buf[0])-256)<<16 | uint32(buf[1])<<8 | uint32(buf[2]))
  105. }
  106. func (r *Reader) ReadI32() int32 {
  107. return int32(r.ReadU32())
  108. }
  109. func (r *Reader) ReadBuf(n int) []byte {
  110. var (
  111. beg = r.c
  112. end = r.c + n
  113. buf = r.p[beg:end]
  114. )
  115. r.c = end
  116. return buf
  117. }
  118. var (
  119. _ io.Reader = (*Reader)(nil)
  120. _ io.ByteReader = (*Reader)(nil)
  121. _ io.Seeker = (*Reader)(nil)
  122. )