iox

package
v0.0.0-...-24c7dc5 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Edit! This Gosh fork includes a modifed copy of of Distin H's buffered io pipes package and the corresponding buffer package on github, both have the same license and copyright. Awesome program, highly recommend. I did this so goshworker could just use the gosh exec api without extrenal dependencies exernal dependiencies other custom than channels which will be for greater memory and task management later, while keeping this version of gosh standard libary compliant.

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(dst io.Writer, src io.Reader, buf Buffer) (n int64, err error)

Copy copies from src to buf, and from buf to dst in parallel until either EOF is reached on src or an error occurs. It returns the number of bytes copied to dst and the first error encountered while copying, if any. EOF is not considered to be an error. If src implements WriterTo, it is used to write to the supplied Buffer. If dst implements ReaderFrom, it is used to read from the supplied Buffer.

func Empty

func Empty(buf Buffer) bool

Empty returns false iff buf.Len() == 0

func Full

func Full(buf Buffer) bool

Full returns true iff buf.Len() == buf.Cap()

func Gap

func Gap(buf Buffer) int64

Gap returns buf.Cap() - buf.Len()

func LimitWriter

func LimitWriter(w io.Writer, n int64) io.Writer

LimitWriter works like io.LimitReader. It writes at most n bytes to the underlying Writer. It returns io.ErrShortWrite if more than n bytes are attempted to be written.

func NewReader

func NewReader(src io.Reader, buf Buffer) io.ReadCloser

NewReader reads from the buffer which is concurrently filled with data from the passed src.

func Pipe

func Pipe(buf Buffer) (r *PipeReader, w *PipeWriter)

Pipe creates a buffered pipe. It can be used to connect code expecting an io.Reader with code expecting an io.Writer. Reads on one end read from the supplied Buffer. Writes write to the supplied Buffer. It is safe to call Read and Write in parallel with each other or with Close. Close will complete once pending I/O is done, and may cancel blocking Read/Writes. Buffered data will still be available to Read after the Writer has been closed. Parallel calls to Read, and parallel calls to Write are also safe : the individual calls will be gated sequentially.

func ReaderFromByteSlice

func ReaderFromByteSlice(bats []byte) io.Reader

func ReaderFromChanByteSlice

func ReaderFromChanByteSlice(ch chan []byte) io.Reader

func ReaderFromChanReadonlyByteSlice

func ReaderFromChanReadonlyByteSlice(ch <-chan []byte) io.Reader

func ReaderFromChanReadonlyString

func ReaderFromChanReadonlyString(ch <-chan string) io.Reader

func ReaderFromChanString

func ReaderFromChanString(ch chan string) io.Reader

func ReaderFromInterface

func ReaderFromInterface(x interface{}) io.Reader

Converts any of a range of data sources to an io.Reader interface, or an io.ReadCloser if appropriate.

Readers will be produced from:

string
[]byte
io.Reader
bytes.Buffer
<-chan string
<-chan []byte

ReadClosers will be produced from:

chan string
chan []byte

An error of type ReaderUnrefinableFromInterface is thrown if an argument of any other type is given.

func ReaderFromString

func ReaderFromString(str string) io.Reader

func Wrap

func Wrap(w DoerAt, p []byte, off int64, wrapAt int64) (n int, err error)

Wrap causes an action on an array of bytes (like read/write) to be done from an offset off, wrapping at offset wrapAt.

func WriterFromInterface

func WriterFromInterface(x interface{}) io.Writer

Converts any of a range of data sinks to an io.Writer interface, or an io.WriteCloser if appropriate.

Writers will be produced from:

io.Writer
bytes.Buffer

WriteClosers will be produced from:

<-chan string
chan string
<-chan []byte
chan []byte

An error of type WriterUnrefinableFromInterface is thrown if an argument of any other type is given.

func WriterToChanByteSlice

func WriterToChanByteSlice(ch chan<- []byte) io.Writer

func WriterToChanString

func WriterToChanString(ch chan<- string) io.Writer

Types

type Buffer

type Buffer interface {
	Len() int64 // How much data is Buffered in bytes
	Cap() int64 // How much data can be Buffered at once in bytes.
	io.Reader   // Read() will read from the top of the buffer [io.EOF if empty]
	io.Writer   // Write() will write to the end of the buffer [io.ErrShortWrite if not enough space]
	Reset()     // Truncates the buffer, Len() == 0.
}
var Discard Buffer = discard{}

Discard is a Buffer which writes to ioutil.Discard and read's return 0, io.EOF. All of its methods are concurrent safe.

func NewMulti

func NewMulti(buffers ...Buffer) Buffer

NewMulti returns a Buffer which is the logical concatenation of the passed buffers. The data in the buffers is shifted such that there is no non-empty buffer following a non-full buffer, this process is also run after every Read. If no buffers are passed, the returned Buffer is nil.

