-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriting.go
133 lines (105 loc) · 2.8 KB
/
writing.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
package tinytcp
import (
"encoding/binary"
"io"
)
// WriteBytes writes a byte into given writer.
func WriteBytes(writer io.Writer, value []byte) error {
remainingBytes := len(value)
for remainingBytes > 0 {
bytesWritten, err := writer.Write(value[len(value)-remainingBytes:])
if err != nil {
return err
}
remainingBytes -= bytesWritten
}
return nil
}
// WriteByte writes a byte into given writer.
func WriteByte(writer io.Writer, value byte) error {
return WriteBytes(writer, []byte{value})
}
// WriteBool writes a bool into given writer.
func WriteBool(writer io.Writer, value bool) error {
var b byte
if value {
b = 1
}
return WriteByte(writer, b)
}
// WriteInt16 writes int16 into given writer.
func WriteInt16(writer io.Writer, value int16, byteOrder ...binary.ByteOrder) error {
var order binary.ByteOrder = binary.BigEndian
if len(byteOrder) > 0 {
order = byteOrder[0]
}
return binary.Write(writer, order, value)
}
// WriteInt32 writes int32 into given writer.
func WriteInt32(writer io.Writer, value int32, byteOrder ...binary.ByteOrder) error {
var order binary.ByteOrder = binary.BigEndian
if len(byteOrder) > 0 {
order = byteOrder[0]
}
return binary.Write(writer, order, value)
}
// WriteInt64 writes int64 into given writer.
func WriteInt64(writer io.Writer, value int64, byteOrder ...binary.ByteOrder) error {
var order binary.ByteOrder = binary.BigEndian
if len(byteOrder) > 0 {
order = byteOrder[0]
}
return binary.Write(writer, order, value)
}
// WriteFloat32 writes float32 into given writer.
func WriteFloat32(writer io.Writer, value float32, byteOrder ...binary.ByteOrder) error {
var order binary.ByteOrder = binary.BigEndian
if len(byteOrder) > 0 {
order = byteOrder[0]
}
return binary.Write(writer, order, value)
}
// WriteFloat64 writes float64 into given writer.
func WriteFloat64(writer io.Writer, value float64, byteOrder ...binary.ByteOrder) error {
var order binary.ByteOrder = binary.BigEndian
if len(byteOrder) > 0 {
order = byteOrder[0]
}
return binary.Write(writer, order, value)
}
// WriteVarInt writes var int into given writer.
func WriteVarInt(writer io.Writer, value int) error {
for {
if (value & ^segmentBits) == 0 {
err := WriteByte(writer, byte(value))
if err != nil {
return err
}
break
}
err := WriteByte(writer, byte((value&segmentBits)|continueBit))
if err != nil {
return err
}
value >>= 7
}
return nil
}
// WriteVarLong writes var long into given writer.
func WriteVarLong(writer io.Writer, value int64) error {
for {
if (value & ^int64(segmentBits)) == 0 {
err := WriteByte(writer, byte(value))
if err != nil {
return err
}
break
}
err := WriteByte(writer, byte((value&int64(segmentBits))|int64(continueBit)))
if err != nil {
return err
}
value >>= 7
}
return nil
}