-
Notifications
You must be signed in to change notification settings - Fork 470
/
bytes_reader.go
53 lines (44 loc) · 1.04 KB
/
bytes_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
package vmutils
import (
"encoding/binary"
)
type BytesReader struct {
byteOrder binary.ByteOrder
data []byte
position int
}
func NewBytesReader(data []byte, byteOrder binary.ByteOrder) BytesReader {
return BytesReader{
byteOrder: byteOrder,
data: data,
position: 0,
}
}
func (reader *BytesReader) Position() int {
return reader.position
}
func (reader *BytesReader) ReadUint8() uint8 {
i := reader.data[reader.position]
reader.position++
return i
}
func (reader *BytesReader) ReadUint16() uint16 {
i := reader.byteOrder.Uint16(reader.data[reader.position:])
reader.position += 2
return i
}
func (reader *BytesReader) ReadUint32() uint32 {
i := reader.byteOrder.Uint32(reader.data[reader.position:])
reader.position += 4
return i
}
func (reader *BytesReader) ReadUint64() uint64 {
i := reader.byteOrder.Uint64(reader.data[reader.position:])
reader.position += 8
return i
}
func (reader *BytesReader) ReadBytes(n int) []byte {
bytes := reader.data[reader.position : reader.position+n]
reader.position += n
return bytes
}