func NewPartition

func NewPartition(pool Pool, buffers ...Buffer) Buffer

NewPartition returns a Buffer which uses a Pool to extend or shrink its size as needed. It automatically allocates new buffers with pool.Get() to extend is length, and pool.Put() to release unused buffers as it shrinks.

func NewRing

func NewRing(buffer BufferAt) Buffer

NewRing returns a Ring Buffer from a BufferAt. It overwrites old data in the Buffer when needed (when its full).

func NewSpill

func NewSpill(buf Buffer, w io.Writer) Buffer

NewSpill returns a Buffer which writes data to w when there's an error writing to buf. Such as when buf is full, or the disk is full, etc.

func NewSwap

func NewSwap(a, b Buffer) Buffer

NewSwap creates a Buffer which writes to a until you write past a.Cap() then it io.Copy's from a to b and writes to b. Once the Buffer is empty again, it starts over writing to a. Note that if b.Cap() <= a.Cap() it will cause a panic, b is expected to be larger in order to accomodate writes past a.Cap().

func NewUnboundedBuffer

func NewUnboundedBuffer(mem, file int64) Buffer

NewUnboundedBuffer returns a Buffer which buffers "mem" bytes to memory and then creates file's of size "file" to buffer above "mem" bytes.

type BufferAt

type BufferAt interface {
	Buffer
	io.ReaderAt
	io.WriterAt
}

BufferAt is a buffer which supports io.ReaderAt and io.WriterAt

func New

func New(n int64) BufferAt

New returns a new in memory BufferAt with max size N. It's backed by a bytes.Buffer.

func NewFile

func NewFile(N int64, file File) BufferAt

NewFile returns a new BufferAt backed by "file" with max-size N.

func NewMultiAt

func NewMultiAt(buffers ...BufferAt) BufferAt

NewMultiAt returns a BufferAt which is the logical concatenation of the passed BufferAts. The data in the buffers is shifted such that there is no non-empty buffer following a non-full buffer, this process is also run after every Read. If no buffers are passed, the returned Buffer is nil.

func NewSwapAt

func NewSwapAt(a, b BufferAt) BufferAt

NewSwapAt creates a BufferAt which writes to a until you write past a.Cap() then it io.Copy's from a to b and writes to b. Once the Buffer is empty again, it starts over writing to a. Note that if b.Cap() <= a.Cap() it will cause a panic, b is expected to be larger in order to accomodate writes past a.Cap().

type DoAtFunc

type DoAtFunc func([]byte, int64) (int, error)

DoAtFunc is implemented by ReadAt/WriteAt

type DoerAt

type DoerAt interface {
	DoAt([]byte, int64) (int, error)
}

type File

type File interface {
	Name() string
	Stat() (fi os.FileInfo, err error)
	io.ReaderAt
	io.WriterAt
	Close() error
}

type List

type List []Buffer

func (*List) Cap

func (l *List) Cap() (n int64)

Cap is the sum of the Cap()'s of the Buffers in the List.

func (*List) Len

func (l *List) Len() (n int64)

Len is the sum of the Len()'s of the Buffers in the List.

func (*List) Pop

func (l *List) Pop() (b Buffer)

Pop removes and returns a Buffer from the front of the List

func (*List) Push

func (l *List) Push(b Buffer)

Push adds a Buffer to the end of the List

func (*List) Reset

func (l *List) Reset()

Reset calls Reset() on each of the Buffers in the list.

type PipeReader

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

func (*PipeReader) Close

func (r *PipeReader) Close() error

Close closes the reader; subsequent writes to the write half of the pipe will return the error io.ErrClosedPipe.

func (*PipeReader) CloseWithError

func (r *PipeReader) CloseWithError(err error) error

CloseWithError closes the reader; subsequent writes to the write half of the pipe will return the error err.

func (*PipeReader) Read

func (r *PipeReader) Read(p []byte) (n int, err error)

type PipeWriter

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

A PipeWriter is the write half of a pipe.

func (*PipeWriter) Close

func (w *PipeWriter) Close() error

Close closes the writer; once the buffer is empty subsequent reads from the read half of the pipe will return no bytes and io.EOF after all the buffer has been read. CloseWithError always returns nil.

func (*PipeWriter) CloseWithError

func (w *PipeWriter) CloseWithError(err error) error

CloseWithError closes the writer; once the buffer is empty subsequent reads from the read half of the pipe will return no bytes and the error err, or io.EOF if err is nil. CloseWithError always returns nil.

func (*PipeWriter) Write

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

type Pool

type Pool interface {
	Get() (Buffer, error) // Allocate a Buffer
	Put(buf Buffer) error // Release or Reuse a Buffer
}

func NewFilePool

func NewFilePool(N int64, dir string) Pool

