buffer

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2020 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

The buffer package provides utilities for temporary storage (buffering) of large blobs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer interface {
	// Open creates new Reader reading from the underlying storage.
	Open() (io.ReadCloser, error)

	// Len reports the length of the stored blob.
	//
	// Notably, it indicates the amount of bytes that can be read from the
	// newly created Reader without hiting io.EOF.
	Len() int

	// Remove discards buffered body and releases all associated resources.
	//
	// Multiple Buffer objects may refer to the same underlying storage.
	// In this case, care should be taken to ensure that Remove is called
	// only once since it will discard the shared storage and invalidate
	// all Buffer objects using it.
	//
	// Readers previously created using Open can still be used, but
	// new ones can't be created.
	Remove() error
}

Buffer interface represents abstract temporary storage for blobs.

The Buffer storage is assumed to be immutable. If any modifications are made - new storage location should be used for them. This is important to ensure goroutine-safety.

Since Buffer objects require a careful management of lifetimes, here is the convention: Its always creator responsibility to call Remove after Buffer is no longer used. If Buffer object is passed to a function - it is not guaranteed to be valid after this function returns. If function needs to preserve the storage contents, it should "re-buffer" it either by reading entire blob and storing it somewhere or applying implementation-specific methods (for example, the FileBuffer storage may be "re-buffered" by hard-linking the underlying file).

func BufferInFile

func BufferInFile(r io.Reader, dir string) (Buffer, error)

BufferInFile is a convenience function which creates FileBuffer with underlying file created in the specified directory with the random name.

func BufferInMemory

func BufferInMemory(r io.Reader) (Buffer, error)

BufferInMemory is a convenience function which creates MemoryBuffer with contents of the passed io.Reader.

type BytesReader

type BytesReader struct {
	*bytes.Reader
	// contains filtered or unexported fields
}

BytesReader is a wrapper for bytes.Reader that stores the original []byte value and allows to retrieve it.

It is meant for passing to libraries that expect a io.Reader but apply certain optimizations when the Reader implements Bytes() interface.

func NewBytesReader

func NewBytesReader(b []byte) BytesReader

func (BytesReader) Bytes

func (br BytesReader) Bytes() []byte

Bytes returns the unread portion of underlying slice used to construct BytesReader.

func (BytesReader) Close

func (br BytesReader) Close() error

Close is a dummy method for implementation of io.Closer so BytesReader can be used in MemoryBuffer directly.

func (BytesReader) Copy

func (br BytesReader) Copy() BytesReader

Copy returns the BytesReader reading from the same slice as br at the same position.

type FileBuffer

type FileBuffer struct {
	Path string

	// LenHint is the size of the stored blob. It can
	// be set to avoid the need to call os.Stat in the
	// Len() method.
	LenHint int
}

FileBuffer implements Buffer interface using file system.

func (FileBuffer) Len

func (fb FileBuffer) Len() int

func (FileBuffer) Open

func (fb FileBuffer) Open() (io.ReadCloser, error)

func (FileBuffer) Remove

func (fb FileBuffer) Remove() error

type MemoryBuffer

type MemoryBuffer struct {
	Slice []byte
}

MemoryBuffer implements Buffer interface using byte slice.

func (MemoryBuffer) Len

func (mb MemoryBuffer) Len() int

func (MemoryBuffer) Open

func (mb MemoryBuffer) Open() (io.ReadCloser, error)

func (MemoryBuffer) Remove

func (mb MemoryBuffer) Remove() error

Jump to

Keyboard shortcuts

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