iohelper

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2022 License: Apache-2.0 Imports: 15 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDeadlineExceeded error

ErrDeadlineExceeded is the export of internal/poll.ErrDeadlineExceeded which is the same as os.ErrDeadlineExceeded in go1.15 and onwards

Functions

func CheckBytesToRead added in v0.3.3

func CheckBytesToRead(fd uintptr) (int, error)

CheckBytesToRead calls ioctl(fd, FIONREAD) to check ready data size of fd

func CopyDir

func CopyDir(src string, dst string) (err error)

CopyDir recursively copies a directory tree, attempting to preserve permissions. Source directory must exist, destination directory must *not* exist. Symlinks are ignored and skipped.

func CopyFile

func CopyFile(src, dst string) (err error)

CopyFile copies the contents of the file named src to the file named by dst. The file will be created if it does not already exist. If the destination file exists, all it's contents will be replaced by the contents of the source file. The file mode will be copied from the source and the copied data is synced/flushed to stable storage.

func CreateSeekRestore added in v0.12.0

func CreateSeekRestore(s io.Seeker) (func() (int64, error), error)

CreateSeekRestore creates a function to seek back to the current position.

func IsDeadlineExceeded added in v0.5.0

func IsDeadlineExceeded(err error) bool

func Pipe

func Pipe() (io.ReadCloser, io.WriteCloser)

Pipe returns io.Pipe if os.Pipe returned error

func ReadInputLine added in v0.8.1

func ReadInputLine(r io.Reader, buf []byte) ([]byte, error)

ReadInputLine reads and returns an input line from the Reader r, it returns io.ErrShortBuffer when there is more data to read but the provided buffer buf already filled up.

based on golang.org/x/term/terminal.go#readPasswordLine

func TempFilename added in v0.2.0

func TempFilename(dir, pattern string) (string, error)

func WriteFile

func WriteFile(file string, data []byte, perm os.FileMode, overwrite bool) (undo func() error, _ error)

WriteFile write data to target file

Types

type BufferredReaderAt added in v0.12.0

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

BufferredReaderAt

func NewBufferedReaderAt added in v0.12.0

func NewBufferedReaderAt(r io.Reader) *BufferredReaderAt

func (*BufferredReaderAt) ReadAt added in v0.12.0

func (s *BufferredReaderAt) ReadAt(p []byte, off int64) (n int, err error)

ReadAt implements io.ReaderAt.

func (*BufferredReaderAt) Size added in v0.12.0

func (s *BufferredReaderAt) Size() int64

Size implements SizedReaderAt.

type ChunkedWriter added in v0.12.0

type ChunkedWriter struct {
	ChunkSize int

	BeforeEachChunkWriting func() error
	AfterEachChunkWrote    func() error

	Underlay io.Writer
	// contains filtered or unexported fields
}

ChunkedWriter

func NewChunkedWriter added in v0.12.0

func NewChunkedWriter(
	chunkSize int,
	underlay io.Writer,
	doBeforeEachChunkWriting func() error,
	doAfterEachChunkWrote func() error,
) *ChunkedWriter

NewChunkedWriter returns a new ChunkedWriter.

func (*ChunkedWriter) Remainder added in v0.12.0

func (cw *ChunkedWriter) Remainder() int

Remainder

func (*ChunkedWriter) Write added in v0.12.0

func (cw *ChunkedWriter) Write(p []byte) (wrote int, err error)

type CustomReadCloser added in v0.8.0

type CustomReadCloser struct {
	io.Reader
	CloseFunc func() error
}

CustomReadCloser

func NewCustomReadCloser added in v0.12.0

func NewCustomReadCloser(r io.Reader, fClose func() error) *CustomReadCloser

NewCustomReadCloser returns a new CustomReadCloser with non-nil CloseFunc

func (*CustomReadCloser) Close added in v0.12.0

func (c *CustomReadCloser) Close() error

Close implements io.Closer.

type CustomWriteCloser added in v0.8.0

type CustomWriteCloser struct {
	io.Writer
	CloseFunc func() error
}

CustomWriteCloser

func NewCustomWriteCloser added in v0.12.0

