-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_bytes.go
65 lines (56 loc) · 1.6 KB
/
util_bytes.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
package acctelemetry
import (
"bytes"
"encoding/binary"
"fmt"
)
func writeString(buffer *bytes.Buffer, str string) error {
lengthBytes := make([]byte, 2)
binary.LittleEndian.PutUint16(lengthBytes, uint16(len(str)))
var writeErr error
_, writeErr = buffer.Write(lengthBytes)
_, writeErr = buffer.Write([]byte(str))
if writeErr != nil {
return fmt.Errorf("failed to write string '%s' to byte buffer: %w", str, writeErr)
}
return nil
}
func readInt32(buffer *bytes.Buffer) (int32, error) {
readBytes := make([]byte, 4)
_, err := buffer.Read(readBytes)
if err != nil {
return 0, fmt.Errorf("failed to read int32: %w", err)
}
var result int32
err = binary.Read(bytes.NewReader(readBytes), binary.LittleEndian, &result)
if err != nil {
return 0, fmt.Errorf("failed to convert int32: %w", err)
}
return result, nil
}
func readUint16(buffer *bytes.Buffer) (uint16, error) {
readBytes := make([]byte, 2)
_, err := buffer.Read(readBytes)
if err != nil {
return 0, fmt.Errorf("failed to read uint: %w", err)
}
var result uint16
err = binary.Read(bytes.NewReader(readBytes), binary.LittleEndian, &result)
if err != nil {
return 0, fmt.Errorf("failed to convert uint: %w", err)
}
return result, nil
}
func readFloat32(buffer *bytes.Buffer) (float32, error) {
readBytes := make([]byte, 4)
_, err := buffer.Read(readBytes)
if err != nil {
return 0, fmt.Errorf("failed to read float32: %w", err)
}
var result float32
err = binary.Read(bytes.NewReader(readBytes), binary.LittleEndian, &result)
if err != nil {
return 0, fmt.Errorf("failed to convert float32: %w", err)
}
return result, nil
}