Skip to content

Commit d1bdba4

Browse files
committed
fix annoying edge case involving buffer not being completely filled
1 parent a7abadc commit d1bdba4

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

aces.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ type BitReader struct {
3030
// NewBitReader returns a BitReader that reads chunkLen bits at a time from in.
3131
func NewBitReader(chunkLen uint8, in io.Reader) (*BitReader, error) {
3232
// bufSize % chunkLen == 0 so that we never have to read across the buffer boundary
33-
bs := &BitReader{chunkLen: chunkLen, in: in, buf: make([]byte, BufSize-BufSize%int(chunkLen))}
33+
br := &BitReader{chunkLen: chunkLen, in: in, buf: make([]byte, BufSize-BufSize%int(chunkLen))}
3434
var err error
35-
bs.bufN, err = bs.in.Read(bs.buf)
36-
if err != nil {
35+
br.bufN, err = io.ReadFull(br.in, br.buf)
36+
if err != nil && err != io.ErrUnexpectedEOF {
3737
return nil, err
3838
}
39-
return bs, nil
39+
return br, nil
4040
}
4141

4242
// Read returns the next chunkLen bits from the stream. If there is no more data to read, it returns io.EOF.
4343
// For example, if chunkLen is 3 and the next 3 bits are 101, Read returns 5, nil.
4444
func (br *BitReader) Read() (byte, error) {
4545
if br.byteIdx >= br.bufN { // need to read more
46-
n, err := br.in.Read(br.buf)
47-
if err != nil {
46+
n, err := io.ReadFull(br.in, br.buf)
47+
if err != nil && err != io.ErrUnexpectedEOF {
4848
return 0, err
4949
}
5050
br.byteIdx = 0

0 commit comments

Comments
 (0)