func NewCustomWriteCloser(w io.Writer, fClose func() error) *CustomWriteCloser

NewCustomWriteCloser returns a new CustomWriteCloser with non-nil CloseFunc.

func (*CustomWriteCloser) Close added in v0.12.0

func (c *CustomWriteCloser) Close() error

Close implements io.Closer.

type DevNull added in v0.9.0

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

func NewDevNull added in v0.9.0

func NewDevNull() DevNull

func (DevNull) Close added in v0.9.0

func (d DevNull) Close() error

func (DevNull) Read added in v0.9.0

func (d DevNull) Read(p []byte) (int, error)

func (DevNull) Write added in v0.9.0

func (d DevNull) Write(p []byte) (int, error)

func (DevNull) WriteString added in v0.9.0

func (d DevNull) WriteString(s string) (int, error)

type FilteredReader added in v0.12.0

type FilteredReader struct {
	Underlay io.Reader

	// FilterFunc called on each p just read, it should update the p and
	// return the new size of p (MUST be less than len(p)).
	FilterFunc func(p []byte) int
}

FilteredReader

func NewFilteredReader added in v0.12.0

func NewFilteredReader(
	underlay io.Reader,
	filter func(p []byte) int,
) FilteredReader

func (FilteredReader) Read added in v0.12.0

func (fr FilteredReader) Read(p []byte) (n int, err error)

Read implements io.Reader.

type LazyAutoCloseReader added in v0.12.0

type LazyAutoCloseReader struct {
	Open func() (io.Reader, error)
	// contains filtered or unexported fields
}

LazyAutoCloseReader open underlay stream on first read call, closes the opened stream on error.

func (*LazyAutoCloseReader) Read added in v0.12.0

func (l *LazyAutoCloseReader) Read(p []byte) (ret int, err error)

type SizedReaderAt added in v0.12.0

type SizedReaderAt interface {
	Size() int64
	io.ReaderAt
}

type StringWriter added in v0.12.0

type StringWriter struct {
	io.Writer
}

StringWriter

func (StringWriter) WriteString added in v0.12.0

func (s StringWriter) WriteString(x string) (int, error)

WriteString implements io.StringWriter.

type TimeoutReader

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

TimeoutReader is a reader with read timeout support

It is designed for those want to read some data from a stream, and the size of the data is unknown, but still want to pipe data to some destination at certain interval

example use case: data streaming for shell interaction over MQTT

	when user input/output is slow, shall we send one character a time for real-time
	interaction? what if the user executed `cat some-large-file`? what if user
	was sending a large chunk of data over stdin?

	for raw tcp connection that's fine if you have configured tcp buffering correctly,
	but for packet oriented connections (in this case MQTT), send one byte per packet
	will significantly increase protocol overhead.

	with TimeoutReader we can read data generated in some interval (say 20ms), no
 real-time experience lost while still keep protocol overhead at a reasonable level

func NewTimeoutReader

func NewTimeoutReader(r io.Reader) *TimeoutReader

NewTimeoutReader creates a new idle timeout reader from a blocking reader

func (*TimeoutReader) Error

func (t *TimeoutReader) Error() error

Error returns the error happened during reading in background

func (*TimeoutReader) FallbackReading added in v0.3.3

func (t *TimeoutReader) FallbackReading(stopSig <-chan struct{})

func (*TimeoutReader) Read added in v0.3.3

func (t *TimeoutReader) Read(maxWait time.Duration, p []byte) (data []byte, shouldCopy bool, err error)

Read performs a read operation with timeout option, will block until maxWait exceeded or p is full

if the function returned because of timeout, the returned error is ErrDeadlineExceeded

func (*TimeoutReader) WaitForData added in v0.3.3

func (t *TimeoutReader) WaitForData(cancel <-chan struct{}) bool

WaitForData is a helper function used to check if there is data available in reader so we can reduce actual call of Read when the timeout is a short duration

when return value is true, you MUST call Read to read data and you can read at least one byte from the underlying reader

otherwise, false means the provided cancel has signaled or error happened, you can no longer read from the underlying reader if Error() returned non-nil value.

Jump to

Keyboard shortcuts

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