pktline

package
v5.4.3 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package pktline implements reading payloads form pkt-lines and encoding pkt-lines from payloads.

Index

Constants

View Source
const (
	// MaxPayloadSize is the maximum payload size of a pkt-line in bytes.
	MaxPayloadSize = 65516

	// For compatibility with canonical Git implementation, accept longer pkt-lines
	OversizePayloadMax = 65520
)

Variables

View Source
var (
	// FlushPkt are the contents of a flush-pkt pkt-line.
	FlushPkt = []byte{'0', '0', '0', '0'}
	// Flush is the payload to use with the Encode method to encode a flush-pkt.
	Flush = []byte{}
	// FlushString is the payload to use with the EncodeString method to encode a flush-pkt.
	FlushString = ""
	// ErrPayloadTooLong is returned by the Encode methods when any of the
	// provided payloads is bigger than MaxPayloadSize.
	ErrPayloadTooLong = errors.New("payload is too long")
)
View Source
var ErrInvalidPktLen = errors.New("invalid pkt-len found")

ErrInvalidPktLen is returned by Err() when an invalid pkt-len is found.

Functions

This section is empty.

Types

type Encoder

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

An Encoder writes pkt-lines to an output stream.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode

func (e *Encoder) Encode(payloads ...[]byte) error

Encode encodes a pkt-line with the payload specified and write it to the output stream. If several payloads are specified, each of them will get streamed in their own pkt-lines.

func (*Encoder) EncodeString

func (e *Encoder) EncodeString(payloads ...string) error

EncodeString works similarly as Encode but payloads are specified as strings.

func (*Encoder) Encodef

func (e *Encoder) Encodef(format string, a ...interface{}) error

Encodef encodes a single pkt-line with the payload formatted as the format specifier. The rest of the arguments will be used in the format string.

func (*Encoder) Flush

func (e *Encoder) Flush() error

Flush encodes a flush-pkt to the output stream.

type Scanner

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

Scanner provides a convenient interface for reading the payloads of a series of pkt-lines. It takes an io.Reader providing the source, which then can be tokenized through repeated calls to the Scan method.

After each Scan call, the Bytes method will return the payload of the corresponding pkt-line on a shared buffer, which will be 65516 bytes or smaller. Flush pkt-lines are represented by empty byte slices.

Scanning stops at EOF or the first I/O error.

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner returns a new Scanner to read from r.

func (*Scanner) Bytes

func (s *Scanner) Bytes() []byte

Bytes returns the most recent payload generated by a call to Scan. The underlying array may point to data that will be overwritten by a subsequent call to Scan. It does no allocation.

func (*Scanner) Err

func (s *Scanner) Err() error

Err returns the first error encountered by the Scanner.

func (*Scanner) Scan

func (s *Scanner) Scan() bool

Scan advances the Scanner to the next pkt-line, whose payload will then be available through the Bytes method. Scanning stops at EOF or the first I/O error. After Scan returns false, the Err method will return any error that occurred during scanning, except that if it was io.EOF, Err will return nil.

Jump to

Keyboard shortcuts

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