/
bufdagreader.go
49 lines (41 loc) · 1.03 KB
/
bufdagreader.go
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
41
42
43
44
45
46
47
48
49
package io
import (
"bytes"
"context"
"io"
)
// BufDagReader implements a DagReader that reads from a byte slice
// using a bytes.Reader. It is used for RawNodes.
type BufDagReader struct {
*bytes.Reader
}
// NewBufDagReader returns a DAG reader for the given byte slice.
// BufDagReader is used to read RawNodes.
func NewBufDagReader(b []byte) *BufDagReader {
return &BufDagReader{bytes.NewReader(b)}
}
var _ DagReader = (*BufDagReader)(nil)
// Close is a nop.
func (*BufDagReader) Close() error {
return nil
}
// CtxReadFull reads the slice onto b.
func (rd *BufDagReader) CtxReadFull(ctx context.Context, b []byte) (int, error) {
return rd.Read(b)
}
// Offset returns the current offset.
func (rd *BufDagReader) Offset() int64 {
of, err := rd.Seek(0, io.SeekCurrent)
if err != nil {
panic("this should never happen " + err.Error())
}
return of
}
// Size returns the size of the buffer.
func (rd *BufDagReader) Size() uint64 {
s := rd.Reader.Size()
if s < 0 {
panic("size smaller than 0 (impossible!!)")
}
return uint64(s)
}