Skip to content
This repository has been archived by the owner on Apr 2, 2023. It is now read-only.

Commit

Permalink
Use io.ReadSeeker instead of io.ReadCloser (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
dudk authored and hajimehoshi committed Mar 20, 2019
1 parent 90191ee commit 427f6ab
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 20 deletions.
2 changes: 1 addition & 1 deletion bench_test.go
Expand Up @@ -26,7 +26,7 @@ func BenchmarkDecode(b *testing.B) {
if err != nil {
b.Fatal(err)
}
src := &bytesReadCloser{bytes.NewReader(buf)}
src := bytes.NewReader(buf)
for i := 0; i < b.N; i++ {
if _, err := src.Seek(0, io.SeekStart); err != nil {
b.Fatal(err)
Expand Down
8 changes: 4 additions & 4 deletions decode.go
Expand Up @@ -111,9 +111,9 @@ func (d *Decoder) Seek(offset int64, whence int) (int64, error) {
return npos, nil
}

// Close is io.Closer's Close.
// Close is io.Closer's Close. Close does nothing and always returns nil.
func (d *Decoder) Close() error {
return d.source.Close()
return nil
}

// SampleRate returns the sample rate like 44100.
Expand Down Expand Up @@ -186,12 +186,12 @@ func (d *Decoder) Length() int64 {
return d.length
}

// NewDecoder decodes the given io.ReadCloser and returns a decoded stream.
// NewDecoder decodes the given io.Reader and returns a decoded stream.
//
// The stream is always formatted as 16bit (little endian) 2 channels
// even if the source is single channel MP3.
// Thus, a sample always consists of 4 bytes.
func NewDecoder(r io.ReadCloser) (*Decoder, error) {
func NewDecoder(r io.Reader) (*Decoder, error) {
s := &source{
reader: r,
}
Expand Down
10 changes: 1 addition & 9 deletions fuzzing_test.go
Expand Up @@ -19,14 +19,6 @@ import (
"testing"
)

type bytesReadCloser struct {
*bytes.Reader
}

func (b *bytesReadCloser) Close() error {
return nil
}

func TestFuzzing(t *testing.T) {
inputs := []string{
// #3
Expand Down Expand Up @@ -109,7 +101,7 @@ func TestFuzzing(t *testing.T) {
"0000000000000",
}
for _, input := range inputs {
b := &bytesReadCloser{bytes.NewReader([]byte(input))}
b := bytes.NewReader([]byte(input))
_, _ = NewDecoder(b)
}
}
7 changes: 1 addition & 6 deletions source.go
Expand Up @@ -19,7 +19,7 @@ import (
)

type source struct {
reader io.ReadCloser
reader io.Reader
buf []byte
pos int64
}
Expand All @@ -38,11 +38,6 @@ func (s *source) Seek(position int64, whence int) (int64, error) {
return n, nil
}

func (s *source) Close() error {
s.buf = nil
return s.reader.Close()
}

func (s *source) skipTags() error {
buf := make([]byte, 3)
if _, err := s.ReadFull(buf); err != nil {
Expand Down

0 comments on commit 427f6ab

Please sign in to comment.