interfaces are fun

master
Jordan Orelli 8 years ago
parent 63ab3c62e8
commit 2bf18c6fce

@ -6,11 +6,12 @@ import (
"io" "io"
) )
// Reader allows for the reading and skipping of bits and bytes.
type Reader interface { type Reader interface {
ReadBits(uint) uint64 ReadBits(uint) uint64
DiscardBits(int) DiscardBits(int)
ReadByte() (byte, error) ReadByte() byte
Read([]byte) (int, error) Read([]byte) int
DiscardBytes(int) DiscardBytes(int)
Err() error Err() error
} }

@ -17,6 +17,10 @@ type streamReader struct {
// ReadBits reads the given number of bits and returns them in the // ReadBits reads the given number of bits and returns them in the
// least-significant part of a uint64. // least-significant part of a uint64.
func (r *streamReader) ReadBits(bits uint) (n uint64) { func (r *streamReader) ReadBits(bits uint) (n uint64) {
if r.err != nil {
return 0
}
for bits > r.bits { for bits > r.bits {
b, err := r.src.ReadByte() b, err := r.src.ReadByte()
if err != nil { if err != nil {
@ -38,37 +42,34 @@ func (r *streamReader) DiscardBits(n int) {
} }
// ReadByte reads a single byte, regardless of alignment. // ReadByte reads a single byte, regardless of alignment.
func (r *streamReader) ReadByte() (byte, error) { func (r *streamReader) ReadByte() byte {
if r.bits == 0 { if r.bits == 0 {
return r.src.ReadByte() b, err := r.src.ReadByte()
if err != nil {
r.err = err
return 0
} }
b := byte(r.ReadBits(8)) return b
if err := r.Err(); err != nil {
return 0, err
} }
return b, nil return byte(r.ReadBits(8))
} }
// Read reads like an io.Reader, taking care of alignment internally. // Read reads like an io.Reader, taking care of alignment internally.
func (r *streamReader) Read(buf []byte) (int, error) { func (r *streamReader) Read(buf []byte) int {
for i := 0; i < len(buf); i++ { for i := 0; i < len(buf); i++ {
b, err := r.ReadByte() b := r.ReadByte()
if err != nil { if r.err != nil {
return 0, err return 0
} }
buf[i] = b buf[i] = b
} }
return len(buf), nil return len(buf)
} }
// discards N byte of data on the reader or until EOF // discards N byte of data on the reader or until EOF
func (r *streamReader) DiscardBytes(n int) { func (r *streamReader) DiscardBytes(n int) {
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
_, err := r.ReadByte() r.ReadByte()
if err != nil {
r.err = err
return
}
} }
} }

Loading…
Cancel
Save