Skip to content

Commit 8ce5cb1

Browse files
committed
added io.{Reader, Writer} support
1 parent 4f07d71 commit 8ce5cb1

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

β€Žio.goβ€Ž

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package multihash
2+
3+
import (
4+
"fmt"
5+
"io"
6+
)
7+
8+
// Reader is an io.Reader wrapper that exposes a function
9+
// to read a whole multihash, parse it, and return it.
10+
type Reader interface {
11+
io.Reader
12+
13+
ReadMultihash() (Multihash, error)
14+
}
15+
16+
// Writer is an io.Writer wrapper that exposes a function
17+
// to write a whole multihash.
18+
type Writer interface {
19+
io.Writer
20+
21+
WriteMultihash(Multihash) error
22+
}
23+
24+
// NewReader wraps an io.Reader with a multihash.Reader
25+
func NewReader(r io.Reader) Reader {
26+
return &mhReader{r}
27+
}
28+
29+
// NewWriter wraps an io.Writer with a multihash.Writer
30+
func NewWriter(w io.Writer) Writer {
31+
return &mhWriter{w}
32+
}
33+
34+
type mhReader struct {
35+
r io.Reader
36+
}
37+
38+
func (r *mhReader) Read(buf []byte) (n int, err error) {
39+
return r.r.Read(buf)
40+
}
41+
42+
func (r *mhReader) ReadMultihash() (Multihash, error) {
43+
mhhdr := make([]byte, 2)
44+
if _, err := io.ReadFull(r.r, mhhdr); err != nil {
45+
return nil, err
46+
}
47+
48+
// first byte is the algo, the second is the length.
49+
50+
// (varints someday...)
51+
length := uint(mhhdr[1])
52+
53+
if length > 127 {
54+
return nil, fmt.Errorf("varints not yet supported (length is %d)", length)
55+
}
56+
57+
buf := make([]byte, length+2)
58+
buf[0] = mhhdr[0]
59+
buf[1] = mhhdr[1]
60+
61+
if _, err := io.ReadFull(r.r, buf[2:]); err != nil {
62+
return nil, err
63+
}
64+
65+
return Cast(buf)
66+
}
67+
68+
type mhWriter struct {
69+
w io.Writer
70+
}
71+
72+
func (w *mhWriter) Write(buf []byte) (n int, err error) {
73+
return w.w.Write(buf)
74+
}
75+
76+
func (w *mhWriter) WriteMultihash(m Multihash) error {
77+
_, err := w.w.Write([]byte(m))
78+
return err
79+
}

β€Žio_test.goβ€Ž

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package multihash
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"testing"
7+
)
8+
9+
func TestReader(t *testing.T) {
10+
11+
var buf bytes.Buffer
12+
13+
for _, tc := range testCases {
14+
m, err := tc.Multihash()
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
19+
buf.Write([]byte(m))
20+
}
21+
22+
r := NewReader(&buf)
23+
24+
for _, tc := range testCases {
25+
h, err := tc.Multihash()
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
30+
h2, err := r.ReadMultihash()
31+
if err != nil {
32+
t.Error(err)
33+
continue
34+
}
35+
36+
if !bytes.Equal(h, h2) {
37+
t.Error("h and h2 should be equal")
38+
}
39+
}
40+
}
41+
42+
func TestWriter(t *testing.T) {
43+
44+
var buf bytes.Buffer
45+
w := NewWriter(&buf)
46+
47+
for _, tc := range testCases {
48+
m, err := tc.Multihash()
49+
if err != nil {
50+
t.Error(err)
51+
continue
52+
}
53+
54+
if err := w.WriteMultihash(m); err != nil {
55+
t.Error(err)
56+
continue
57+
}
58+
59+
buf2 := make([]byte, len(m))
60+
if _, err := io.ReadFull(&buf, buf2); err != nil {
61+
t.Error(err)
62+
continue
63+
}
64+
65+
if !bytes.Equal(m, buf2) {
66+
t.Error("m and buf2 should be equal")
67+
}
68+
}
69+
}

0 commit comments

Comments
Β (0)