filebuffer

package module
Version: v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 7, 2020 License: MIT Imports: 5 Imported by: 16

README

filebuffer

filebuffer is a package implementing a few file-like interfaces such as io.Reader, io.ReaderAt, io.Seeker and more. The implementation is backed by a byte buffer and the main purpose is to have in-memory alternative to using an io.File.

More information can be found on the GoDoc page.

GoDoc

Build Status

Documentation

Overview

Package filebuffer is a package implementing a few file like interfaces backed by a byte buffer. Implemented interfaces:

* Reader * ReaderAt * Writer * Seeker * Closer

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer struct {
	// Buff is the backing buffer
	Buff *bytes.Buffer
	// Index indicates where in the buffer we are at
	Index int64
	// contains filtered or unexported fields
}

Buffer implements interfaces implemented by files. The main purpose of this type is to have an in memory replacement for a file.

func New

func New(b []byte) *Buffer

New returns a new populated Buffer

func NewFromReader added in v1.0.1

func NewFromReader(reader io.Reader) (*Buffer, error)

NewFromReader is a convenience method that returns a new populated Buffer whose contents are sourced from a supplied reader by loading it entirely into memory.

func (*Buffer) Bytes

func (f *Buffer) Bytes() []byte

Bytes returns the bytes available until the end of the buffer.

func (*Buffer) Close

func (f *Buffer) Close() error

Close implements io.Closer https://golang.org/pkg/io/#Closer It closes the buffer, rendering it unusable for I/O. It returns an error, if any.

func (*Buffer) Read

func (f *Buffer) Read(b []byte) (n int, err error)

When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. An instance of this general case is that a Reader returning a non-zero number of bytes at the end of the input stream may return either err == EOF or err == nil. The next Read should return 0, EOF.

func (*Buffer) ReadAt

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

ReadAt implements io.ReaderAt https://golang.org/pkg/io/#ReaderAt ReadAt reads len(p) bytes into p starting at offset off in the underlying input source. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered.

When ReadAt returns n < len(p), it returns a non-nil error explaining why more bytes were not returned. In this respect, ReadAt is stricter than Read.

Even if ReadAt returns n < len(p), it may use all of p as scratch space during the call. If some data is available but not len(p) bytes, ReadAt blocks until either all the data is available or an error occurs. In this respect ReadAt is different from Read.

If the n = len(p) bytes returned by ReadAt are at the end of the input source, ReadAt may return either err == EOF or err == nil.

If ReadAt is reading from an input source with a seek offset, ReadAt should not affect nor be affected by the underlying seek offset. Clients of ReadAt can execute parallel ReadAt calls on the same input source.

func (*Buffer) Seek

func (f *Buffer) Seek(offset int64, whence int) (idx int64, err error)

Seek implements io.Seeker https://golang.org/pkg/io/#Seeker

func (*Buffer) String

func (f *Buffer) String() string

String implements the Stringer interface

func (*Buffer) Write

func (f *Buffer) Write(p []byte) (n int, err error)

Write implements io.Writer https://golang.org/pkg/io/#Writer by appending the passed bytes to the buffer unless the buffer is closed or index negative.

Source Files

Jump to

Keyboard shortcuts

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