bencode: github.com/zeebo/bencode Index | Examples | Files

package bencode

import "github.com/zeebo/bencode"

Package bencode implements encoding and decoding of bencoded objects.

It has a similar API to the encoding/json package and many other serialization formats.

Index

Examples

Package Files

decode.go doc.go encode.go raw.go tag.go tracer.go

func DecodeBytes Uses

func DecodeBytes(b []byte, val interface{}) error

DecodeBytes reads the data in b and stores it into the value pointed to by val. Read the docs for Decode for more information.

Code:

var torrent interface{}
if err := DecodeBytes([]byte(data), &torrent); err != nil {
    panic(err)
}

func DecodeString Uses

func DecodeString(in string, val interface{}) error

DecodeString reads the data in the string and stores it into the value pointed to by val. Read the docs for Decode for more information.

Code:

var torrent interface{}
if err := DecodeString(data, &torrent); err != nil {
    panic(err)
}

func EncodeBytes Uses

func EncodeBytes(val interface{}) ([]byte, error)

EncodeBytes returns the bencoded data of val as a slice of bytes.

Code:

var torrent interface{}
data, err := EncodeBytes(torrent)
if err != nil {
    panic(err)
}
fmt.Println(data)

func EncodeString Uses

func EncodeString(val interface{}) (string, error)

EncodeString returns the bencoded data of val as a string.

Code:

var torrent interface{}
data, err := EncodeString(torrent)
if err != nil {
    panic(err)
}
fmt.Println(data)

type Decoder Uses

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

A Decoder reads and decodes bencoded data from an input stream.

func NewDecoder Uses

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r

func (*Decoder) BytesParsed Uses

func (d *Decoder) BytesParsed() int

BytesParsed returns the number of bytes that have actually been parsed

func (*Decoder) Decode Uses

func (d *Decoder) Decode(val interface{}) error

Decode reads the bencoded value from its input and stores it in the value pointed to by val. Decode allocates maps/slices as necessary with the following additional rules: To decode a bencoded value into a nil interface value, the type stored in the interface value is one of:

int64 for bencoded integers
string for bencoded strings
[]interface{} for bencoded lists
map[string]interface{} for bencoded dicts

To unmarshal bencode into a value implementing the Unmarshaler interface, Unmarshal calls that value's UnmarshalBencode method. Otherwise, if the value implements encoding.TextUnmarshaler and the input is a bencode string, Unmarshal calls that value's UnmarshalText method with the decoded form of the string.

Code:

dec := NewDecoder(r)
var torrent struct {
    Announce string
    List     [][]string `bencode:"announce-list"`
}
if err := dec.Decode(&torrent); err != nil {
    panic(err)
}

func (*Decoder) SetFailOnUnorderedKeys Uses

func (d *Decoder) SetFailOnUnorderedKeys(fail bool)

SetFailOnUnorderedKeys will cause the decoder to fail when encountering unordered keys. The default is to not fail.

type Encoder Uses

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

An Encoder writes bencoded objects to an output stream.

func NewEncoder Uses

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode Uses

func (e *Encoder) Encode(val interface{}) error

Encode writes the bencoded data of val to its output stream. If an encountered value implements the Marshaler interface, its MarshalBencode method is called to produce the bencode output for this value. If no MarshalBencode method is present but the value implements encoding.TextMarshaler instead, its MarshalText method is called, which encodes the result as a bencode string. See the documentation for Decode about the conversion of Go values to bencoded data.

Code:

var x struct {
    Foo string
    Bar []string `bencode:"name"`
}

enc := NewEncoder(w)
if err := enc.Encode(x); err != nil {
    panic(err)
}

type Marshaler Uses

type Marshaler interface {
    MarshalBencode() ([]byte, error)
}

Marshaler is the interface implemented by types that can marshal themselves into valid bencode.

type RawMessage Uses

type RawMessage []byte

RawMessage is a special type that will store the raw bencode data when encoding or decoding.

type Unmarshaler Uses

type Unmarshaler interface {
    UnmarshalBencode([]byte) error
}

Unmarshaler is the interface implemented by types that can unmarshal a bencode description of themselves. The input can be assumed to be a valid encoding of a bencode value. UnmarshalBencode must copy the bencode data if it wishes to retain the data after returning.

Package bencode imports 12 packages (graph) and is imported by 61 packages. Updated 2018-08-19. Refresh now. Tools for package owners.