io

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2022 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package io provides basic interfaces to I/O primitives. Its primary job is to wrap existing implementations of such primitives, such as those in package os, into shared public interfaces that abstract the functionality, plus some other related primitives.

Because these interfaces and primitives wrap lower-level operations with various implementations, unless otherwise informed clients should not assume they are safe for parallel execution.

Index

Constants

This section is empty.

Variables

View Source
var EOF = errors.New("EOF")

EOF is the error returned by Read when no more input is available. Functions should return EOF only to signal a graceful end of input. If the EOF occurs unexpectedly in a structured data stream, the appropriate error is either ErrUnexpectedEOF or some other error giving more detail.

View Source
var ErrNoProgress = errors.New("multiple Read calls return no data or error")

ErrNoProgress is returned by some clients of an io.Reader when many calls to Read have failed to return any data or error, usually the sign of a broken io.Reader implementation.

View Source
var ErrShortBuffer = errors.New("short buffer")

ErrShortBuffer means that a read required a longer buffer than was provided.

View Source
var ErrShortWrite = errors.New("short write")

ErrShortWrite means that a write accepted fewer bytes than requested but failed to return an explicit error.

View Source
var ErrUnexpectedEOF = errors.New("unexpected EOF")

ErrUnexpectedEOF means that EOF was encountered in the middle of reading a fixed-size block or data structure.

Functions

func Copy

func Copy(dst Writer, src Reader) (written int64, err error)

Copy copies from src to dst until either EOF is reached on src or an error occurs. It returns the number of bytes copied and the first error encountered while copying, if any.

A successful Copy returns err == nil, not err == EOF. Because Copy is defined to read from src until EOF, it does not treat an EOF from Read as an error to be reported.

If dst implements the ReaderFrom interface, the copy is implemented by calling dst.ReadFrom(src). Otherwise, if src implements the WriterTo interface, the copy is implemented by calling src.WriteTo(dst).

func CopyN

func CopyN(dst Writer, src Reader, n int64) (written int64, err error)

CopyN copies n bytes (or until an error) from src to dst. It returns the number of bytes copied and the earliest error encountered while copying. On return, written == n if and only if err == nil.

If dst implements the ReaderFrom interface, the copy is implemented using it.

func ReadAtLeast

func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error)

ReadAtLeast reads from r into buf until it has read at least min bytes. It returns the number of bytes copied and an error if fewer bytes were read. The error is EOF only if no bytes were read. If an EOF happens after reading fewer than min bytes, ReadAtLeast returns ErrUnexpectedEOF. If min is greater than the length of buf, ReadAtLeast returns ErrShortBuffer. On return, n >= min if and only if err == nil.

func ReadFull

func ReadFull(r Reader, buf []byte) (n int, err error)

ReadFull reads exactly len(buf) bytes from r into buf. It returns the number of bytes copied and an error if fewer bytes were read. The error is EOF only if no bytes were read. If an EOF happens after reading some but not all the bytes, ReadFull returns ErrUnexpectedEOF. On return, n == len(buf) if and only if err == nil.

func WriteString

func WriteString(w Writer, s string) (n int, err error)

WriteString writes the contents of the string s to w, which accepts an array of bytes. If w already implements a WriteString method, it is invoked directly.

Types

type ByteReader

type ByteReader interface {
	ReadByte() (c byte, err error)
}

ByteReader is the interface that wraps the ReadByte method.

ReadByte reads and returns the next byte from the input. If no byte is available, err will be set.

type ByteScanner

type ByteScanner interface {
	ByteReader
	UnreadByte() error
}

ByteScanner is the interface that adds the UnreadByte method to the basic ReadByte method.

UnreadByte causes the next call to ReadByte to return the same byte as the previous call to ReadByte. It may be an error to call UnreadByte twice without an intervening call to ReadByte.

type ByteWriter

type ByteWriter interface {
	WriteByte(c byte) error
}

ByteWriter is the interface that wraps the WriteByte method.

type Closer

type Closer interface {
	Close() error
}

Closer is the interface that wraps the basic Close method.

The behavior of Close after the first call is undefined. Specific implementations may document their own behavior.

type LimitedReader

type LimitedReader struct {
	R Reader // underlying reader
	N int64  // max bytes remaining
}

A LimitedReader reads from R but limits the amount of data returned to just N bytes. Each call to Read updates N to reflect the new amount remaining.

func (*LimitedReader) Read

func (l *LimitedReader) Read(p []byte) (n int, err error)

type ReadCloser

type ReadCloser interface {
	Reader
	Closer
}

ReadCloser is the interface that groups the basic Read and Close methods.

type ReadSeeker

type ReadSeeker interface {
	Reader
	Seeker
}

ReadSeeker is the interface that groups the basic Read and Seek methods.

type ReadWriteCloser

type ReadWriteCloser interface {
	Reader
	Writer
	Closer
}

