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

feat: [FLAC] drop io.Closer element in Stream and Encoder types #70

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ import (
type Encoder struct {
// FLAC stream of encoder.
*Stream
// Underlying io.Writer to the output stream.
// Underlying io.Writer or io.WriteCloser to the output stream.
w io.Writer
// io.Closer to flush pending writes to output stream.
c io.Closer
// Minimum and maximum block size (in samples) of frames written by encoder.
blockSizeMin, blockSizeMax uint16
// Minimum and maximum frame size (in bytes) of frames written by encoder.
Expand All @@ -43,9 +41,7 @@ func NewEncoder(w io.Writer, info *meta.StreamInfo, blocks ...*meta.Block) (*Enc
w: w,
md5sum: md5.New(),
}
if c, ok := w.(io.Closer); ok {
enc.c = c
}

bw := bitio.NewWriter(w)
if _, err := bw.Write(flacSignature); err != nil {
return nil, errutil.Err(err)
Expand Down Expand Up @@ -102,8 +98,8 @@ func (enc *Encoder) Close() error {
return errutil.Err(err)
}
}
if enc.c != nil {
return enc.c.Close()
if closer, ok := enc.w.(io.Closer); ok {
return closer.Close()
}
return nil
}
4 changes: 3 additions & 1 deletion encode_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ func (enc *Encoder) encodeFrameHeader(w io.Writer, hdr frame.Header) error {
h := crc8.NewATM()
hw := io.MultiWriter(h, w)
bw := bitio.NewWriter(hw)
enc.c = bw

// Closing the *bitio.Writer will not close the underlying writer
defer bw.Close()

// Sync code: 11111111111110
if err := bw.WriteBits(0x3FFE, 14); err != nil {
Expand Down
17 changes: 7 additions & 10 deletions flac.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ type Stream struct {
// is relative to this position.
dataStart int64

// Underlying io.Reader.
// Underlying io.Reader, or io.ReadCloser.
r io.Reader
// Underlying io.Closer of file if opened with Open and ParseFile, and nil
// otherwise.
c io.Closer
}

// New creates a new Stream for accessing the audio samples of r. It reads and
Expand Down Expand Up @@ -264,7 +261,7 @@ func Open(path string) (stream *Stream, err error) {
if err != nil {
return nil, err
}
stream.c = f

return stream, err
}

Expand All @@ -285,16 +282,16 @@ func ParseFile(path string) (stream *Stream, err error) {
if err != nil {
return nil, err
}
stream.c = f

return stream, err
}

// Close closes the stream if opened through a call to Open or ParseFile, and
// performs no operation otherwise.
// Close closes the stream gracefully if the underlying io.Reader also implements the io.Closer interface.
func (stream *Stream) Close() error {
if stream.c != nil {
return stream.c.Close()
if closer, ok := stream.r.(io.Closer); ok {
return closer.Close()
}

return nil
}

Expand Down
Loading