-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
154 lines (139 loc) · 3.15 KB
/
reader.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
package codec
import (
"time"
)
// Read allows a struct to read from a BinraryCodec as long as it implements
// the Read(*BinaryCodec) function.
func (c *BinaryCodec) Read( i interface{ Read(*BinaryCodec) } ) *BinaryCodec {
if c.err == nil {
i.Read( c )
}
return c
}
// ReadByte reads a single byte
func (c *BinaryCodec) ReadByte( b *byte ) *BinaryCodec {
if c.err == nil {
if t, err := c.buf.ReadByte(); err != nil {
c.err = err
} else {
*b = t
}
}
return c
}
// ReadBytes reads a byte slice
func (c *BinaryCodec) ReadBytes( b *[]byte ) *BinaryCodec {
var l int16
if c.err == nil {
c.ReadInt16( &l )
}
if c.err == nil {
ary := make( []byte, l )
if _, err := c.buf.Read( ary ); err != nil {
c.err = err
} else {
*b = ary
}
}
return c
}
// ReadString reads a string
func (c *BinaryCodec) ReadString( s *string) *BinaryCodec {
var b []byte
if c.err == nil {
c.ReadBytes( &b )
}
if c.err == nil {
*s = string(b[:])
}
return c
}
// ReadStringArray reads an array of strings
func (c *BinaryCodec) ReadStringArray( s *[]string) *BinaryCodec {
var l int16
if c.err == nil {
c.ReadInt16( &l )
}
if c.err == nil {
var a []string = make( []string, l )
for i := 0; i < int(l); i++ {
c.ReadString( &(a[i]) )
}
if c.err == nil {
*s = a
}
}
return c
}
// ReadInt reads an integer. The underlying storage is 64bits in length.
func (c *BinaryCodec) ReadInt( i *int ) *BinaryCodec {
var t int64
c.ReadInt64( &t )
if( c.err == nil ) {
*i = int(t)
}
return c
}
// ReadInt64 reads an int64
func (c *BinaryCodec) ReadInt64( i *int64) *BinaryCodec {
if c.err == nil {
var b []byte = make( []byte, 8 )
if _, err := c.buf.Read( b ); err != nil {
c.err = err
} else {
*i = int64( uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56 )
}
}
return c
}
// ReadInt32 reads an int32
func (c *BinaryCodec) ReadInt32( i *int32 ) *BinaryCodec {
if c.err == nil {
var b []byte = make( []byte, 4 )
if _, err := c.buf.Read( b ); err != nil {
c.err = err
} else {
*i = int32( uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 )
}
}
return c
}
// ReadInt16 reads an int16
func (c *BinaryCodec) ReadInt16( i *int16 ) *BinaryCodec {
if c.err == nil {
var b []byte = make( []byte, 2 )
if _, err := c.buf.Read( b ); err != nil {
c.err = err
} else {
*i = int16( uint16(b[0]) | uint16(b[1])<<8 )
}
}
return c
}
// ReadBool reads a bool. The underlying storage is a single byte
func (c *BinaryCodec) ReadBool( i *bool ) *BinaryCodec {
if c.err == nil {
if b, err := c.buf.ReadByte(); err != nil {
c.err = err
} else {
if b == 0 {
*i = false
} else {
*i = true
}
}
}
return c
}
// ReadTime reads a time.Time value
func (c *BinaryCodec) ReadTime( i *time.Time ) *BinaryCodec {
var b []byte
if c.err == nil {
c.ReadBytes( &b )
}
if c.err == nil {
c.err = i.UnmarshalBinary( b )
}
return c
}