more bit reader stuff

master
Jordan Orelli 8 years ago
parent 8010c47cab
commit 7d589f8f62

@ -49,4 +49,25 @@ func (r *Reader) ReadBits(bits uint) (n uint64) {
return
}
// ReadByte reads a single byte, regardless of alignment.
func (r *Reader) ReadByte() (byte, error) {
b := byte(r.ReadBits(8))
if err := r.Err(); err != nil {
return 0, err
}
return b, nil
}
// Read reads like an io.Reader, taking care of alignment internally.
func (r *Reader) Read(buf []byte) (int, error) {
for i := 0; i < len(buf); i++ {
b, err := r.ReadByte()
if err != nil {
return 0, err
}
buf[i] = b
}
return len(buf), nil
}
func (r *Reader) Err() error { return r.err }

@ -11,7 +11,8 @@ var (
badFood = []byte{0x8b, 0xad, 0xf0, 0x0d}
)
func TestRead(t *testing.T) {
// test the bit-level reads
func TestReadBits(t *testing.T) {
assert := assert.New(t)
var r *Reader
@ -50,3 +51,22 @@ func TestRead(t *testing.T) {
// ^----------^
assert.Equal(uint64(0xd), r.ReadBits(10))
}
// test the Read calls, satisfying io.Reader
func TestRead(t *testing.T) {
assert := assert.New(t)
var r *Reader
r = NewBytesReader(badFood)
// 1000 1011 1010 1101 1111 0000 0000 1101
r.ReadBits(1)
// 0001 0111 0101 1011 1110 0000 0001 101
buf := make([]byte, 3)
n, err := r.Read(buf)
assert.NoError(err)
assert.Equal(3, n)
expected := []byte{0x17, 0x5b, 0xe0}
assert.Equal(expected, buf)
}

Loading…
Cancel
Save