Documentation ¶
Overview ¶
EofReader is an empty reader returning EOF. Thread-safe
LineReader reads a stream one line per Read invocation.
LineReader reads a stream one line per Read invocation.
ReadWriteCloserSlice is a read-writer with a slice as intermediate storage. thread-safe.
TeeWriter is a writer that copies its writes to one or more other writers.
Index ¶
- Variables
- func CopyContext(dst io.Writer, src io.Reader, buf []byte, ctx context.Context) (written int64, err error)
- func CopyThread(label string, reader io.Reader, writer io.Writer, errCh chan<- error, ...)
- func InitWriteCloserToChan(wcp *WriteCloserToChan)
- func NewReadCloserCallbacker(closeCallback func(err error) (e error), readCloser io.ReadCloser) (readCloserCallbacker io.ReadCloser)
- func NewTeeWriter(closeCallback func() (err error), writers ...io.Writer) (teeWriter io.WriteCloser)
- func NewWriteCloserCallbacker(closeCallback func(err error) (e error), writeCloser io.WriteCloser) (writeCloserCallbacker io.WriteCloser)
- func NewWriteCloserToChan() (writeCloser io.WriteCloser)
- func NewWriteCloserToChanLine(withNewline ...bool) (writeCloser io.WriteCloser)
- func NewWriteCloserToString() io.WriteCloser
- type CloserCallbacker
- type ContextCloser
- type ContextCopier
- type ContextReader
- type ContextWriter
- type LineReader
- type LineWriter
- type ReadCloserCallbacker
- type ReadWriteCloserSlice
- type TeeWriter
- type WriteCloserCallbacker
- type WriteCloserToChan
- type WriteCloserToChanLine
- type WriteCloserToString
Constants ¶
This section is empty.
Variables ¶
var EofReader io.Reader = &eofReader{}
EofReader is an empty reader returning EOF. Thread-safe
var ErrCopyShutdown = errors.New("Copy received Shutdown")
var ErrFileAlreadyClosed = errors.New("file alread closed")
On write after close, ErrFileAlreadyClosed is returned.
if errors.Is(err, pio.ErrFileAlreadyClosed)…
var ErrInvalidWrite = errors.New("invalid write result")
errInvalidWrite means that a write returned an impossible count
- cause is buggy io.Writer implementation
Functions ¶
func CopyContext ¶ added in v0.4.108
func CopyContext(dst io.Writer, src io.Reader, buf []byte, ctx context.Context) (written int64, err error)
CopyContext is like io.Copy but is cancelable via context
- CopyContext closes both reader and writer if their runtime type is closable
- context cancel is on any Read or Write invocation and by parallel close if either reader or writer is closable
func CopyThread ¶ added in v0.4.95
func CopyThread( label string, reader io.Reader, writer io.Writer, errCh chan<- error, ctx context.Context, )
CopyThread copies from an io.Reader to an io.Writer.
- label is used for thread identification on panics
- errCh receives result and makes thread awaitable
- if ctx, a CancelContext, is non-nil and error occurs, ctx is cancelled
- CopyThread itself never fails
func InitWriteCloserToChan ¶
func InitWriteCloserToChan(wcp *WriteCloserToChan)
func NewReadCloserCallbacker ¶ added in v0.4.38
func NewReadCloserCallbacker(closeCallback func(err error) (e error), readCloser io.ReadCloser) (readCloserCallbacker io.ReadCloser)
func NewTeeWriter ¶ added in v0.4.38
func NewTeeWriter(closeCallback func() (err error), writers ...io.Writer) (teeWriter io.WriteCloser)
TeeWriter is a writer that copies its writes to one or more other writers.
func NewWriteCloserCallbacker ¶ added in v0.4.38
func NewWriteCloserCallbacker(closeCallback func(err error) (e error), writeCloser io.WriteCloser) (writeCloserCallbacker io.WriteCloser)
func NewWriteCloserToChan ¶
func NewWriteCloserToChan() (writeCloser io.WriteCloser)
func NewWriteCloserToChanLine ¶
func NewWriteCloserToChanLine(withNewline ...bool) (writeCloser io.WriteCloser)
func NewWriteCloserToString ¶
func NewWriteCloserToString() io.WriteCloser
NewWriteCloserToString returns an io.WriteCloser that aggregates its oputput in a string. Thread-safe.
Types ¶
type CloserCallbacker ¶ added in v0.4.38
type CloserCallbacker struct {
// contains filtered or unexported fields
}
CloserCallbacker implements a close callback for io.Closer
func (*CloserCallbacker) Close ¶ added in v0.4.38
func (c *CloserCallbacker) Close(closer io.Closer) (err error)
func (*CloserCallbacker) IsClosed ¶ added in v0.4.38
func (cc *CloserCallbacker) IsClosed() (isClosed bool)
func (*CloserCallbacker) Wait ¶ added in v0.4.38
func (cc *CloserCallbacker) Wait()
type ContextCloser ¶ added in v0.4.108
type ContextCloser struct {
// contains filtered or unexported fields
}
ContextCloser is an idempotent io.Closer
- implements io.Closer
func NewContextCloser ¶ added in v0.4.108
func NewContextCloser(closer io.Closer) (contextCloser *ContextCloser)
NewContextCloser returns a an idempotent io.Closer
- closer may be nil
- panic-free idempotent observable
func (*ContextCloser) Close ¶ added in v0.4.108
func (c *ContextCloser) Close() (err error)
Close closes the io.Closer
- if Close has already been invoked, noop, no error
- if io.Closer is nil, noop, no error
- panic-free idempotent
func (*ContextCloser) IsCloseable ¶ added in v0.4.108
func (c *ContextCloser) IsCloseable() (isCloseable bool)
IsCloseable indicates whether an io.Closer is present that can be closed
type ContextCopier ¶ added in v0.4.108
type ContextCopier struct {
// contains filtered or unexported fields
}
ContextCopier is an io.Copy cancelable via context
func NewContextCopier ¶ added in v0.4.108
func NewContextCopier(buf ...[]byte) (copier *ContextCopier)
NewContextCopier copies src to dst aborting if context is canceled
- buf is buffer that can be used
- if reader implements WriteTo or writer implements ReadFrom, no buffer is required
- if a buffer is reqiired ans missing, 1 MiB is allocated
- Copy methods does copying
- Shutdown method or context cancel aborts Copy in progress
- if the runtime type of reader or writer is io.Closable, a thread is active during copying
func (*ContextCopier) Copy ¶ added in v0.4.134
func (c *ContextCopier) Copy( dst io.Writer, src io.Reader, ctx context.Context, ) (n int64, err error)
Copy copies from src to dst until end of data, error, Shutdown or context cancel
- Shutdown method or context cancel aborts copy in progress
- on context cancel, error returned is context.Canceled
- on Shutdown, error returned has ErrCopyShutdown
- if the runtime type of dst or src is io.Closable, a thread is active during copying
- such reader or writer will be closed
func (*ContextCopier) Shutdown ¶ added in v0.4.134
func (c *ContextCopier) Shutdown()
Shutdown order the thread to exit and wait for its result
- every Copy invocation will have a Shutdown either by consumer or the deferred copyEnd method
type ContextReader ¶ added in v0.4.108
type ContextReader struct { // idempotent pannic-free closer if reader implemented [io.Closer] // - Close() IsClosable() ContextCloser // contains filtered or unexported fields }
ContextReader is an io.ReadCloser that aborts on context cancel
- on context cancel, Read returns error context.Canceled
- If the runtime type of reader implements io.Close, it ContextReader can close it
func NewContextReader ¶ added in v0.4.108
func NewContextReader(reader io.Reader, ctx context.Context) (contextReader *ContextReader)
NewContextReader returns an io.ReadCloser that aborts on context cancel
- on context cancel, Read returns error context.Canceled
- If the runtime type of reader implements io.Close, it can be closed
func (*ContextReader) Read ¶ added in v0.4.108
func (c *ContextReader) Read(p []byte) (n int, err error)
Read is like io.Reader.Read but cancels if the context is canceled
- on context cancel, the error returned is context.Canceled
type ContextWriter ¶ added in v0.4.108
type ContextWriter struct { // idempotent pannic-free closer if reader implemented [io.Closer] // - Close() IsClosable() ContextCloser // contains filtered or unexported fields }
ContextWriter is an io.WriteCloser that aborts on context cancel
- on context cancel, Write returns error context.Canceled
- If the runtime type of writer implements io.Close, it ContextWriter can close it
func NewContextWriter ¶ added in v0.4.108
func NewContextWriter(writer io.Writer, ctx context.Context) (contextWriter *ContextWriter)
NewContextWriter returns an io.WriteCloser that aborts on context cancel
- on context cancel, Write returns error context.Canceled
- If the runtime type of reader implements io.Close, it can be closed
func (*ContextWriter) Write ¶ added in v0.4.108
func (c *ContextWriter) Write(p []byte) (n int, err error)
Write is like io.Writer.Write but cancels if the context is canceled
- on context cancel, the error returned is context.Canceled
type LineReader ¶ added in v0.4.38
type LineReader struct {
// contains filtered or unexported fields
}
LineReader reads a stream one line per Read invocation.
func NewLineReader ¶ added in v0.4.38
func NewLineReader(reader io.Reader) (lineReader *LineReader)
func (*LineReader) Read ¶ added in v0.4.38
func (rr *LineReader) Read(p []byte) (n int, err error)
Read returns a byte-sequence ending with newline if size of p is sufficient.
- if size of p is too short, the text will not end with newline
- if EOF without newline, text has no newline and err is io.EOF
func (*LineReader) ReadLine ¶ added in v0.4.38
func (rr *LineReader) ReadLine(p []byte) (line []byte, isEOF bool, err error)
ReadLine returns full lines, extending p as necessary
- len(line) is number of bytes
- max line length 1 MiB
- line will end with newLine unless 1 MiB or isEOF
- EOF is returned as isEOF true
type LineWriter ¶ added in v0.4.38
type LineWriter struct {
// contains filtered or unexported fields
}
func NewLineWriter ¶ added in v0.4.38
func NewLineWriter(writeCloser io.WriteCloser, filter func(line *[]byte, isLastLine bool) (skipLine bool, err error)) (lineWriter *LineWriter)
type ReadCloserCallbacker ¶ added in v0.4.38
type ReadCloserCallbacker struct { io.ReadCloser CloserCallbacker }
func (*ReadCloserCallbacker) Close ¶ added in v0.4.38
func (cc *ReadCloserCallbacker) Close() (err error)
type ReadWriteCloserSlice ¶
type ReadWriteCloserSlice struct {
// contains filtered or unexported fields
}
ReadWriteCloserSlice is a read-writer with a slice as intermediate storage. thread-safe.
- Close closes the writer side indicating no further data will be added
- Write and Close may return error that can be checked: errors.Is(err, pio.ErrFileAlreadyClosed)
- read will eventually return io.EOF after a Close
- there are no other errors
func NewReadWriteCloserSlice ¶
func NewReadWriteCloserSlice() (readWriteCloser *ReadWriteCloserSlice)
func (*ReadWriteCloserSlice) Buffer ¶ added in v0.4.95
func (r *ReadWriteCloserSlice) Buffer() (buffer []byte)
func (*ReadWriteCloserSlice) Close ¶
func (r *ReadWriteCloserSlice) Close() (err error)
Close closes thw Write part, may return ErrFileAlreadyClosed
type TeeWriter ¶ added in v0.4.38
type TeeWriter struct {
// contains filtered or unexported fields
}
TeeWriter is a writer that copies its writes to one or more other writers.
type WriteCloserCallbacker ¶ added in v0.4.38
type WriteCloserCallbacker struct { io.WriteCloser CloserCallbacker }
func (*WriteCloserCallbacker) Close ¶ added in v0.4.38
func (cc *WriteCloserCallbacker) Close() (err error)
type WriteCloserToChan ¶
type WriteCloserToChan struct {
// contains filtered or unexported fields
}
func (*WriteCloserToChan) Ch ¶
func (wc *WriteCloserToChan) Ch() (readCh <-chan []byte)
func (*WriteCloserToChan) Close ¶
func (wc *WriteCloserToChan) Close() (err error)
type WriteCloserToChanLine ¶
type WriteCloserToChanLine struct {
// contains filtered or unexported fields
}
func (*WriteCloserToChanLine) Ch ¶
func (wc *WriteCloserToChanLine) Ch() (readCh <-chan string)
func (*WriteCloserToChanLine) Close ¶
func (wc *WriteCloserToChanLine) Close() (err error)
type WriteCloserToString ¶
type WriteCloserToString struct {
// contains filtered or unexported fields
}
WriteCloserToString is an io.WriteCloser that aggregates its oputput in a string. Thread-safe.
- the string is available using the Data method.
func (*WriteCloserToString) Close ¶
func (wc *WriteCloserToString) Close() (err error)
Close should only be invoked once. Close is not required for releasing resources.
func (*WriteCloserToString) Data ¶
func (wc *WriteCloserToString) Data() (s string)
Data returns current string data