forked from libgox/buffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer_int.go
78 lines (65 loc) · 1.63 KB
/
buffer_int.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
package buffer
import "encoding/binary"
func (b *Buffer) ReadInt16() (int16, error) {
bytes, err := b.ReadNBytes(2)
if err != nil {
return 0, err
}
return int16(binary.BigEndian.Uint16(bytes)), nil
}
func (b *Buffer) WriteInt16(x int16) error {
return b.WriteUInt16(uint16(x))
}
func (b *Buffer) ReadUInt16() (uint16, error) {
bytes, err := b.ReadNBytes(2)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint16(bytes), nil
}
func (b *Buffer) WriteUInt16(x uint16) error {
binary.BigEndian.PutUint16(b.WritableSlice(), x)
return b.AdjustWriteCursor(2)
}
func (b *Buffer) ReadInt32() (int32, error) {
bytes, err := b.ReadNBytes(4)
if err != nil {
return 0, err
}
return int32(binary.BigEndian.Uint32(bytes)), nil
}
func (b *Buffer) WriteInt32(x int32) error {
return b.WriteUInt32(uint32(x))
}
func (b *Buffer) ReadUInt32() (uint32, error) {
bytes, err := b.ReadNBytes(4)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint32(bytes), nil
}
func (b *Buffer) WriteUInt32(x uint32) error {
binary.BigEndian.PutUint32(b.WritableSlice(), x)
return b.AdjustWriteCursor(4)
}
func (b *Buffer) ReadInt64() (int64, error) {
bytes, err := b.ReadNBytes(8)
if err != nil {
return 0, err
}
return int64(binary.BigEndian.Uint64(bytes)), nil
}
func (b *Buffer) WriteInt64(x int64) error {
return b.WriteUInt64(uint64(x))
}
func (b *Buffer) ReadUInt64() (uint64, error) {
bytes, err := b.ReadNBytes(8)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint64(bytes), nil
}
func (b *Buffer) WriteUInt64(x uint64) error {
binary.BigEndian.PutUint64(b.WritableSlice(), x)
return b.AdjustWriteCursor(8)
}