ctxio

package module
v0.0.0-...-0159b27 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2022 License: MIT Imports: 9 Imported by: 0

README

ctxio

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(ctx context.Context, dst Writer, src Reader) (written int64, err error)

Copy copies from src to dst until either EOF is reached on src or an error occurs. It returns the number of bytes copied and the first error encountered while copying, if any.

A successful Copy returns err == nil, not err == EOF. Because Copy is defined to read from src until EOF, it does not treat an EOF from Read as an error to be reported.

func CopyBuffer

func CopyBuffer(ctx context.Context, dst Writer, src Reader, buf []byte) (written int64, err error)

CopyBuffer is identical to Copy except that it stages through the provided buffer (if one is required) rather than allocating a temporary one. If buf is nil, one is allocated; otherwise if it has zero length, CopyBuffer panics.

func Pipe

func Pipe() (*PipeReader, *PipeWriter)

func ReadAll

func ReadAll(ctx context.Context, r Reader) ([]byte, error)

ReadAll reads from r until an error or io.EOF and returns the data it read. A successful call returns err == nil, not err == io.EOF. Because ReadAll is defined to read from src until io.EOF, it does not treat an io.EOF from Read as an error to be reported.

func ReadAtLeast

func ReadAtLeast(ctx context.Context, r Reader, buf []byte, min int) (n int, err error)

ReadAtLeast reads from r into buf until it has read at least min bytes.

func ReadFull

func ReadFull(ctx context.Context, r Reader, buf []byte) (n int, err error)

ReadFull reads exactly len(buf) bytes from r into buf.

func WriteStringContext

func WriteStringContext(ctx context.Context, w Writer, s string) (n int, err error)

Types

type PipeReader

type PipeReader struct {
	// contains filtered or unexported fields
}

func (*PipeReader) Close

func (r *PipeReader) Close() error

Close closes the reader; subsequent writes to the write half of the pipe will return the error ErrClosedPipe.

func (*PipeReader) CloseWithError

func (r *PipeReader) CloseWithError(err error) error

CloseWithError closes the reader; subsequent writes to the write half of the pipe will return the error err.

CloseWithError never overwrites the previous error if it exists and always returns nil.

func (*PipeReader) ReadContext

func (r *PipeReader) ReadContext(ctx context.Context, data []byte) (n int, err error)

type PipeWriter

type PipeWriter struct {
	// contains filtered or unexported fields
}

A PipeWriter is the write half of a pipe.

func (*PipeWriter) Close

func (w *PipeWriter) Close() error

Close closes the writer; subsequent reads from the read half of the pipe will return no bytes and EOF.

func (*PipeWriter) CloseWithError

func (w *PipeWriter) CloseWithError(err error) error

CloseWithError closes the writer; subsequent reads from the read half of the pipe will return no bytes and the error err, or EOF if err is nil.

CloseWithError never overwrites the previous error if it exists and always returns nil.

func (*PipeWriter) WriteContext

func (w *PipeWriter) WriteContext(ctx context.Context, data []byte) (n int, err error)

type ReadCloser

type ReadCloser interface {
	Reader
	io.Closer
}

func NewReader

func NewReader(reader io.Reader) ReadCloser

func NopCloser

func NopCloser(r Reader) ReadCloser

NopCloser returns a ReadCloser with a no-op Close method wrapping the provided Reader r.

type Reader

type Reader interface {
	ReadContext(ctx context.Context, data []byte) (n int, err error)
}

type ReaderFrom

type ReaderFrom interface {
	ReadFromContext(ctx context.Context, r Reader) (n int64, err error)
}

ReaderFrom is the interface that wraps the ReadFromContext method.

ReadFromContext reads data from r until EOF or error. The return value n is the number of bytes read. Any error except EOF encountered during the read is also returned.

The Copy function uses ReaderFrom if available.

type StringWriter

type StringWriter interface {
	WriteStringContext(ctx context.Context, s string) (n int, err error)
}

StringWriter is the interface that wraps the WriteStringContext method.

type WriteCloser

type WriteCloser interface {
	Writer
	io.Closer
}

func NewWriter

func NewWriter(writer io.Writer) WriteCloser

type Writer

type Writer interface {
	WriteContext(ctx context.Context, data []byte) (n int, err error)
}
var Discard Writer = discard{}

Discard is a Writer on which all Write calls succeed without doing anything.

type WriterTo

type WriterTo interface {
	WriteToContext(ctx context.Context, w Writer) (n int64, err error)
}

WriterTo is the interface that wraps the WriteToContext method.

WriteToContext writes data to w until there's no more data to write or when an error occurs. The return value n is the number of bytes written. Any error encountered during the write is also returned.

The Copy function uses WriterTo if available.

Jump to

Keyboard shortcuts

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