github.com/fluhus/gostuff@v0.4.1-0.20240331134726-be71864f2b5d/aio/wrap.go (about)

     1  package aio
     2  
     3  import (
     4  	"bufio"
     5  	"io"
     6  )
     7  
     8  // Wraps a writer and its underlying closer.
     9  type writerWrapper struct {
    10  	top, bottom io.WriteCloser
    11  }
    12  
    13  func (w *writerWrapper) Write(p []byte) (int, error) {
    14  	return w.top.Write(p)
    15  }
    16  
    17  func (w *writerWrapper) Close() error {
    18  	if err := w.top.Close(); err != nil {
    19  		return err
    20  	}
    21  	return w.bottom.Close()
    22  }
    23  
    24  type Reader struct {
    25  	bufio.Reader
    26  	r io.ReadCloser
    27  }
    28  
    29  func (r *Reader) Close() error {
    30  	return r.r.Close()
    31  }
    32  
    33  type Writer struct {
    34  	bufio.Writer
    35  	w io.WriteCloser
    36  }
    37  
    38  func (w *Writer) Close() error {
    39  	if err := w.Flush(); err != nil {
    40  		return err
    41  	}
    42  	return w.w.Close()
    43  }