NewFilePool returns a Pool, Get() returns a file-based buffer of max size N. Put() closes and deletes the underlying file for the buffer. Get() may return an error if it fails to create a file for the buffer. Put() may return an error if it fails to delete the file.

func NewMemPool

func NewMemPool(N int64) Pool

NewMemPool returns a Pool, Get() returns an in memory buffer of max size N. Put() returns the buffer to the pool after resetting it. Get() and Put() errors will always be nil.

func NewPool

func NewPool(New func() Buffer) Pool

NewPool returns a Pool(), it's backed by a sync.Pool so its safe for concurrent use. Get() and Put() errors will always be nil. It will not work with gob.

type ReadWriterAt

type ReadWriterAt interface {
	io.ReaderAt
	io.WriterAt
}

ReadWriterAt implements io.ReaderAt and io.WriterAt

type ReaderUnrefinableFromInterface

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

Error raised by ReaderFromInterface() when is called with an argument of an unexpected type.

func (ReaderUnrefinableFromInterface) Error

type WrapReader

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

WrapReader wraps reads around a section of data.

func NewWrapReader

func NewWrapReader(r io.ReaderAt, off int64, wrapAt int64) *WrapReader

NewWrapReader creates a WrapReader starting at offset off, and wrapping at offset wrapAt.

func (WrapReader) DoAt

func (w WrapReader) DoAt(p []byte, off int64) (n int, err error)

func (WrapReader) Offset

func (w WrapReader) Offset() int64

func (*WrapReader) Read

func (r *WrapReader) Read(p []byte) (n int, err error)

Read reads into p starting at the current offset, wrapping if it reaches the end. The current offset is shifted forward by the amount read.

func (*WrapReader) ReadAt

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

ReadAt reads into p starting at the current offset, wrapping when it reaches the end.

func (WrapReader) Seek

func (w WrapReader) Seek(offset int64, whence int) (int64, error)

type WrapWriter

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

WrapWriter wraps writes around a section of data.

func NewWrapWriter

func NewWrapWriter(w io.WriterAt, off int64, wrapAt int64) *WrapWriter

NewWrapWriter creates a WrapWriter starting at offset off, and wrapping at offset wrapAt.

func (WrapWriter) DoAt

func (w WrapWriter) DoAt(p []byte, off int64) (n int, err error)

func (WrapWriter) Offset

func (w WrapWriter) Offset() int64

func (WrapWriter) Seek

func (w WrapWriter) Seek(offset int64, whence int) (int64, error)

func (*WrapWriter) Write

func (w *WrapWriter) Write(p []byte) (n int, err error)

Write writes p starting at the current offset, wrapping when it reaches the end. The current offset is shifted forward by the amount written.

func (*WrapWriter) WriteAt

func (w *WrapWriter) WriteAt(p []byte, off int64) (n int, err error)

WriteAt writes p starting at offset off, wrapping when it reaches the end.

type Wrapper

type Wrapper struct {
	// N is the offset at which to "wrap" back to the start
	N int64
	// L is the length of the data written
	L int64
	// O is our offset in the data
	O int64
	// contains filtered or unexported fields
}

Wrapper implements a io.ReadWriter and ReadWriterAt such that when reading/writing goes past N bytes, it "wraps" back to the beginning.

func NewWrapper

func NewWrapper(rwa ReadWriterAt, L, O, N int64) *Wrapper

NewWrapper creates a Wrapper based on ReadWriterAt rwa. L is the current length, O is the current offset, and N is offset at which we "wrap".

func (*Wrapper) Cap

func (wpr *Wrapper) Cap() int64

Cap returns the "wrap" offset (max # of bytes)

func (*Wrapper) Len

func (wpr *Wrapper) Len() int64

Len returns the # of bytes in the Wrapper

func (*Wrapper) Read

func (wpr *Wrapper) Read(p []byte) (n int, err error)

Read reads from the current offset into p, wrapping at Cap()

func (*Wrapper) ReadAt

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

ReadAt reads from the current offset+off into p, wrapping at Cap()

func (*Wrapper) Reset

func (wpr *Wrapper) Reset()

Reset seeks to the start (0 offset), and sets the length to 0.

func (*Wrapper) SetReadWriterAt

func (wpr *Wrapper) SetReadWriterAt(rwa ReadWriterAt)

SetReadWriterAt lets you switch the underlying Read/WriterAt

func (*Wrapper) Write

func (wpr *Wrapper) Write(p []byte) (n int, err error)

Write writes p to the end of the Wrapper (at Len()), wrapping at Cap()

func (*Wrapper) WriteAt

func (wpr *Wrapper) WriteAt(p []byte, off int64) (n int, err error)

WriteAt writes p at the current offset+off, wrapping at Cap()

type WriterUnrefinableFromInterface

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

Error raised by WriterFromInterface() when is called with an argument of an unexpected type.

func (WriterUnrefinableFromInterface) Error

Jump to

Keyboard shortcuts

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