Skip to content

Commit

Permalink
feat: [FLAC] drop io.Closer element in Stream and Encoder types (#70)
Browse files Browse the repository at this point in the history
flac: simplify internal handling of io.Closer for io.Reader and io.Writer

* flac: drop io.Closer element in Stream type; get it from io.Reader if implemented

* encoder: drop io.Closer element in Encoder type; get it from io.Writer if implemented

* encoder/frame: defer bitio.Writer's closure, instead of replacing the encoder's closer, since bitio.Writer does not close the underlying writer
  • Loading branch information
zalgonoise authored May 8, 2024
1 parent fbea67c commit c38ea50
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
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

0 comments on commit c38ea50

Please sign in to comment.