This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtbs.go
208 lines (169 loc) · 3.9 KB
/
tbs.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package yoo
import (
"math"
"errors"
"encoding/binary"
)
type ByteArray struct {
buf []byte
posWrite int
posRead int
endian binary.ByteOrder
}
var ByteArrayEndian binary.ByteOrder = binary.BigEndian
func CreateByteArray(bytes []byte) *ByteArray {
var ba *ByteArray
if len(bytes) > 0 {
ba = &ByteArray{ buf: bytes }
} else {
ba = &ByteArray{}
}
ba.endian = binary.BigEndian
return ba
}
func (this *ByteArray) Length() int {
return len(this.buf)
}
func (this *ByteArray) Available() int {
return this.Length() - this.posRead
}
func (this *ByteArray) SetEndian(endian binary.ByteOrder) {
this.endian = endian
}
func (this *ByteArray) GetEndian() binary.ByteOrder {
if this.endian == nil {
return ByteArrayEndian
}
return this.endian
}
func (this *ByteArray) SetWritePos(pos int) error {
if pos > this.Length() {
this.posWrite = this.Length()
return errors.New("Buffer end")
} else {
this.posWrite = pos
}
return nil
}
func (this *ByteArray) SetWriteEnd() {
this.SetWritePos(this.Length())
}
func (this *ByteArray) GetWritePos() int {
return this.posWrite
}
func (this *ByteArray) SetReadPos(pos int) error {
if pos > this.Length() {
this.posRead = this.Length()
return errors.New("Buffer end")
} else {
this.posRead = pos
}
return nil
}
func (this *ByteArray) SetReadEnd() {
this.SetReadPos(this.Length())
}
func (this *ByteArray) GetReadPos() int {
return this.posRead
}
func (this *ByteArray) Seek(pos int) error {
err := this.SetWritePos(pos)
this.SetReadPos(pos)
return err
}
func (this *ByteArray) Reset() {
this.buf = []byte{}
this.Seek(0)
}
func (this *ByteArray) Bytes() []byte {
return this.buf
}
func (this *ByteArray) BytesAvailable() []byte {
return this.buf[this.posRead:]
}
func (this *ByteArray) Read(bytes []byte) (l int, err error) {
if len(bytes) == 0 {
return
}
if len(bytes) > this.Length() - this.posRead{
return 0, errors.New("Buffer end")
}
l = copy(bytes, this.buf[this.posRead:])
this.posRead += l
return l, nil
}
func (this *ByteArray) ReadBytes(bytes []byte, length int, offset int) (l int, err error) {
return this.Read(bytes[offset:offset + length])
}
func (this *ByteArray) ReadByte() (b byte, err error) {
bytes := make([]byte, 1)
_, err = this.ReadBytes(bytes, 1, 0)
if err == nil{
b = bytes[0]
}
return
}
func (this *ByteArray) ReadLength() (ret int, err error) {
i := 0
for _tk, b := true, 0; _tk || (err == nil && b == 255); _tk = false {
b, err = this.ReadByte()
}
ret = int(math.Pow(256, i)) - 1 + b
return
}
func (this *ByteArray) ReadInt8() (ret int8, err error) {
err = binary.Read(this, this.endian, &ret)
return
}
func (this *ByteArray) ReadInt16() (ret int16, err error) {
err = binary.Read(this, this.endian, &ret)
return
}
func (this *ByteArray) ReadInt32() (ret int32, err error) {
err = binary.Read(this, this.endian, &ret)
return
}
func (this *ByteArray) ReadInt64() (ret int64, err error) {
err = binary.Read(this, this.endian, &ret)
return
}
func (this *ByteArray) ReadFloat32() (ret float32, err error) {
err = binary.Read(this, this.endian, &ret)
return
}
func (this *ByteArray) ReadFloat64() (ret float64, err error) {
err = binary.Read(this, this.endian, &ret)
return
}
func (this *ByteArray) ReadBool() (ret bool, err error) {
var bb byte
bb, err = this.ReadByte()
if err == nil{
if bb == 1 {
ret = true
} else {
ret = false
}
} else {
ret = false
}
return
}
func (this *ByteArray) ReadString(length int) (ret string, err error) {
bytes := make([]byte, length, length)
_, err = this.ReadBytes(bytes, length, 0)
if err == nil{
ret = string(bytes)
} else {
ret = "";
}
return
}
func (this *ByteArray) ReadUTF() (ret string, err error) {
var l int16
l, err = this.ReadInt16()
if err != nil{
return "", err
}
return this.ReadString(int(l))
}