iohlp

package
v0.20.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2022 License: Apache-2.0 Imports: 13 Imported by: 11

Documentation

Overview

Package iohlp contains small io-related utility functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConcurrentReader

func ConcurrentReader(r io.Reader) io.Reader

ConcurrentReader wraps the given io.Reader such that it can be called concurrently.

func Fcntl

func Fcntl(fd uintptr, cmd, flags int) error

Fcntl calls the fcntl command with the flags.

func FindReader added in v0.18.2

func FindReader(r io.Reader, needle []byte) (int, error)

FindReader finds the first occurrence of needle in the io.Reader and gives back its position. Returns -1 when needle is not found.

Uses the default buffer size (64kB).

func FindReaderSize added in v0.18.2

func FindReaderSize(r io.Reader, needle []byte, bufSize int) (int, error)

FindReaderSize finds the first occurrence of needle in the io.Reader and gives back its position. Returns -1 when needle is not found.

Uses the specified amount of buffer (must be longer than needle!).

func MakeSectionReader

func MakeSectionReader(r io.Reader, threshold int) (*io.SectionReader, error)

MakeSectionReader reads the reader and returns the byte slice.

If the read length is below the threshold, then the bytes are read into memory; otherwise, a temp file is created, and mmap-ed.

func MmapFile

func MmapFile(fn string) (io.ReaderAt, error)

MmapFile returns the mmap of the given path.

func NewMultiCloser

func NewMultiCloser(c ...io.Closer) *multiCloser

NewMultiCloser returns an io.Closer which will close all contained io.Closer, in the given order.

func NewStreamReplacer

func NewStreamReplacer(r io.Reader, patternReplacementPairs ...[]byte) io.Reader

NewStreamReplacer returns an io.Reader in which all non-overlapping patterns are replaced to their replacement pairs (such as a strings.Replacer on strings).

func RedirFd

func RedirFd(fd int) (*os.File, func() error, error)

RedirFd redirects the fd, returns a reader pipe for the written data, and a cleanup function that reverses the effect of this function (closes the writer and redirects the fd).

func SetDirect

func SetDirect(f *os.File) error

SetDirect sets the O_DIRECT flag on the os.File.

func URLEncode

func URLEncode(w io.Writer, keyvals ...NamedReader) error

URLEncode encodes the Name:Reader pairs just as url.Values.Encode does.

func Walk

func Walk(root string, walkFn filepath.WalkFunc) error

Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. All errors that arise visiting files and directories are filtered by walkFn. The files are walked UNORDERED, which makes the output undeterministic! Walk does not follow symbolic links.

func WalkWithSymlinks(root string, walkFn filepath.WalkFunc) error

WalkWithSymlinks walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. All errors that arise visiting files and directories are filtered by walkFn. The files are walked UNORDERED, which makes the output undeterministic! WalkWithSymlinks does follow symbolic links!

func WrappingReader

func WrappingReader(r io.Reader, width uint) io.Reader

WrappingReader returns an io.Reader which will wrap lines longer than the given width. All other lines (LF chars) will be preserved.

Types

type BytesReplacer

type BytesReplacer [][2][]byte

BytesReplacer is a Replacer for bytes.

func NewBytesReplacer

func NewBytesReplacer(patternReplacementPairs ...[]byte) BytesReplacer

NewBytesReplacer returns a Replacer, such as strings.Replacer, but for []byte.

func (BytesReplacer) MaxPatternLen

func (br BytesReplacer) MaxPatternLen() int

func (BytesReplacer) Replace

func (br BytesReplacer) Replace(p []byte) []byte

Replace as strings.Replacer would do.

type CloserFunc

type CloserFunc func() error

CloserFunc implements the io.Closer.

func (CloserFunc) Close

func (cf CloserFunc) Close() error

Close calls the function.

type ErrWriter

type ErrWriter struct {
	io.Writer
	// contains filtered or unexported fields
}

ErrWriter is a writer with a "stuck-in" error policy: writes normally, until the underlying io.Writer returns error; then after it always returns that error.

func (*ErrWriter) Err

func (w *ErrWriter) Err() error

Err returns the first error the underlying io.Writer returned.

func (*ErrWriter) Write

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

type NamedReader

type NamedReader struct {
	io.Reader
	Name string
}

type ReaderAt

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

ReaderAt reads a memory-mapped file.

Like any io.ReaderAt, clients can execute parallel ReadAt calls, but it is not safe to call Close and reading methods concurrently.

Copied from https://github.com/golang/exp/blob/85be41e4509f/mmap/mmap_unix.go#L115

func Mmap

func Mmap(f *os.File) (*ReaderAt, error)

Mmap the file for read, return the bytes and the error. Will read the data directly if Mmap fails.

func (*ReaderAt) At

func (r *ReaderAt) At(i int) byte

At returns the byte at index i.

func (*ReaderAt) Close

func (r *ReaderAt) Close() error

Close closes the reader.

func (*ReaderAt) Len

func (r *ReaderAt) Len() int

Len returns the length of the underlying memory-mapped file.

func (*ReaderAt) ReadAt

func (r *ReaderAt) ReadAt(p []byte, off int64) (int, error)

ReadAt implements the io.ReaderAt interface.

Jump to

Keyboard shortcuts

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