iohelp

package
v0.0.0-...-892de5e Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package iohelp provides utilities for doing io

Index

Constants

This section is empty.

Variables

View Source
var ErrThroughput = errors.New("throughput below minimum allowable")

ErrThroughput is the error that is returned by ReadWithMinThroughput if the throughput drops below the threshold

Functions

func NopWrCloser

func NopWrCloser(wr io.Writer) io.WriteCloser

NopWrCloser returns a WriteCloser with a no-op Close method wrapping the provided Writer wr.

func ReadLine

func ReadLine(br *bufio.Reader) (line string, done bool, err error)

ReadLine will read a line from an unbuffered io.Reader where it considers lines to be separated by newlines (\n). The data returned will be a string with \r\n characters removed from the end, a bool which says whether the end of the stream has been reached, and any errors that have been encountered (other than eof which is treated as the end of the final line)

func ReadLineNoBuf

func ReadLineNoBuf(r io.Reader) (string, bool, error)

ReadLineNoBuf will read a line from an unbuffered io.Reader where it considers lines to be separated by newlines (\n). The data returned will be a string with \r\n characters removed from the end, a bool which says whether the end of the stream has been reached, and any errors that have been encountered (other than eof which is treated as the end of the final line). This isn't efficient, so you shouldn't do this if you can use a buffered reader and the iohelp.ReadLine method.

func ReadNBytes

func ReadNBytes(r io.Reader, n int) ([]byte, error)

ReadNBytes will read n bytes from the given reader and return a new slice containing the data. ReadNBytes will always return n bytes, or it will return no data and an error (So if you request 100 bytes and there are only 99 left before the reader returns io.EOF you won't receive any of the data as this is considered an error as it can't read 100 bytes).

func ReadNWithProgress

func ReadNWithProgress(r io.Reader, n int64, bytesRead *int64) ([]byte, error)

ReadNWithProgress reads n bytes from reader r. As it reads it atomically updates the value pointed at by bytesRead. In order to cancel this read the reader should be closed.

func ReadWithMinThroughput

func ReadWithMinThroughput(r io.ReadCloser, n int64, mtcParams MinThroughputCheckParams) ([]byte, error)

ReadWithMinThroughput reads n bytes from reader r erroring if the throughput ever drops below the threshold defined by MinThroughputCheckParams.

func WriteAll

func WriteAll(w io.Writer, dataBuffers ...[]byte) error

WriteAll will write the entirety of the supplied data buffers to an io.Writer. This my result in multiple calls to the io.Writer's Write method in order to write the entire buffer, and if at any point there is an error then the error will be returned.

func WriteIfNoErr

func WriteIfNoErr(w io.Writer, bytes []byte, err error) error

WriteIfNoErr allows you to chain calls to write and handle errors at the very end. If the error passed into the the function is non-nil then the error will be returned without any io. If the error is nil then all bytes in the supplied buffer will be written to the io.Writer

func WriteLine

func WriteLine(w io.Writer, line string) error

WriteLine will write the given string to an io.Writer followed by a newline.

func WriteLines

func WriteLines(w io.Writer, lines ...string) error

WriteLines will write the given strings to an io.Writer, each followed by a newline.

func WritePrimIfNoErr

func WritePrimIfNoErr(w io.Writer, prim interface{}, err error) error

WritePrimIfNoErr allows you to chain calls to write and handle errors at the very end. If the error passed into the function is non-nil then the error will be returned without any io. If the error is nil then supplied primitive will be written to the io.Writer using binary.Write with BigEndian byte ordering

Types

type ErrPreservingReader

type ErrPreservingReader struct {
	// R is the reader supplying the actual data.
	R io.Reader

	// Err is the first error that occurred, or nil
	Err error
}

ErrPreservingReader is a utility class that provides methods to read from a reader where errors can be ignored and handled later. Once an error occurs subsequent calls to read won't pull data from the io.Reader, will be a noop, and the initial error can be retrieved from Err at any time. ErrPreservingReader implements the io.Reader interface itself so it can be used as any other Reader would be.

func NewErrPreservingReader

func NewErrPreservingReader(r io.Reader) *ErrPreservingReader

NewErrPreservingReader creates a new instance of an ErrPreservingReader

func (*ErrPreservingReader) Read

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

Read reads data from the underlying io.Reader if no previous errors have occurred. If an error has already occurred then read will simply no-op and return 0 for the number of bytes read and the original error.

type MinThroughputCheckParams

type MinThroughputCheckParams struct {
	// MinBytesPerSec is the minimum throughput.  If ReadWithMinThroughput drops below this value for the most recent
	// time window then it will fail.
	MinBytesPerSec int64

	// CheckInterval how often should the throughput be checked
	CheckInterval time.Duration

	// NumIntervals defines the number of intervals that should be considered when looking at the throughput.
	// NumIntervals*CheckInterval defines the window size
	NumIntervals int
}

MinThroughputCheckParams defines the miminimum throughput, how often it should be checked, and what the time window size is

Jump to

Keyboard shortcuts

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