-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbencode.go
More file actions
40 lines (35 loc) · 706 Bytes
/
bencode.go
File metadata and controls
40 lines (35 loc) · 706 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package bencode
import (
"bufio"
"bytes"
"io"
"strings"
)
func Decode(obj any, r io.Reader) error {
decodedData, err := decode(r)
if err != nil {
return err
}
return populateObject(obj, decodedData)
}
func Unmarshal(obj any, encodedStr string) error {
decodedData, err := decode(strings.NewReader(encodedStr))
if err != nil {
return err
}
return populateObject(obj, decodedData)
}
func Encode(obj any, w io.Writer) error {
return encode(obj, w)
}
func Marshal(obj any) ([]byte, error) {
var buf bytes.Buffer
bw := bufio.NewWriter(&buf)
if err := encode(obj, bw); err != nil {
return nil, err
}
if err := bw.Flush(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}