Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrHeaderInsufficient = errors.New("data is not sufficient to construct the header")
ErrHeaderInsufficient ...
View Source
var ErrInsufficient = errors.New("data is not sufficient to construct the body")
ErrInsufficient ...
Functions ¶
func DecodeHeader ¶
DecodeHeader decode bytes into data length, header length and whether it's sufficient to parse the header from raw.
func DecodeTuple ¶
DecodeTuple zero copy decode
func Encode ¶
Encode encode data into frame format
Example ¶
package main import ( "fmt" "github.com/ysmood/byframe" ) func main() { frame := byframe.Encode([]byte("test")) data, _, _ := byframe.Decode(frame) fmt.Println(string(data)) }
Output: test
func EncodeHeader ¶
EncodeHeader encode data length into header
Example ¶
package main import ( "fmt" "github.com/ysmood/byframe" ) func main() { header := byframe.EncodeHeader(1000) dataLen, headerLen, sufficient := byframe.DecodeHeader(header) fmt.Println(headerLen, dataLen, sufficient) }
Output: 2 1000 true
func EncodeTuple ¶
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.
Example ¶
package main import ( "fmt" "github.com/ysmood/byframe" ) func main() { 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
Types ¶
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner scan frames based on the length header
Example ¶
package main import ( "bytes" "fmt" "github.com/ysmood/byframe" ) func main() { frame := byframe.Encode([]byte("test")) s := byframe.NewScanner(bytes.NewReader(frame)) for s.Scan() { fmt.Println(string(s.Frame())) } }
Output: test
Click to show internal directories.
Click to hide internal directories.