lengthprefixed

package module
v0.0.0-...-8a85c1d Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2018 License: MIT Imports: 6 Imported by: 0

README

lengthprefixed

GoDoc Go Report Card Build Status

lengthprefixed implements a Reader and Writer for reading, and writing, data in length-prefixed frames.

This is useful for efficiently streaming, or storing, large quantities of serialized data structures together when they are not self-delimiting, e.g. protocol buffer messages.

A length-prefixed frame adds 9-18 bytes of overhead to user-provided data: a varint representing length of user-provided data, and then an 8-byte (uint64) checksum generated by the xxhash algorithm.

Frame Structure

Each frame contains three parts: length (4 bytes), chunk data (length bytes), and checksum (8 bytes).

0       1-10              N         N+8
+--------+================+----------+
| length |   frame data   | checksum |
+--------+================+----------+

The lengthprefixed API only requires that the user interact with the frame data. The length and checksum components are hidden from the user.

Usage

package main

import (
  "bytes"
  "fmt"

  "github.com/dpirotte/lengthprefixed"
)

func main() {
  var buf bytes.Buffer
  w := lengthprefixed.NewWriter(&buf)

  w.Write([]byte("This string would more likely be a protobuf message."))

  r := lengthprefixed.NewReader(&buf)
  b, err := r.ReadFrame()
  if err != nil {
    panic(err)
  }

  fmt.Println(string(b))
}

Documentation

Overview

Package lengthprefixed implements a Reader and Writer for reading, and writing, data in length-prefixed frames.

Each frame contains three parts: a varint representing the length of the data, N bytes of data, and 8 bytes (uint64) of checksum for the data.

Index

Constants

This section is empty.

Variables

View Source
var ErrChecksum = errors.New("lengthprefixed: invalid checksum")

ErrChecksum is returned when frame data's computed checksum match the provided checksum

Functions

This section is empty.

Types

type Reader

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

A Reader is a wrapper designed to read frames from an underlying io.Reader.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a new Reader wrapping r.

func (*Reader) ReadFrame

func (r *Reader) ReadFrame() (b []byte, err error)

ReadFrame returns the data component of length-prefixed frames. It first reads a varint from the io.Reader to determine how many bytes to return in the byteslice. Then, it reads that many bytes into a byteslice and checksums against the following int64 via xxhash. If the checksum matches, the byteslice will be returned to the caller. Otherwise, an ErrChecksum will be returned.

Note: The caller takes responsibility for watching for io.EOF, which will be bubbled up from the underlying reader.

type Writer

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

Writer implements an io.Writer

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a new Writer writing to w.

func (*Writer) Write

func (w *Writer) Write(b []byte) (int, error)

Write implements the io.Writer interface, but instead of just writing b to the underlying io.Writer, it writes a length-prefixed frame containing a varint (length of b) + b + uint64 (xxhash checksum of b).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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