| 1 |
package binary |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"io" |
| 6 |
) |
| 7 |
|
| 8 |
// Write writes the binary representation of data into w, using BigEndian order |
| 9 |
// https://golang.org/pkg/encoding/binary/#Write |
| 10 |
func Write(w io.Writer, data ...interface{}) error { |
| 11 |
for _, v := range data { |
| 12 |
if err := binary.Write(w, binary.BigEndian, v); err != nil { |
| 13 |
return err |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
return nil |
| 18 |
} |
| 19 |
|
| 20 |
func WriteVariableWidthInt(w io.Writer, n int64) error { |
| 21 |
buf := []byte{byte(n & 0x7f)} |
| 22 |
n >>= 7 |
| 23 |
for n != 0 { |
| 24 |
n-- |
| 25 |
buf = append([]byte{0x80 | (byte(n & 0x7f))}, buf...) |
| 26 |
n >>= 7 |
| 27 |
} |
| 28 |
|
| 29 |
_, err := w.Write(buf) |
| 30 |
|
| 31 |
return err |
| 32 |
} |
| 33 |
|
| 34 |
// WriteUint64 writes the binary representation of a uint64 into w, in BigEndian |
| 35 |
// order |
| 36 |
func WriteUint64(w io.Writer, value uint64) error { |
| 37 |
return binary.Write(w, binary.BigEndian, value) |
| 38 |
} |
| 39 |
|
| 40 |
// WriteUint32 writes the binary representation of a uint32 into w, in BigEndian |
| 41 |
// order |
| 42 |
func WriteUint32(w io.Writer, value uint32) error { |
| 43 |
return binary.Write(w, binary.BigEndian, value) |
| 44 |
} |
| 45 |
|
| 46 |
// WriteUint16 writes the binary representation of a uint16 into w, in BigEndian |
| 47 |
// order |
| 48 |
func WriteUint16(w io.Writer, value uint16) error { |
| 49 |
return binary.Write(w, binary.BigEndian, value) |
| 50 |
} |