-
Notifications
You must be signed in to change notification settings - Fork 786
/
bytebuf_util.go
177 lines (158 loc) · 3.82 KB
/
bytebuf_util.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
/*
* Copyright 2021 CloudWeGo Authors
*
* Licensed 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 codec
import (
"encoding/binary"
"errors"
"io"
"github.com/cloudwego/kitex/pkg/remote"
)
// ReadUint32 ...
func ReadUint32(in remote.ByteBuffer) (uint32, error) {
var buf []byte
var err error
if buf, err = in.Next(Size32); err != nil {
return 0, err
}
return binary.BigEndian.Uint32(buf), nil
}
// PeekUint32 ...
func PeekUint32(in remote.ByteBuffer) (uint32, error) {
var buf []byte
var err error
if buf, err = in.Peek(Size32); err != nil {
return 0, err
}
return binary.BigEndian.Uint32(buf), nil
}
// ReadUint16 ...
func ReadUint16(in remote.ByteBuffer) (uint16, error) {
buf, err := in.Next(Size16)
if err != nil {
return 0, err
}
n := binary.BigEndian.Uint16(buf)
return n, nil
}
// WriteUint32 ...
func WriteUint32(val uint32, out remote.ByteBuffer) error {
var buf []byte
var err error
if buf, err = out.Malloc(Size32); err != nil {
return err
}
binary.BigEndian.PutUint32(buf, val)
return nil
}
// WriteUint16 ...
func WriteUint16(val uint16, out remote.ByteBuffer) error {
var buf []byte
var err error
if buf, err = out.Malloc(Size16); err != nil {
return err
}
binary.BigEndian.PutUint16(buf, val)
return nil
}
// WriteByte ...
func WriteByte(val byte, out remote.ByteBuffer) error {
var buf []byte
var err error
if buf, err = out.Malloc(1); err != nil {
return err
}
buf[0] = val
return nil
}
// Bytes2Uint32NoCheck ...
func Bytes2Uint32NoCheck(bytes []byte) uint32 {
return binary.BigEndian.Uint32(bytes)
}
// Bytes2Uint32 ...
func Bytes2Uint32(bytes []byte) (uint32, error) {
if len(bytes) < 4 {
return 0, io.EOF
}
return binary.BigEndian.Uint32(bytes), nil
}
// Bytes2Uint16NoCheck ...
func Bytes2Uint16NoCheck(bytes []byte) uint16 {
return binary.BigEndian.Uint16(bytes)
}
// Bytes2Uint16 ...
func Bytes2Uint16(bytes []byte, off int) (uint16, error) {
if len(bytes)-off < 2 {
return 0, io.EOF
}
return binary.BigEndian.Uint16(bytes[off:]), nil
}
// Bytes2Uint8 ...
func Bytes2Uint8(bytes []byte, off int) (uint8, error) {
if len(bytes)-off < 1 {
return 0, io.EOF
}
return bytes[off], nil
}
// ReadString ...
func ReadString(in remote.ByteBuffer) (string, int, error) {
strLen, err := ReadUint32(in)
if err != nil {
return "", 0, err
}
if strLen == 0 {
return "", 0, errors.New("invalid string length in ReadString")
}
str, err := in.ReadString(int(strLen))
return str, int(strLen) + Size32, err
}
// WriteString ...
func WriteString(val string, out remote.ByteBuffer) (int, error) {
strLen := len(val)
if err := WriteUint32(uint32(strLen), out); err != nil {
return 0, err
}
n, err := out.WriteString(val)
if err != nil {
return 0, err
}
return n + 4, nil
}
// WriteString2BLen ...
func WriteString2BLen(val string, out remote.ByteBuffer) (int, error) {
strLen := len(val)
if err := WriteUint16(uint16(strLen), out); err != nil {
return 0, err
}
n, err := out.WriteString(val)
if err != nil {
return 0, err
}
return n + 2, nil
}
// ReadString2BLen ...
func ReadString2BLen(bytes []byte, off int) (string, int, error) {
length, err := Bytes2Uint16(bytes, off)
strLen := int(length)
if err != nil {
return "", 0, err
}
off += 2
if len(bytes)-off < strLen {
return "", 0, io.EOF
}
buf := bytes[off : off+strLen]
return string(buf), int(length) + 2, nil
}