mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 00:55:11 +01:00
Add a Close method to rac.Codec{Reader,Writer}
This commit is contained in:
1 parent
fed2073b00
commit
8d408c7c2c
3 files changed
+83
-25
No files matched your search
+44
-11
@@ -49,6 +49,9 @@ type ReaderContext struct {
|
|||||||
//
|
//
|
||||||
// Instances are not required to support concurrent use.
|
// Instances are not required to support concurrent use.
|
||||||
type CodecReader interface {
|
type CodecReader interface {
|
||||||
|
// Close tells the CodecReader that no further calls will be made.
|
||||||
|
Close() error
|
||||||
|
|
||||||
// Accepts returns whether this CodecReader can decode a Codec.
|
// Accepts returns whether this CodecReader can decode a Codec.
|
||||||
Accepts(c Codec) bool
|
Accepts(c Codec) bool
|
||||||
|
|
||||||
@@ -135,6 +138,9 @@ type Reader struct {
|
|||||||
decompressor io.Reader
|
decompressor io.Reader
|
||||||
inImplicitZeroes bool
|
inImplicitZeroes bool
|
||||||
|
|
||||||
|
// closed is whether this Reader is closed.
|
||||||
|
closed bool
|
||||||
|
|
||||||
// currChunk is an io.Reader for the current chunk, used while in "State
|
// currChunk is an io.Reader for the current chunk, used while in "State
|
||||||
// B". It serves zlib-compressed data, which the (non-nil) decompressor
|
// B". It serves zlib-compressed data, which the (non-nil) decompressor
|
||||||
// turns into decompressed data.
|
// turns into decompressed data.
|
||||||
@@ -505,6 +511,8 @@ func (r *Reader) seek(offset int64, whence int, limit int64) (int64, error) {
|
|||||||
|
|
||||||
// Close implements io.Closer.
|
// Close implements io.Closer.
|
||||||
//
|
//
|
||||||
|
// Calling Close will call Close on all of r's CodecReaders.
|
||||||
|
//
|
||||||
// r.ReadSeeker will not be accessed after Close returns. If r.Concurrency is
|
// r.ReadSeeker will not be accessed after Close returns. If r.Concurrency is
|
||||||
// non-zero, this may involve waiting for various goroutines to shut down,
|
// non-zero, this may involve waiting for various goroutines to shut down,
|
||||||
// which may take some time. If the caller does not care about waiting until it
|
// which may take some time. If the caller does not care about waiting until it
|
||||||
@@ -514,19 +522,44 @@ func (r *Reader) seek(offset int64, whence int, limit int64) (int64, error) {
|
|||||||
// It is not safe to call Close from a separate goroutine while another method
|
// It is not safe to call Close from a separate goroutine while another method
|
||||||
// call like Read or Seek is in progress.
|
// call like Read or Seek is in progress.
|
||||||
func (r *Reader) Close() error {
|
func (r *Reader) Close() error {
|
||||||
if err := r.initialize(); err != nil {
|
return r.close(false)
|
||||||
r.concReader.Close()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
r.err = errAlreadyClosed
|
|
||||||
return r.concReader.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CloseWithoutWaiting is like Close but does not wait until it is safe to
|
||||||
|
// close or otherwise release the r.ReadSeeker's resources.
|
||||||
func (r *Reader) CloseWithoutWaiting() error {
|
func (r *Reader) CloseWithoutWaiting() error {
|
||||||
if err := r.initialize(); err != nil {
|
return r.close(true)
|
||||||
r.concReader.CloseWithoutWaiting()
|
}
|
||||||
return err
|
|
||||||
|
func (r *Reader) close(withoutWaiting bool) error {
|
||||||
|
if r.closed {
|
||||||
|
return r.err
|
||||||
}
|
}
|
||||||
r.err = errAlreadyClosed
|
r.closed = true
|
||||||
return r.concReader.CloseWithoutWaiting()
|
|
||||||
|
if err := r.initialize(); r.err == nil {
|
||||||
|
r.err = err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range r.CodecReaders {
|
||||||
|
if err := c.Close(); r.err == nil {
|
||||||
|
r.err = err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if withoutWaiting {
|
||||||
|
if err := r.concReader.CloseWithoutWaiting(); r.err == nil {
|
||||||
|
r.err = err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err := r.concReader.Close(); r.err == nil {
|
||||||
|
r.err = err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.err == nil {
|
||||||
|
r.err = errAlreadyClosed
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return r.err
|
||||||
}
|
}
|
||||||
+28
-13
@@ -156,6 +156,9 @@ const NoResourceUsed int = -1
|
|||||||
//
|
//
|
||||||
// Instances are not required to support concurrent use.
|
// Instances are not required to support concurrent use.
|
||||||
type CodecWriter interface {
|
type CodecWriter interface {
|
||||||
|
// Close tells the CodecWriter that no further calls will be made.
|
||||||
|
Close() error
|
||||||
|
|
||||||
// Clone duplicates this. The clone and the original can run concurrently.
|
// Clone duplicates this. The clone and the original can run concurrently.
|
||||||
Clone() CodecWriter
|
Clone() CodecWriter
|
||||||
|
|
||||||
@@ -331,6 +334,9 @@ type Writer struct {
|
|||||||
// uncompressed are the uncompressed bytes that have been given to this
|
// uncompressed are the uncompressed bytes that have been given to this
|
||||||
// (via the Write method) but not yet compressed as a chunk.
|
// (via the Write method) but not yet compressed as a chunk.
|
||||||
uncompressed writeBuffer
|
uncompressed writeBuffer
|
||||||
|
|
||||||
|
// closed is whether this Writer is closed.
|
||||||
|
closed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Writer) initialize() error {
|
func (w *Writer) initialize() error {
|
||||||
@@ -562,6 +568,8 @@ func (w *Writer) tryCChunk(targetDChunkSize uint64, force bool) error {
|
|||||||
// Close writes the RAC index to w.Writer and marks that w accepts no further
|
// Close writes the RAC index to w.Writer and marks that w accepts no further
|
||||||
// method calls.
|
// method calls.
|
||||||
//
|
//
|
||||||
|
// Calling Close will call Close on w's CodecWriter.
|
||||||
|
//
|
||||||
// For a one pass encoding, no further action is taken. For a two pass encoding
|
// For a one pass encoding, no further action is taken. For a two pass encoding
|
||||||
// (i.e. IndexLocationAtStart), it then copies w.TempFile to w.Writer. Either
|
// (i.e. IndexLocationAtStart), it then copies w.TempFile to w.Writer. Either
|
||||||
// way, if this method returns nil error, the entirety of what was written to
|
// way, if this method returns nil error, the entirety of what was written to
|
||||||
@@ -569,21 +577,28 @@ func (w *Writer) tryCChunk(targetDChunkSize uint64, force bool) error {
|
|||||||
//
|
//
|
||||||
// Closing the underlying w.Writer, w.TempFile or both is the responsibility of
|
// Closing the underlying w.Writer, w.TempFile or both is the responsibility of
|
||||||
// the rac.Writer caller, not the rac.Writer itself.
|
// the rac.Writer caller, not the rac.Writer itself.
|
||||||
//
|
|
||||||
// It is not necessary to call Close, e.g. if an earlier Write call returned a
|
|
||||||
// non-nil error. Unlike an os.File, failing to call rac.Writer.Close will not
|
|
||||||
// leak resources such as file descriptors.
|
|
||||||
func (w *Writer) Close() error {
|
func (w *Writer) Close() error {
|
||||||
if err := w.initialize(); err != nil {
|
if w.closed {
|
||||||
return err
|
return w.err
|
||||||
}
|
}
|
||||||
if err := w.write(true); err != nil {
|
w.closed = true
|
||||||
return err
|
|
||||||
}
|
if err := w.initialize(); w.err == nil {
|
||||||
if err := w.chunkWriter.Close(); err != nil {
|
|
||||||
w.err = err
|
w.err = err
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
w.err = errAlreadyClosed
|
if w.err == nil {
|
||||||
return nil
|
w.err = w.write(true)
|
||||||
|
}
|
||||||
|
if w.err == nil {
|
||||||
|
w.err = w.chunkWriter.Close()
|
||||||
|
}
|
||||||
|
if err := w.CodecWriter.Close(); w.err == nil {
|
||||||
|
w.err = err
|
||||||
|
}
|
||||||
|
|
||||||
|
if w.err == nil {
|
||||||
|
w.err = errAlreadyClosed
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return w.err
|
||||||
}
|
}
|
||||||
+11
-1
@@ -79,6 +79,11 @@ type CodecReader struct {
|
|||||||
buf [2]byte
|
buf [2]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close implements rac.CodecReader.
|
||||||
|
func (r *CodecReader) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Accepts implements rac.CodecReader.
|
// Accepts implements rac.CodecReader.
|
||||||
func (r *CodecReader) Accepts(c rac.Codec) bool {
|
func (r *CodecReader) Accepts(c rac.Codec) bool {
|
||||||
return c == rac.CodecZlib
|
return c == rac.CodecZlib
|
||||||
@@ -163,8 +168,13 @@ type CodecWriter struct {
|
|||||||
stash []byte
|
stash []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close implements rac.CodecWriter.
|
||||||
|
func (w *CodecWriter) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Clone implements rac.CodecWriter.
|
// Clone implements rac.CodecWriter.
|
||||||
func (r *CodecWriter) Clone() rac.CodecWriter {
|
func (w *CodecWriter) Clone() rac.CodecWriter {
|
||||||
return &CodecWriter{}
|
return &CodecWriter{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user