-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarywriter.go
More file actions
40 lines (34 loc) · 1.08 KB
/
binarywriter.go
File metadata and controls
40 lines (34 loc) · 1.08 KB
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
package binaryreaderwriter
import (
"encoding/binary"
"io"
)
//Writer struct to create simple binaryWriter
type Writer struct {
ByteStream io.Writer
Endianess binary.ByteOrder
}
//BinaryWriter Returns instance of Writer
func BinaryWriter(ByteStream io.Writer) *Writer {
return &Writer{ByteStream: ByteStream, Endianess: binary.LittleEndian}
}
//WriteInt32 write an int32 to Writer.ByteStream
func (Writer *Writer) WriteInt32(i int32) {
binary.Write(Writer.ByteStream, Writer.Endianess, i)
}
//Write7BitEncodedInt write a int32 as a 7bit encoded int to Writer.ByteStream
func (Writer *Writer) Write7BitEncodedInt(i int32) {
b := uint32(i)
for b > 0x80 {
binary.Write(Writer.ByteStream, Writer.Endianess, byte(b|0x80))
b = b >> 7
}
binary.Write(Writer.ByteStream, Writer.Endianess, byte(b))
}
//WriteString write a string prefixed with the length as a 7bit encoded int to Writer.ByteStream
func (Writer *Writer) WriteString(s string) {
size := len(s)
Writer.Write7BitEncodedInt(int32(size))
//binary.Write(Writer.ByteStream, Writer.Endianess, s)
Writer.ByteStream.Write([]byte(s))
}