io

package
v0.0.0-...-c1b4e3e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2025 License: GPL-3.0, LGPL-3.0 Imports: 5 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIncompleteRead = errors.NewStringError("incomplete read")
View Source
var ErrSequenceFailure = errors.NewStringError("error while executing sequence of closers")

Functions

func CloseIgnored

func CloseIgnored(c io.Closer)

CloseIgnored closes the closer and discards any possible error.

func CloseLogged

func CloseLogged(c io.Closer, message string)

CloseLogged closes the closer and logs specified message in case of error. Any error except is logged. The error message is logged as a warning.

func CloseLoggedWithIgnores

func CloseLoggedWithIgnores(c io.Closer, message string, ignore ...error)

CloseLoggedWithIgnores closes the closer and logs specified message in case of error. Any error except for ignored are logged. The error message is logged as a warning. Typical ignored errors: net.ErrClosed (when closed elsewhere), io.ErrClosedPipe (when closed elsewhere)

func ClosePanicked

func ClosePanicked(c io.Closer, message string)

ClosePanicked closes the closer and panics with specified message in case of any error.

func ClosePanickedWithIgnores

func ClosePanickedWithIgnores(c io.Closer, message string, ignore ...error)

ClosePanickedWithIgnores closes the closer and panics with specified message in case of any error, except for those in `ignore`. Typical ignored errors: net.ErrClosed (when closed elsewhere), io.ErrClosedPipe (when closed elsewhere)

func CopyNoWarning

func CopyNoWarning(out io.Writer, in io.Reader) int64

CopyNoWarning copies contents from in to out. Do NOT log anything in case transfer interrupts.

func CopyWithWarning

func CopyWithWarning(out io.Writer, in io.Reader) int64

CopyWithWarning copies contents from in to out. It logs a warning in case transfer interrupts.

func Discard

func Discard(in io.Reader) (int64, error)

Discard reads remaining data from reader and discards it.

Returns nr of bytes read, thus discarded. An error is returned if encountered while reading from input.

func DiscardN

func DiscardN(in io.Reader, n int64) (int64, error)

DiscardN reads `n` bytes from `in` and discards them.

The number of discarded bytes is returned, and an error is returned only if it is not successful in reading and discarding `n` bytes.

func MustReadAll

func MustReadAll(in io.Reader) []byte

MustReadAll reads all data from reader and panics in case an error occurs.

func MustReadByte

func MustReadByte(in io.Reader) byte

func MustReadBytes

func MustReadBytes(in io.Reader, dst []byte)

MustReadBytes reads bytes into dst and fails if anything out of the ordinary happens.

func MustReadFull

func MustReadFull(in io.Reader, dst []byte)

func MustReadN

func MustReadN(in io.Reader, n uint) []byte

MustReadN creates then fills the buffer to `n` by reading from `in` and panics on error.

func NewCloseSequence

func NewCloseSequence(seq ...io.Closer) io.Closer

NewCloseSequence creates a new composite sequential closer that closes in the order provided.

The sequence will not halt on error. If closing behavior is dependent on other closers, this should be part of the closer's logic. Instead, errors are collected and an aggregate error is returned that includes error messages from all the failures that occurred while closing the sequence.

Returns `ErrSequenceFailure` with context in case at least one of the closers fails to close.

Panics are not mitigated in any way.

func NewCloserWrapper

func NewCloserWrapper(closer io.Closer) *closerWrapper

func ReadAll

func ReadAll(in io.Reader) ([]byte, error)

func ReadByte

func ReadByte(in io.Reader) (byte, error)

func ReadExpect

func ReadExpect(next byte, in io.Reader) (bool, error)

ReadExpect reads and checks if the read byte is the expected `next` byte.

func ReadN

func ReadN(in io.Reader, n uint) ([]byte, error)

ReadN creates then fills the buffer to `n` by reading from `in`.

func ReadUntil

func ReadUntil(in io.Reader, stop byte) ([]byte, error)

ReadUntil reads until a specific byte is encountered.

All bytes read before the stop-byte are returned. If an error is encountered, everything read until the error is returned together with the error. Following the behavior of `io.ReadFull`, ReadUntil will return io.EOF if end-of-file is reached.

For more sophisticated and more capable functions, use `bufio` (buffered-io). These functions are provided for one-off cases and cases where reading ahead is not desirable or not allowed.

func Transfer

func Transfer(wg *sync.WaitGroup, dst io.Writer, src io.Reader)

Transfer may be called in a goroutine. It copies all content from one connection to the next. Errors are ignored. In case copying is interrupted, for whatever reason, the function finishes up and releases `wg`.

Types

type CountingWriter

type CountingWriter struct {

	// FIXME consider better name for field; something that is descriptive when used.
	Cum int64
	// contains filtered or unexported fields
}

TODO make `Cum` (cumulative) public or private with accessor function?

func NewCountingWriter

func NewCountingWriter(out io.Writer) CountingWriter

func (*CountingWriter) Write

func (w *CountingWriter) Write(p []byte) (int, error)

Write writes `p` to the underlying output and counts the number of bytes written. Write follows the conventions of io.Writer. It will add up the number of bytes written, even if the write was incomplete and consequently an error returned.

type NopCloser

type NopCloser struct {
	Rw io.ReadWriter
}

NopCloser is a no-op close ReadWriteCloser implementation.

One would typically want to respect the Close method on writers. This implementation provides no-op closing for cases where `Close()` is already handled at a different level of nesting.

func (*NopCloser) Close

func (n *NopCloser) Close() error

Close is a no-op.

func (*NopCloser) Read

func (n *NopCloser) Read(p []byte) (int, error)

func (*NopCloser) Write

func (n *NopCloser) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL