io

package
v0.0.0-...-a89fe16 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrLimitExceeded = errors.New("io: read limit exceeded")

ErrLimitExceeded identifies reads that exceed a configured byte limit.

Functions

func Copy

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

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

Note: This implementation wraps the reader with a context check, which means it won't benefit from WriterTo optimization in io.Copy if the source implements it. This is a trade-off for being able to cancel the operation on context cancellation.

func MaxBytesReader

func MaxBytesReader(r io.Reader, n int64) io.Reader

MaxBytesReader returns a reader that exposes at most n bytes from r. It returns a MaxBytesError when a read proves that r contains more than n bytes. Detecting overflow consumes one byte beyond the limit from r. A negative limit is treated as zero.

func ReadAllWithLimit

func ReadAllWithLimit(r io.Reader, n int64) ([]byte, error)

ReadAllWithLimit reads from r until EOF or until the read exceeds n bytes. On overflow, it returns the first n bytes and a MaxBytesError. A negative limit is treated as zero.

Types

type CountingReader

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

CountingReader wraps an io.Reader and counts the number of bytes read. Note: This implementation is NOT thread-safe. It should not be used concurrently from multiple goroutines.

func NewCountingReader

func NewCountingReader(r io.Reader) *CountingReader

NewCountingReader creates a new CountingReader that wraps the provided io.Reader.

func (*CountingReader) BytesRead

func (c *CountingReader) BytesRead() int64

BytesRead returns the number of bytes read.

func (*CountingReader) Read

func (c *CountingReader) Read(p []byte) (int, error)

Read reads data from the underlying reader and counts the number of bytes read.

type MaxBytesError

type MaxBytesError struct {
	Limit int64
}

MaxBytesError is returned by MaxBytesReader when its read limit is exceeded.

func (*MaxBytesError) Error

func (e *MaxBytesError) Error() string

func (*MaxBytesError) Is

func (*MaxBytesError) Is(target error) bool

type ReadSeekCloserAt

type ReadSeekCloserAt interface {
	io.ReadSeekCloser
	io.ReaderAt
}

func NopCloser

func NopCloser(r ReadSeekerAt) ReadSeekCloserAt

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

type ReadSeekerAt

type ReadSeekerAt interface {
	io.ReadSeeker
	io.ReaderAt
}

func NewReadSeekerAt

func NewReadSeekerAt(r io.Reader) (ReadSeekerAt, error)

func NewReadSeekerAtWithSize

func NewReadSeekerAtWithSize(r io.Reader) (ReadSeekerAt, int64, error)

Jump to

Keyboard shortcuts

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