delimited

package
v0.1.16-profiler Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package delimited implements a reader and writer for simple streams of length-delimited byte records. Each record is written as a varint-encoded length in bytes, followed immediately by the record itself.

A stream consists of a sequence of such records packed consecutively without additional padding. There are no checksums or compression.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(sink Sink, src Source) error

Copy sequentially copies each record read from src to sink until src.Next() returns io.EOF or another error occurs.

Types

type Chan

type Chan struct {
	CloseChan chan struct{}
	// contains filtered or unexported fields
}

Chan is a delimited duplex channel. It is used to have a channel interface around a delimited.Reader or Writer.

func NewChan

func NewChan(chanSize int) *Chan

NewChan constructs a Chan with a given buffer size.

func (*Chan) Close

func (s *Chan) Close()

Close the Chan

func (*Chan) In

func (s *Chan) In() chan []byte

In exposes the incoming message channel

func (*Chan) Out

func (s *Chan) Out(message []byte) error

Out sends message on the wire, blocking.

func (*Chan) Pipe

func (s *Chan) Pipe(rwc io.ReadWriteCloser)

Pipe invokes the reader and writer flows, once it's ran Chan can start serving incoming/outgoing messages

type Reader

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

Reader consumes length-delimited records from a byte source.

Usage:

rd := delimited.NewReader(r)
for {
  rec, err := rd.Next()
  if err == io.EOF {
    break
  } else if err != nil {
    log.Fatal(err)
  }
  doStuffWith(rec)
}

func NewReader

func NewReader(r io.Reader) *Reader

NewReader constructs a new delimited Reader for the records in r.

func (*Reader) Next

func (r *Reader) Next() ([]byte, error)

Next returns the next length-delimited record from the input, or io.EOF if there are no more records available. Returns io.ErrUnexpectedEOF if a short record is found, with a length of n but fewer than n bytes of data. Because there is no resynchronization mechanism, it is generally not possible to recover from a short record in this format.

The slice returned is valid only until a subsequent call to Next.

func (*Reader) NextProto

func (r *Reader) NextProto(pb proto.Message) error

NextProto consumes the next available record by calling r.Next, and decodes it into pb with proto.Unmarshal.

type Sink

type Sink interface {
	// Put delivers a record to the sink.
	Put([]byte) error
}

A Sink represents a receiver of records.

type Source

type Source interface {
	// Next returns the next record in the sequence, or io.EOF when no further
	// records are available. The slice returned by Next is only required to be
	// valid until a subsequent call to Next.
	Next() ([]byte, error)
}

A Source represents a sequence of records.

type Writer

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

A Writer outputs delimited records to an io.Writer.

Basic usage:

wr := delimited.NewWriter(w)
for record := range records {
   if err := wr.Put(record); err != nil {
     log.Fatal(err)
   }
}

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter constructs a new delimited Writer that writes records to w.

func (Writer) Put

func (w Writer) Put(record []byte) error

Put writes the specified record to the writer. It equivalent to WriteRecord, but discards the number of bytes written.

func (Writer) PutProto

func (w Writer) PutProto(msg proto.Message) error

PutProto encodes and writes the specified proto.Message to the writer.

func (Writer) WriteRecord

func (w Writer) WriteRecord(record []byte) (int, error)

WriteRecord writes the specified record to the underlying writer, returning the total number of bytes written including the length tag.

Jump to

Keyboard shortcuts

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