-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathencoder.go
62 lines (50 loc) · 1.08 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
package goloxi
import (
"bytes"
"encoding/binary"
)
type Encoder struct {
buffer *bytes.Buffer
}
func NewEncoder() *Encoder {
return &Encoder{
buffer: new(bytes.Buffer),
}
}
func (e *Encoder) PutChar(c byte) {
e.buffer.WriteByte(c)
}
func (e *Encoder) PutUint8(i uint8) {
e.buffer.WriteByte(i)
}
func (e *Encoder) PutUint16(i uint16) {
var tmp [2]byte
binary.BigEndian.PutUint16(tmp[0:2], i)
e.buffer.Write(tmp[:])
}
func (e *Encoder) PutUint32(i uint32) {
var tmp [4]byte
binary.BigEndian.PutUint32(tmp[0:4], i)
e.buffer.Write(tmp[:])
}
func (e *Encoder) PutUint64(i uint64) {
var tmp [8]byte
binary.BigEndian.PutUint64(tmp[0:8], i)
e.buffer.Write(tmp[:])
}
func (e *Encoder) PutUint128(i Uint128) {
var tmp [16]byte
binary.BigEndian.PutUint64(tmp[0:8], i.Hi)
binary.BigEndian.PutUint64(tmp[8:16], i.Lo)
e.buffer.Write(tmp[:])
}
func (e *Encoder) Write(b []byte) {
e.buffer.Write(b)
}
func (e *Encoder) Bytes() []byte {
return e.buffer.Bytes()
}
func (e *Encoder) SkipAlign() {
length := len(e.buffer.Bytes())
e.Write(bytes.Repeat([]byte{0}, (length+7)/8*8-length))
}