-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathencoder.go
191 lines (162 loc) · 4.05 KB
/
encoder.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package binary
import (
"bytes"
"encoding/binary"
"io"
"math"
"reflect"
"sync"
)
// Reusable long-lived encoder pool.
var encoders = &sync.Pool{New: func() interface{} {
return &Encoder{
schemas: make(map[reflect.Type]Codec),
}
}}
// Marshal encodes the payload into binary format.
func Marshal(v interface{}) (output []byte, err error) {
var buffer bytes.Buffer
buffer.Grow(64)
// Encode and set the buffer if successful
if err = MarshalTo(v, &buffer); err == nil {
output = buffer.Bytes()
}
return
}
// MarshalTo encodes the payload into a specific destination.
func MarshalTo(v interface{}, dst io.Writer) (err error) {
// Get the encoder from the pool, reset it
e := encoders.Get().(*Encoder)
e.Reset(dst)
// Encode and set the buffer if successful
err = e.Encode(v)
// Put the encoder back when we're finished
encoders.Put(e)
return
}
// Encoder represents a binary encoder.
type Encoder struct {
scratch [10]byte
schemas map[reflect.Type]Codec
out io.Writer
err error
}
// NewEncoder creates a new encoder.
func NewEncoder(out io.Writer) *Encoder {
return &Encoder{
out: out,
schemas: make(map[reflect.Type]Codec),
}
}
// Reset resets the encoder and makes it ready to be reused.
func (e *Encoder) Reset(out io.Writer) {
e.out = out
e.err = nil
}
// Buffer returns the underlying writer.
func (e *Encoder) Buffer() io.Writer {
return e.out
}
// Encode encodes the value to the binary format.
func (e *Encoder) Encode(v interface{}) (err error) {
// Scan the type (this will load from cache)
rv := reflect.Indirect(reflect.ValueOf(v))
var c Codec
if c, err = scanToCache(rv.Type(), e.schemas); err != nil {
return
}
// Encode the value
if err = c.EncodeTo(e, rv); err == nil {
err = e.err
}
return
}
// Write writes the contents of p into the buffer.
func (e *Encoder) Write(p []byte) {
if e.err == nil {
_, e.err = e.out.Write(p)
}
}
// WriteVarint writes a variable size integer
func (e *Encoder) WriteVarint(v int64) {
x := uint64(v) << 1
if v < 0 {
x = ^x
}
i := 0
for x >= 0x80 {
e.scratch[i] = byte(x) | 0x80
x >>= 7
i++
}
e.scratch[i] = byte(x)
e.Write(e.scratch[:(i + 1)])
}
// WriteUvarint writes a variable size unsigned integer
func (e *Encoder) WriteUvarint(x uint64) {
i := 0
for x >= 0x80 {
e.scratch[i] = byte(x) | 0x80
x >>= 7
i++
}
e.scratch[i] = byte(x)
e.Write(e.scratch[:(i + 1)])
}
// WriteUint16 writes a Uint16
func (e *Encoder) WriteUint16(v uint16) {
e.scratch[0] = byte(v)
e.scratch[1] = byte(v >> 8)
e.Write(e.scratch[:2])
}
// WriteUint32 writes a Uint32
func (e *Encoder) WriteUint32(v uint32) {
e.scratch[0] = byte(v)
e.scratch[1] = byte(v >> 8)
e.scratch[2] = byte(v >> 16)
e.scratch[3] = byte(v >> 24)
e.Write(e.scratch[:4])
}
// WriteUint64 writes a Uint64
func (e *Encoder) WriteUint64(v uint64) {
e.scratch[0] = byte(v)
e.scratch[1] = byte(v >> 8)
e.scratch[2] = byte(v >> 16)
e.scratch[3] = byte(v >> 24)
e.scratch[4] = byte(v >> 32)
e.scratch[5] = byte(v >> 40)
e.scratch[6] = byte(v >> 48)
e.scratch[7] = byte(v >> 56)
e.Write(e.scratch[:8])
}
// WriteFloat32 a 32-bit floating point number
func (e *Encoder) WriteFloat32(v float32) {
e.WriteUint32(math.Float32bits(v))
}
// WriteFloat64 a 64-bit floating point number
func (e *Encoder) WriteFloat64(v float64) {
e.WriteUint64(math.Float64bits(v))
}
// WriteBool writes a single boolean value into the buffer
func (e *Encoder) writeBool(v bool) {
e.scratch[0] = 0
if v {
e.scratch[0] = 1
}
e.Write(e.scratch[:1])
}
// Writes a complex number
func (e *Encoder) writeComplex64(v complex64) {
e.err = binary.Write(e.out, binary.LittleEndian, v)
}
// Writes a complex number
func (e *Encoder) writeComplex128(v complex128) {
e.err = binary.Write(e.out, binary.LittleEndian, v)
}
// WriteString writes a string prefixed with a variable-size integer size.
func (e *Encoder) WriteString(v string) {
e.WriteUvarint(uint64(len(v)))
e.Write(ToBytes(v))
}