ReadWriteCloser is the interface that groups the basic Read, Write and Close methods.

type ReadWriteSeeker

type ReadWriteSeeker interface {
	Reader
	Writer
	Seeker
}

ReadWriteSeeker is the interface that groups the basic Read, Write and Seek methods.

type ReadWriter

type ReadWriter interface {
	Reader
	Writer
}

ReadWriter is the interface that groups the basic Read and Write methods.

type Reader

type Reader interface {
	Read(p []byte) (n int, err error)
}

Reader is the interface that wraps the basic Read method.

Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Even if Read 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, Read conventionally returns what is available instead of waiting for more.

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 regardless.

Callers should always process the n > 0 bytes returned before considering the error err. Doing so correctly handles I/O errors that happen after reading some bytes and also both of the allowed EOF behaviors.

Implementations of Read are discouraged from returning a zero byte count with a nil error, and callers should treat that situation as a no-op.

func LimitReader

func LimitReader(r Reader, n int64) Reader

LimitReader returns a Reader that reads from r but stops with EOF after n bytes. The underlying implementation is a *LimitedReader.

func TeeReader

func TeeReader(r Reader, w Writer) Reader

TeeReader returns a Reader that writes to w what it reads from r. All reads from r performed through it are matched with corresponding writes to w. There is no internal buffering - the write must complete before the read completes. Any error encountered while writing is reported as a read error.

type ReaderAt

type ReaderAt interface {
	ReadAt(p []byte, off int64) (n int, err error)
}

ReaderAt is the interface that wraps the basic ReadAt method.

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.

type ReaderFrom

type ReaderFrom interface {
	ReadFrom(r Reader) (n int64, err error)
}

ReaderFrom is the interface that wraps the ReadFrom method.

ReadFrom reads data from r until EOF or error. The return value n is the number of bytes read. Any error except io.EOF encountered during the read is also returned.

The Copy function uses ReaderFrom if available.

type RuneReader

type RuneReader interface {
	ReadRune() (r rune, size int, err error)
}

RuneReader is the interface that wraps the ReadRune method.

ReadRune reads a single UTF-8 encoded Unicode character and returns the rune and its size in bytes. If no character is available, err will be set.

type RuneScanner

type RuneScanner interface {
	RuneReader
	UnreadRune() error
}

RuneScanner is the interface that adds the UnreadRune method to the basic ReadRune method.

UnreadRune causes the next call to ReadRune to return the same rune as the previous call to ReadRune. It may be an error to call UnreadRune twice without an intervening call to ReadRune.

type SectionReader

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

SectionReader implements Read, Seek, and ReadAt on a section of an underlying ReaderAt.

func NewSectionReader

func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader

NewSectionReader returns a SectionReader that reads from r starting at offset off and stops with EOF after n bytes.

func (*SectionReader) Read

func (s *SectionReader) Read(p []byte) (n int, err error)

func (*SectionReader) ReadAt

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

func (*SectionReader) Seek

func (s *SectionReader) Seek(offset int64, whence int) (ret int64, err error)

func (*SectionReader) Size

func (s *SectionReader) Size() int64

Size returns the size of the section in bytes.

type Seeker

type Seeker interface {
	Seek(offset int64, whence int) (ret int64, err error)
}

Seeker is the interface that wraps the basic Seek method.

Seek sets the offset for the next Read or Write to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. Seek returns the new offset and an Error, if any.

type WriteCloser

type WriteCloser interface {
	Writer
	Closer
}

WriteCloser is the interface that groups the basic Write and Close methods.

type WriteSeeker

type WriteSeeker interface {
	Writer
	Seeker
}

WriteSeeker is the interface that groups the basic Write and Seek methods.

type Writer

type Writer interface {
	Write(p []byte) (n int, err error)
}

Writer is the interface that wraps the basic Write method.

Write writes len(p) bytes from p to the underlying data stream. It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early. Write must return a non-nil error if it returns n < len(p).

type WriterAt

type WriterAt interface {
	WriteAt(p []byte, off int64) (n int, err error)
}

WriterAt is the interface that wraps the basic WriteAt method.

WriteAt writes len(p) bytes from p to the underlying data stream at offset off. It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early. WriteAt must return a non-nil error if it returns n < len(p).

If WriteAt is writing to a destination with a seek offset, WriteAt should not affect nor be affected by the underlying seek offset.

Clients of WriteAt can execute parallel WriteAt calls on the same destination if the ranges do not overlap.

type WriterTo

type WriterTo interface {
	WriteTo(w Writer) (n int64, err error)
}

WriterTo is the interface that wraps the WriteTo method.

WriteTo writes data to w until there's no more data to write or when an error occurs. The return value n is the number of bytes written. Any error encountered during the write is also returned.

The Copy function uses WriterTo if available.

Directories

Path Synopsis
Package ioutil implements some I/O utility functions.
Package ioutil implements some I/O utility functions.

Jump to

Keyboard shortcuts

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