|
|
@ -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 {
|
|
|
|
b := byte(r.ReadBits(8))
|
|
|
|
r.err = err
|
|
|
|
if err := r.Err(); err != nil {
|
|
|
|
return 0
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|