-
Notifications
You must be signed in to change notification settings - Fork 31
/
serializer.go
152 lines (135 loc) · 2.63 KB
/
serializer.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package serializer
import (
"encoding/binary"
"io"
"math"
"github.com/pkg/errors"
)
type Serializer struct {
w io.Writer
n int
}
func New(w io.Writer) *Serializer {
return &Serializer{
w: w,
n: 0,
}
}
func (a *Serializer) Write(b []byte) (int, error) {
n, err := a.w.Write(b)
if err != nil {
return 0, err
}
a.n += n
return n, nil
}
func (a *Serializer) StringWithUInt16Len(s string) error {
if len(s) > math.MaxUint16 {
return errors.Errorf("too long string, expected max %d, found %d", math.MaxUint16, len(s))
}
if err := a.Uint16(uint16(len(s))); err != nil {
return err
}
if err := a.String(s); err != nil {
return err
}
return nil
}
// StringWithUInt32Len writes to the buffer `buf` four bytes of the string's `s` length followed with the bytes of string itself.
func (a *Serializer) StringWithUInt32Len(s string) error {
lenStrU32 := uint32(len(s))
maxU32 := uint32(math.MaxUint32)
if lenStrU32 > maxU32 {
return errors.Errorf("too long string, expected max %d, found %d", maxU32, lenStrU32)
}
if err := a.Uint32(lenStrU32); err != nil {
return err
}
if err := a.String(s); err != nil {
return err
}
return nil
}
func (a *Serializer) BytesWithUInt16Len(data []byte) error {
sl := uint16(len(data))
if err := a.Uint16(sl); err != nil {
return err
}
return a.Bytes(data)
}
func (a *Serializer) BytesWithUInt32Len(data []byte) error {
sl := uint32(len(data))
if err := a.Uint32(sl); err != nil {
return err
}
return a.Bytes(data)
}
func (a *Serializer) Uint16(v uint16) error {
buf := [2]byte{}
binary.BigEndian.PutUint16(buf[:], v)
n, err := a.w.Write(buf[:])
if err != nil {
return err
}
a.n += n
return nil
}
func (a *Serializer) Uint32(v uint32) error {
buf := [4]byte{}
binary.BigEndian.PutUint32(buf[:], v)
n, err := a.w.Write(buf[:])
if err != nil {
return err
}
a.n += n
return nil
}
func (a *Serializer) Uint64(v uint64) error {
buf := [8]byte{}
binary.BigEndian.PutUint64(buf[:], v)
n, err := a.w.Write(buf[:])
if err != nil {
return err
}
a.n += n
return nil
}
func (a *Serializer) String(s string) error {
n, err := a.w.Write([]byte(s))
if err != nil {
return err
}
a.n += n
return nil
}
func (a *Serializer) Byte(b byte) error {
n, err := a.w.Write([]byte{b})
if err != nil {
return err
}
a.n += n
return nil
}
func (a *Serializer) N() int64 {
return int64(a.n)
}
func (a *Serializer) Bool(b bool) error {
var v byte = 0
if b {
v = 1
}
n, err := a.w.Write([]byte{v})
if err != nil {
return err
}
a.n += n
return nil
}
func (a *Serializer) Bytes(b []byte) error {
n, err := a.w.Write(b)
if err != nil {
return err
}
a.n += n
return nil
}