-
Notifications
You must be signed in to change notification settings - Fork 277
/
buf.go
228 lines (195 loc) · 5.28 KB
/
buf.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bytes
import (
"encoding/binary"
"io"
gxbytes "github.com/dubbogo/gost/bytes"
)
type ByteBuffer struct {
buf *gxbytes.Buffer
}
func NewByteBuffer(buf []byte) *ByteBuffer {
return &ByteBuffer{
buf: gxbytes.NewBuffer(buf),
}
}
func (b *ByteBuffer) Bytes() []byte {
return b.buf.Bytes()
}
func (b *ByteBuffer) Read(p []byte) (n int, err error) {
return b.buf.Read(p)
}
func (b *ByteBuffer) ReadByte() (byte, error) {
data := make([]byte, 1)
n, err := b.Read(data)
if err != nil {
return 0, err
}
if n < 1 {
return 0, io.ErrShortBuffer
}
return data[0], nil
}
func (b *ByteBuffer) ReadInt64() (int64, error) {
data := make([]byte, 8)
n, err := b.Read(data)
if err != nil {
return 0, err
}
if n < 8 {
return 0, io.ErrShortBuffer
}
return Byte2Int64(data), nil
}
func (b *ByteBuffer) ReadUint16() (uint16, error) {
data := make([]byte, 2)
n, err := b.Read(data)
if err != nil {
return 0, err
}
if n < 2 {
return 0, io.ErrShortBuffer
}
return Byte2UInt16(data), nil
}
func (b *ByteBuffer) ReadUint32() (uint32, error) {
data := make([]byte, 4)
n, err := b.Read(data)
if err != nil {
return 0, err
}
if n < 2 {
return 0, io.ErrShortBuffer
}
return Byte2UInt32(data), nil
}
func (b *ByteBuffer) ReadUint64() (uint64, error) {
data := make([]byte, 8)
n, err := b.Read(data)
if err != nil {
return 0, err
}
if n < 2 {
return 0, io.ErrShortBuffer
}
return Byte2UInt64(data), nil
}
func (b *ByteBuffer) Write(p []byte) (n int, err error) {
return b.buf.Write(p)
}
func (b *ByteBuffer) WriteString(str string) (n int, err error) {
return b.buf.Write([]byte(str))
}
func (b *ByteBuffer) WriteByte(p byte) error {
return b.buf.WriteByte(p)
}
func (b *ByteBuffer) WriteUint16(p uint16) (n int, err error) {
return b.buf.Write(UInt16ToBytes(p))
}
func (b *ByteBuffer) WriteUint32(p uint32) (n int, err error) {
return b.buf.Write(UInt32ToBytes(p))
}
func (b *ByteBuffer) WriteUint64(p uint64) (n int, err error) {
return b.buf.Write(UInt64ToBytes(p))
}
func (b *ByteBuffer) WriteInt64(p int64) (n int, err error) {
return b.buf.Write(Int64ToBytes(p))
}
// Byte2Int64 byte array to int64 value using big order
func Byte2Int64(data []byte) int64 {
return int64((int64(data[0])&0xff)<<56 |
(int64(data[1])&0xff)<<48 |
(int64(data[2])&0xff)<<40 |
(int64(data[3])&0xff)<<32 |
(int64(data[4])&0xff)<<24 |
(int64(data[5])&0xff)<<16 |
(int64(data[6])&0xff)<<8 |
(int64(data[7]) & 0xff))
}
// Byte2UInt64 byte array to int64 value using big order
func Byte2UInt64(data []byte) uint64 {
return binary.BigEndian.Uint64(data)
}
// Byte2UInt16 byte array to uint16 value using big order
func Byte2UInt16(data []byte) uint16 {
return binary.BigEndian.Uint16(data)
}
// Byte2UInt32 byte array to uint32 value using big order
func Byte2UInt32(data []byte) uint32 {
return binary.BigEndian.Uint32(data)
}
// Int2BytesTo int value to bytes array using big order
func Int2BytesTo(v int, ret []byte) {
ret[0] = byte(v >> 24)
ret[1] = byte(v >> 16)
ret[2] = byte(v >> 8)
ret[3] = byte(v)
}
// Int2Bytes int value to bytes array using big order
func Int2Bytes(v int) []byte {
ret := make([]byte, 4)
Int2BytesTo(v, ret)
return ret
}
// Int64ToBytesTo int64 value to bytes array using big order
func Int64ToBytesTo(v int64, ret []byte) {
ret[0] = byte(v >> 56)
ret[1] = byte(v >> 48)
ret[2] = byte(v >> 40)
ret[3] = byte(v >> 32)
ret[4] = byte(v >> 24)
ret[5] = byte(v >> 16)
ret[6] = byte(v >> 8)
ret[7] = byte(v)
}
// Uint64ToBytesTo uint64 value to bytes array using big order
func Uint64ToBytesTo(v uint64, ret []byte) {
binary.BigEndian.PutUint64(ret, v)
}
// Int64ToBytes int64 value to bytes array using big order
func Int64ToBytes(v int64) []byte {
ret := make([]byte, 8)
Int64ToBytesTo(v, ret)
return ret
}
// Uint32ToBytesTo uint32 value to bytes array using big order
func Uint32ToBytesTo(v uint32, ret []byte) {
binary.BigEndian.PutUint32(ret, v)
}
// UInt32ToBytes uint32 value to bytes array using big order
func UInt32ToBytes(v uint32) []byte {
ret := make([]byte, 4)
Uint32ToBytesTo(v, ret)
return ret
}
// Uint16ToBytesTo uint16 value to bytes array using big order
func Uint16ToBytesTo(v uint16, ret []byte) {
binary.BigEndian.PutUint16(ret, v)
}
// UInt16ToBytes uint16 value to bytes array using big order
func UInt16ToBytes(v uint16) []byte {
ret := make([]byte, 2)
Uint16ToBytesTo(v, ret)
return ret
}
// UInt16ToBytes uint16 value to bytes array using big order
func UInt64ToBytes(v uint64) []byte {
ret := make([]byte, 8)
Uint64ToBytesTo(v, ret)
return ret
}