-
Notifications
You must be signed in to change notification settings - Fork 25
/
io.go
101 lines (79 loc) · 1.7 KB
/
io.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package typegen
import (
"io"
)
var (
_ io.Reader = (*CborReader)(nil)
_ io.ByteScanner = (*CborReader)(nil)
)
type CborReader struct {
r BytePeeker
hbuf [maxHeaderSize]byte
}
func NewCborReader(r io.Reader) *CborReader {
if r, ok := r.(*CborReader); ok {
return r
}
return &CborReader{
r: GetPeeker(r),
}
}
func (cr *CborReader) Read(p []byte) (n int, err error) {
return cr.r.Read(p)
}
func (cr *CborReader) ReadByte() (byte, error) {
return cr.r.ReadByte()
}
func (cr *CborReader) UnreadByte() error {
return cr.r.UnreadByte()
}
func (cr *CborReader) ReadHeader() (byte, uint64, error) {
return CborReadHeaderBuf(cr.r, cr.hbuf[:])
}
func (cr *CborReader) SetReader(r io.Reader) {
cr.r = GetPeeker(r)
}
var (
_ io.Writer = (*CborWriter)(nil)
_ io.StringWriter = (*CborWriter)(nil)
)
type CborWriter struct {
w io.Writer
hbuf [maxHeaderSize]byte
sw io.StringWriter
}
func NewCborWriter(w io.Writer) *CborWriter {
if w, ok := w.(*CborWriter); ok {
return w
}
cw := &CborWriter{
w: w,
}
if sw, ok := w.(io.StringWriter); ok {
cw.sw = sw
}
return cw
}
func (cw *CborWriter) SetWriter(w io.Writer) {
cw.w = w
if sw, ok := w.(io.StringWriter); ok {
cw.sw = sw
} else {
cw.sw = nil
}
}
func (cw *CborWriter) Write(p []byte) (n int, err error) {
return cw.w.Write(p)
}
func (cw *CborWriter) WriteMajorTypeHeader(t byte, l uint64) error {
return WriteMajorTypeHeaderBuf(cw.hbuf[:], cw.w, t, l)
}
func (cw *CborWriter) CborWriteHeader(t byte, l uint64) error {
return WriteMajorTypeHeaderBuf(cw.hbuf[:], cw.w, t, l)
}
func (cw *CborWriter) WriteString(s string) (int, error) {
if cw.sw != nil {
return cw.sw.WriteString(s)
}
return cw.w.Write([]byte(s))
}