audio/vorbis: bug fix: an int16Stream with io.Reader didn't work

Closes #3192
This commit is contained in:
Hajime Hoshi 2025-02-11 14:44:47 +09:00
parent 6cdc2a693b
commit 4df5d02c36
2 changed files with 9 additions and 14 deletions

View File

@ -113,14 +113,7 @@ func (s *i16Stream) Read(b []byte) (int, error) {
s.i16Reader = newInt16BytesReaderFromFloat32Reader(s.vorbisReader) s.i16Reader = newInt16BytesReaderFromFloat32Reader(s.vorbisReader)
} }
l := s.totalBytes() - s.posInBytes l := int64(len(b))
if l > int64(len(b)) {
l = int64(len(b))
}
if l < 0 {
return 0, io.EOF
}
retry: retry:
n, err := s.i16Reader.Read(b[:l]) n, err := s.i16Reader.Read(b[:l])
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
@ -130,12 +123,8 @@ retry:
// When l is too small, decoder's Read might return 0 for a while. Let's retry. // When l is too small, decoder's Read might return 0 for a while. Let's retry.
goto retry goto retry
} }
s.posInBytes += int64(n) s.posInBytes += int64(n)
if s.posInBytes == s.totalBytes() || err == io.EOF { return n, err
return n, io.EOF
}
return n, nil
} }
func (s *i16Stream) Seek(offset int64, whence int) (int64, error) { func (s *i16Stream) Seek(offset int64, whence int) (int64, error) {

View File

@ -138,7 +138,13 @@ func TestNonSeeker(t *testing.T) {
t.Errorf("s.SampleRate(): got: %d, want: %d", got, want) t.Errorf("s.SampleRate(): got: %d, want: %d", got, want)
} }
// TODO: Check the result of io.ReadAll (#3192). buf, err := io.ReadAll(s)
if err != nil {
t.Errorf("io.ReadAll: %v", err)
}
if len(buf) == 0 {
t.Errorf("len(buf): got: %d, want: > 0", len(buf))
}
} }
func TestNonSeekerF32(t *testing.T) { func TestNonSeekerF32(t *testing.T) {