byframe: github.com/ysmood/byframe Index | Examples | Files

package byframe

import "github.com/ysmood/byframe"

Index

Examples

Package Files

byframe.go stream.go tuple.go

Variables

var ErrHeaderInsufficient = errors.New("data is not sufficient to construct the header")

ErrHeaderInsufficient ...

var ErrInsufficient = errors.New("data is not sufficient to construct the body")

ErrInsufficient ...

func Decode Uses

func Decode(data []byte) ([]byte, int, error)

Decode decode frame into data, decoded bytes and error

func DecodeHeader Uses

func DecodeHeader(raw []byte) (int, int, bool)

DecodeHeader decode bytes into data length, header length and whether it's sufficient to parse the header from raw.

func DecodeTuple Uses

func DecodeTuple(data []byte, items ...*[]byte) error

DecodeTuple zero copy decode

func Encode Uses

func Encode(data []byte) []byte

Encode encode data into frame format

Code:

frame := byframe.Encode([]byte("test"))

data, _, _ := byframe.Decode(frame)

fmt.Println(string(data))

Output:

test

func EncodeHeader Uses

func EncodeHeader(l int) (header []byte)

EncodeHeader encode data length into header

Code:

header := byframe.EncodeHeader(1000)

dataLen, headerLen, sufficient := byframe.DecodeHeader(header)

fmt.Println(headerLen, dataLen, sufficient)

Output:

2 1000 true

func EncodeTuple Uses

func EncodeTuple(items ...*[]byte) []byte

EncodeTuple encode an array of []byte into an []byte. The format:

| itemA length | itemA bytes | ... | itemN length | itemN bytes | itemLast bytes |

The last item doesn't need length header. The protection for data curruption can be done on other levels of data handling, this algorithm is designed for small size.

Code:

type Name struct {
    First []byte
    Last  []byte
}

name := Name{[]byte("Jack"), []byte("Black")}

data := byframe.EncodeTuple(&name.First, &name.Last)

var newName Name
_ = byframe.DecodeTuple(data, &newName.First, &newName.Last)

fmt.Println(string(newName.First), string(newName.Last))

Output:

Jack Black

type Scanner Uses

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

Scanner scan frames based on the length header

Code:

frame := byframe.Encode([]byte("test"))
s := byframe.NewScanner(bytes.NewReader(frame))

for s.Scan() {
    fmt.Println(string(s.Frame()))
}

Output:

test

func NewScanner Uses

func NewScanner(r io.Reader) *Scanner

NewScanner just like line scanner

func (*Scanner) Err Uses

func (s *Scanner) Err() error

Err the error

func (*Scanner) Frame Uses

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

Frame current frame

func (*Scanner) Scan Uses

func (s *Scanner) Scan() bool

Scan scan next frame

Package byframe imports 2 packages (graph) and is imported by 4 packages. Updated 2019-08-16. Refresh now. Tools for package owners.