-
Notifications
You must be signed in to change notification settings - Fork 4
/
write.go
162 lines (150 loc) · 3.68 KB
/
write.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package bin
import (
"bytes"
"encoding/binary"
"io"
"github.com/meverselabs/meverse/common/hash"
"github.com/pkg/errors"
)
// WriteUint64 writes the uint64 number to the writer
func WriteUint64(w io.Writer, num uint64) (int64, error) {
BNum := make([]byte, 8)
defer func() {
BNum = nil
}()
binary.LittleEndian.PutUint64(BNum, num)
if n, err := w.Write(BNum); err != nil {
return int64(n), errors.WithStack(err)
} else if n != 8 {
return int64(n), errors.WithStack(ErrInvalidLength)
} else {
return 8, nil
}
}
// WriteUint32 writes the uint32 number to the writer
func WriteUint32(w io.Writer, num uint32) (int64, error) {
BNum := make([]byte, 4)
defer func() {
BNum = nil
}()
binary.LittleEndian.PutUint32(BNum, num)
if n, err := w.Write(BNum); err != nil {
return int64(n), errors.WithStack(err)
} else if n != 4 {
return int64(n), errors.WithStack(ErrInvalidLength)
} else {
return 4, nil
}
}
// WriteUint16 writes the uint16 number to the writer
func WriteUint16(w io.Writer, num uint16) (int64, error) {
BNum := make([]byte, 2)
defer func() {
BNum = nil
}()
binary.LittleEndian.PutUint16(BNum, num)
if n, err := w.Write(BNum); err != nil {
return int64(n), errors.WithStack(err)
} else if n != 2 {
return int64(n), errors.WithStack(ErrInvalidLength)
} else {
return 2, nil
}
}
// WriteUint8 writes the uint8 number to the writer
func WriteUint8(w io.Writer, num uint8) (int64, error) {
if n, err := w.Write([]byte{byte(num)}); err != nil {
return int64(n), errors.WithStack(err)
} else if n != 1 {
return int64(n), errors.WithStack(ErrInvalidLength)
} else {
return 1, nil
}
}
// WriteBytes writes the byte array bytes with the var-length-bytes to the writer
func WriteBytes(w io.Writer, bs []byte) (int64, error) {
var wrote int64
if len(bs) < 254 {
if n, err := WriteUint8(w, uint8(len(bs))); err != nil {
return wrote, err
} else {
wrote += n
}
if n, err := w.Write(bs); err != nil {
return wrote, err
} else {
wrote += int64(n)
}
} else if len(bs) < 65536 {
if n, err := WriteUint8(w, 254); err != nil {
return wrote, err
} else {
wrote += n
}
if n, err := WriteUint16(w, uint16(len(bs))); err != nil {
return wrote, err
} else {
wrote += n
}
if n, err := w.Write(bs); err != nil {
return wrote, err
} else {
wrote += int64(n)
}
} else {
if n, err := WriteUint8(w, 255); err != nil {
return wrote, err
} else {
wrote += n
}
if n, err := WriteUint32(w, uint32(len(bs))); err != nil {
return wrote, err
} else {
wrote += n
}
if n, err := w.Write(bs); err != nil {
return wrote, err
} else {
wrote += int64(n)
}
}
bs = nil
return wrote, nil
}
// WriteString writes the string with the var-length-byte to the writer
func WriteString(w io.Writer, str string) (int64, error) {
return WriteBytes(w, []byte(str))
}
// WriteBool writes the bool using a uint8 to the writer
func WriteBool(w io.Writer, b bool) (int64, error) {
if b {
return WriteUint8(w, 1)
} else {
return WriteUint8(w, 0)
}
}
// WriterToBytes return bytes from writer to
func WriterToBytes(w io.WriterTo) ([]byte, int64, error) {
var buffer bytes.Buffer
if n, err := w.WriteTo(&buffer); err != nil {
return nil, n, errors.WithStack(err)
} else {
return buffer.Bytes(), n, nil
}
}
// WriterToHash return bytes from writer to
func WriterToHash(w io.WriterTo) (hash.Hash256, int64, error) {
bs, n, err := WriterToBytes(w)
if err != nil {
return hash.Hash256{}, n, errors.WithStack(err)
}
return hash.Hash(bs), n, nil
}
// MustWriterToHash return bytes from writer to
func MustWriterToHash(w io.WriterTo) hash.Hash256 {
bs, _, err := WriterToBytes(w)
if err != nil {
panic(err)
}
return hash.Hash(bs)
}