-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathByteBuffer.cs
More file actions
293 lines (249 loc) · 7.9 KB
/
ByteBuffer.cs
File metadata and controls
293 lines (249 loc) · 7.9 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* 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.
*/
using System;
using System.Text;
namespace Apache.IoTDB.DataStructure
{
public class ByteBuffer
{
private byte[] _buffer;
private int _writePos;
private int _readPos;
private int _totalLength;
private readonly bool _isLittleEndian = BitConverter.IsLittleEndian;
public ByteBuffer(byte[] buffer)
{
_buffer = buffer;
_readPos = 0;
_writePos = buffer.Length;
_totalLength = buffer.Length;
}
public ByteBuffer(int reserve = 1)
{
_buffer = new byte[reserve];
_writePos = 0;
_readPos = 0;
_totalLength = reserve;
}
public bool HasRemaining()
{
return _readPos < _writePos;
}
// these for read
public byte GetByte()
{
var byteVal = _buffer[_readPos];
_readPos += 1;
return byteVal;
}
public bool GetBool()
{
var boolValue = BitConverter.ToBoolean(_buffer, _readPos);
_readPos += 1;
return boolValue;
}
public int GetInt()
{
var intBuff = _buffer[_readPos..(_readPos + 4)];
if (_isLittleEndian)
{
Array.Reverse(intBuff);
}
#if NET461_OR_GREATER || NETSTANDARD2_0
var intValue = BitConverter.ToInt32(intBuff, 0);
#else
var intValue = BitConverter.ToInt32(intBuff);
#endif
_readPos += 4;
return intValue;
}
public long GetLong()
{
var longBuff = _buffer[_readPos..(_readPos + 8)];
if (_isLittleEndian)
{
Array.Reverse(longBuff);
}
#if NET461_OR_GREATER || NETSTANDARD2_0
var longValue = BitConverter.ToInt64(longBuff, 0);
#else
var longValue = BitConverter.ToInt64(longBuff);
#endif
_readPos += 8;
return longValue;
}
public float GetFloat()
{
var floatBuff = _buffer[_readPos..(_readPos + 4)];
if (_isLittleEndian)
{
Array.Reverse(floatBuff);
}
#if NET461_OR_GREATER || NETSTANDARD2_0
var floatValue = BitConverter.ToSingle(floatBuff, 0);
#else
var floatValue = BitConverter.ToSingle(floatBuff);
#endif
_readPos += 4;
return floatValue;
}
public double GetDouble()
{
var doubleBuff = _buffer[_readPos..(_readPos + 8)];
if (_isLittleEndian)
{
Array.Reverse(doubleBuff);
}
#if NET461_OR_GREATER || NETSTANDARD2_0
var doubleValue = BitConverter.ToDouble(doubleBuff, 0);
#else
var doubleValue = BitConverter.ToDouble(doubleBuff);
#endif
_readPos += 8;
return doubleValue;
}
public string GetStr()
{
var length = GetInt();
var strBuff = _buffer[_readPos..(_readPos + length)];
var strValue = Encoding.UTF8.GetString(strBuff);
_readPos += length;
return strValue;
}
public byte[] GetBinary()
{
var length = GetInt();
var buff = _buffer[_readPos..(_readPos + length)];
_readPos += length;
return buff;
}
public byte[] GetBuffer()
{
return _buffer[.._writePos];
}
public byte[] GetBytesByLength(int length)
{
if (_readPos + length > _buffer.Length)
throw new ArgumentOutOfRangeException(nameof(length),
$"Requested length ({length}) with current read position ({_readPos}) exceeds buffer size ({_buffer.Length}).");
var strBuff = _buffer[_readPos..(_readPos + length)];
_readPos += length;
return strBuff;
}
private void ExtendBuffer(int spaceNeed)
{
if (_writePos + spaceNeed >= _totalLength)
{
_totalLength = Math.Max(spaceNeed, _totalLength);
var newBuffer = new byte[_totalLength * 2];
_buffer.CopyTo(newBuffer, 0);
_buffer = newBuffer;
_totalLength = 2 * _totalLength;
}
}
// these for write
public void AddBool(bool value)
{
var boolBuffer = BitConverter.GetBytes(value);
if (_isLittleEndian)
{
Array.Reverse(boolBuffer);
}
ExtendBuffer(boolBuffer.Length);
boolBuffer.CopyTo(_buffer, _writePos);
_writePos += boolBuffer.Length;
}
public void AddInt(int value)
{
var intBuff = BitConverter.GetBytes(value);
if (_isLittleEndian)
{
Array.Reverse(intBuff);
}
ExtendBuffer(intBuff.Length);
intBuff.CopyTo(_buffer, _writePos);
_writePos += intBuff.Length;
}
public void AddLong(long value)
{
var longBuff = BitConverter.GetBytes(value);
if (_isLittleEndian)
{
Array.Reverse(longBuff);
}
ExtendBuffer(longBuff.Length);
longBuff.CopyTo(_buffer, _writePos);
_writePos += longBuff.Length;
}
public void AddFloat(float value)
{
var floatBuff = BitConverter.GetBytes(value);
if (_isLittleEndian)
{
Array.Reverse(floatBuff);
}
ExtendBuffer(floatBuff.Length);
floatBuff.CopyTo(_buffer, _writePos);
_writePos += floatBuff.Length;
}
public void AddDouble(double value)
{
var doubleBuff = BitConverter.GetBytes(value);
if (_isLittleEndian)
{
Array.Reverse(doubleBuff);
}
ExtendBuffer(doubleBuff.Length);
doubleBuff.CopyTo(_buffer, _writePos);
_writePos += doubleBuff.Length;
}
public void AddStr(string value)
{
var strBuf = Encoding.UTF8.GetBytes(value);
AddInt(strBuf.Length);
ExtendBuffer(strBuf.Length);
strBuf.CopyTo(_buffer, _writePos);
_writePos += strBuf.Length;
}
public void AddBinary(byte[] value)
{
AddInt(value.Length);
ExtendBuffer(value.Length);
value.CopyTo(_buffer, _writePos);
_writePos += value.Length;
}
public void AddChar(char value)
{
var charBuf = BitConverter.GetBytes(value);
if (_isLittleEndian)
{
Array.Reverse(charBuf);
}
ExtendBuffer(charBuf.Length);
charBuf.CopyTo(_buffer, _writePos);
_writePos += charBuf.Length;
}
public void AddByte(byte value)
{
ExtendBuffer(1);
_buffer[_writePos] = value;
_writePos += 1;
}
}
}