/
buffer_reader.go
64 lines (54 loc) · 1.37 KB
/
buffer_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
package stream
import (
"bufio"
"encoding/binary"
"io"
)
func readUShort(readerStream io.Reader) uint16 {
var res uint16
_ = binary.Read(readerStream, binary.BigEndian, &res)
return res
}
func readShort(readerStream io.Reader) int16 {
var res int16
_ = binary.Read(readerStream, binary.BigEndian, &res)
return res
}
func readUInt(readerStream io.Reader) (uint32, error) {
var res uint32
err := binary.Read(readerStream, binary.BigEndian, &res)
return res, err
}
func peekByte(readerStream *bufio.Reader) (uint8, error) {
res, err := readerStream.Peek(1)
if err != nil {
return 0, err
}
return res[0], nil
}
func readInt64(readerStream io.Reader) int64 {
var res int64
_ = binary.Read(readerStream, binary.BigEndian, &res)
return res
}
func readByte(readerStream io.Reader) uint8 {
var res uint8
_ = binary.Read(readerStream, binary.BigEndian, &res)
return res
}
func readByteError(readerStream io.Reader) (uint8, error) {
var res uint8
err := binary.Read(readerStream, binary.BigEndian, &res)
return res, err
}
func readString(readerStream io.Reader) string {
lenString := readUShort(readerStream)
buff := make([]byte, lenString)
_ = binary.Read(readerStream, binary.BigEndian, &buff)
return string(buff)
}
func readUint8Array(readerStream io.Reader, size uint32) []uint8 {
var res = make([]uint8, size)
_, _ = io.ReadFull(readerStream, res)
return res
}