Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReadBits still drops bits on encountering the last byte #11

Open
itchyny opened this issue Nov 16, 2020 · 1 comment
Open

ReadBits still drops bits on encountering the last byte #11

itchyny opened this issue Nov 16, 2020 · 1 comment

Comments

@itchyny
Copy link

itchyny commented Nov 16, 2020

This is quite similar to #6 but there is another failure case when Read() returns both data and io.EOF.
Here's a sample code.

  • *myReader returns io.EOF with the last byte.
  • Read with ReadBits(n >= 8) including the last byte.
package main

import (
	"fmt"
	"io"
	"os"

	bitstream "github.com/dgryski/go-bitstream"
)

type myReader struct {
	n int
}

func (e *myReader) Read(p []byte) (int, error) {
	switch e.n {
	case 0:
		p[0] = '\xF0'
		e.n++
		return 1, nil
	case 1:
		p[0] = '\x1F'
		e.n++
		return 1, io.EOF
	default:
		return 0, io.EOF
	}
}

func main() {
	br := bitstream.NewReader(&myReader{})
	b, err := br.ReadBits(4)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		os.Exit(1)
	}
	fmt.Printf("%q\n", b)
	b, err = br.ReadBits(8)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		os.Exit(1)
	}
	fmt.Printf("%q\n", b)
	b, err = br.ReadBits(4)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
	}
	fmt.Printf("%q\n", b)
	_, err = br.ReadBits(4)
	if err != io.EOF {
		panic("unreachable")
	}
}

The result is

'\x0f'
EOF
exit status 1

But, I expect the following output, no error.

'\x0f'
'\x01'
'\x0f'
@itchyny
Copy link
Author

itchyny commented Dec 7, 2020

@dgryski Any thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant