reader

package
v0.11.16 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 15 Imported by: 37 modules, 119 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTimeout = errors.New("Timeout")
View Source
var (
	ErrTooLarge = errors.New("reader: too large only 8MB allowed as per MaxReadSize")
)
View Source
var (
	// although this is more than enough for most cases
	MaxReadSize, _ = units.FromHumanSize("8mb")
)

Functions

func ConnReadN added in v0.0.62

func ConnReadN(ctx context.Context, reader io.Reader, N int64) ([]byte, error)

ConnReadN reads at most N bytes from reader and it optimized for connection based readers like net.Conn it should not be used for file/buffer based reading, ConnReadN should be preferred instead of 'conn.Read() without loop' . It ignores EOF, UnexpectedEOF and timeout errors Note: you are responsible for adding a timeout to context Note: if the reader supports SetReadDeadline (e.g. net.Conn), a cancelled context expires its read deadline to unblock a pending read, leaving the deadline in the past; a connection whose read was cancelled should be discarded rather than reused.

func ConnReadNWithTimeout added in v0.0.62

func ConnReadNWithTimeout(reader io.Reader, N int64, after time.Duration) ([]byte, error)

ConnReadNWithTimeout is same as ConnReadN but it takes timeout instead of context and it returns error if read does not finish in given time

func IsAcceptedError added in v0.0.62

func IsAcceptedError(err error) bool

IsAcceptedError checks if the error is accepted error for example: connection refused, io.EOF, io.ErrUnexpectedEOF while reading from connection

Types

type FrozenReader

type FrozenReader struct{}

FrozenReader is a reader that never returns

func (FrozenReader) Read

func (reader FrozenReader) Read(p []byte) (n int, err error)

Read into the buffer

type KeyPressReader added in v0.0.17

type KeyPressReader struct {
	Timeout time.Duration

	Once       *sync.Once
	Raw        bool
	BufferSize int
	// contains filtered or unexported fields
}

func (KeyPressReader) Read added in v0.0.17

func (reader KeyPressReader) Read(p []byte) (n int, err error)

Read into the buffer

func (*KeyPressReader) Start added in v0.0.17

func (reader *KeyPressReader) Start() error

func (*KeyPressReader) Stop added in v0.0.17

func (reader *KeyPressReader) Stop() error

type ReusableReadCloser

type ReusableReadCloser struct {
	*sync.RWMutex
	io.Reader
	// contains filtered or unexported fields
}

ReusableReadCloser is a reusable reader with no-op close

func NewReusableReadCloser

func NewReusableReadCloser(raw interface{}) (*ReusableReadCloser, error)

NewReusableReadCloser is returned for any type of input

func (ReusableReadCloser) Close

func (r ReusableReadCloser) Close() error

Close is a no-op close of ReusableReadCloser

func (ReusableReadCloser) Read

func (r ReusableReadCloser) Read(p []byte) (int, error)

Read []byte from Reader

type TimeoutReader

type TimeoutReader struct {
	Timeout time.Duration
	Reader  io.Reader
}

TimeoutReader is a reader wrapper that stops waiting after Timeout.

A Timeout of zero or less disables the timeout: Read calls Reader directly.

io.Reader has no way to cancel a Read that is blocked, so a timeout only stops the caller from waiting. The underlying Read keeps running on a helper goroutine until Reader returns, so a Reader that never returns leaks that goroutine. On timeout, Read returns 0 and ErrTimeout and never writes to the caller's buffer after it returns.

Bytes (and the error) that Reader delivers after a timeout are discarded, not kept for the next Read, so the data stream has a gap. A Read after a timeout starts a new Read on Reader while the abandoned one may still be pending, so Reader must tolerate overlapping Reads. Treat ErrTimeout as terminal unless the data in flight does not matter.

Every Read with a positive Timeout allocates a scratch buffer as long as the caller's, because Reader must not be handed the caller's buffer (see above).

func (TimeoutReader) Read

func (reader TimeoutReader) Read(buf []byte) (int, error)

Read into the buffer

Directories

Path Synopsis
examples
keypress/raw command
Package rawmode reads and changes the terminal mode of a file, including switching it to raw mode.
Package rawmode reads and changes the terminal mode of a file, including switching it to raw mode.

Jump to

Keyboard shortcuts

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