forked from perlin-network/noise
-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.go
75 lines (56 loc) · 1.17 KB
/
writer.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
package payload
import (
"bytes"
"encoding/binary"
"io"
)
var _ io.Writer = (*Writer)(nil)
type Writer struct {
buffer *bytes.Buffer
}
func NewWriter(buf []byte) Writer {
return Writer{
buffer: bytes.NewBuffer(buf),
}
}
// Len returns the number of bytes written so far.
func (b Writer) Len() int {
return b.buffer.Len()
}
func (b Writer) Bytes() []byte {
return b.buffer.Bytes()
}
func (b Writer) Write(buf []byte) (n int, err error) {
return b.buffer.Write(buf)
}
func (b Writer) WriteBytes(buf []byte) Writer {
b.WriteUint32(uint32(len(buf)))
_, _ = b.Write(buf)
return b
}
func (b Writer) WriteString(x string) Writer {
b.WriteBytes([]byte(x))
return b
}
func (b Writer) WriteByte(x byte) Writer {
b.buffer.WriteByte(x)
return b
}
func (b Writer) WriteUint16(x uint16) Writer {
var buf [2]byte
binary.LittleEndian.PutUint16(buf[:], x)
_, _ = b.Write(buf[:])
return b
}
func (b Writer) WriteUint32(x uint32) Writer {
var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], x)
_, _ = b.Write(buf[:])
return b
}
func (b Writer) WriteUint64(x uint64) Writer {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], x)
_, _ = b.Write(buf[:])
return b
}