-
Notifications
You must be signed in to change notification settings - Fork 17
/
binary_v2.go
44 lines (41 loc) · 1.28 KB
/
binary_v2.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
package turbo
import (
b "encoding/binary"
"io"
)
// Read reads structured binary data from r into data.
// Data must be a pointer to a fixed-size value or a slice
// of fixed-size values.
// Bytes read from r are decoded using the specified byte order
// and written to successive fields of the data.
// When reading into structs, the field data for fields with
// blank (_) field names is skipped; i.e., blank field names
// may be used for padding.
// When reading into a struct, all non-blank fields must be exported.
func Read(r io.Reader, order b.ByteOrder, data interface{}) error {
arr, ok := data.([]uint8)
if ok {
if _, err := io.ReadFull(r, arr); err != nil {
return err
}
return nil
} else {
return b.Read(r, order, data)
}
}
// Write writes the binary representation of data into w.
// Data must be a fixed-size value or a slice of fixed-size
// values, or a pointer to such data.
// Bytes written to w are encoded using the specified byte order
// and read from successive fields of the data.
// When writing structs, zero values are written for fields
// with blank (_) field names.
func Write(w io.Writer, order b.ByteOrder, data interface{}) error {
arr, ok := data.([]uint8)
if ok {
_, err := w.Write(arr)
return err
} else {
return b.Write(w, order, data)
}
}