nio

package module
v2.0.3+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2017 License: MIT Imports: 2 Imported by: 20

README

nio

GoDoc Release Software License Build Status Coverage Status Go Report Card

Usage

The Buffer interface:

type Buffer interface {
	Len() int64
	Cap() int64
	io.ReadWriter
}

nio's Copy method concurrently copies from an io.Reader to a supplied nio.Buffer, then from the nio.Buffer to an io.Writer. This way, blocking writes don't slow the io.Reader.

import (
  "github.com/djherbis/buffer"
  "github.com/djherbis/nio"
)

buf := buffer.New(32*1024) // 32KB In memory Buffer
nio.Copy(w, r, buf) // Reads and Writes concurrently, buffering using buf.

nio's Pipe method is a buffered version of io.Pipe The writer return once its data has been written to the Buffer. The reader returns with data off the Buffer.

import (
  "gopkg.in/djherbis/buffer.v1"
  "gopkg.in/djherbis/nio.v2"
)

buf := buffer.New(32*1024) // 32KB In memory Buffer
r, w := nio.Pipe(buf)

Installation

go get gopkg.in/djherbis/nio.v2

For some pre-built buffers grab:

go get gopkg.in/djherbis/buffer.v1

Documentation

Overview

Package nio provides a few buffered io primitives.

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

Types

type Buffer

type Buffer interface {
	// Len returns how many bytes are buffered
	Len() int64

	// Cap returns how many bytes can in the buffer at a time
	Cap() int64

	// ReadWriter writes are stored in the buffer, reads return the stored data
	io.ReadWriter
}

Buffer is used to store bytes.

type PipeReader

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

PipeReader is the read half of the pipe.

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)

Jump to

Keyboard shortcuts

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