-
Notifications
You must be signed in to change notification settings - Fork 3
/
ByteBuffer.h
440 lines (387 loc) · 9.41 KB
/
ByteBuffer.h
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#ifndef __BYTEBUFFER_H__
#define __BYTEBUFFER_H__
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <string>
#include <iostream>
// Default size of the buffer
#define DEFAULT_BUFFER_SIZE 2048
class ByteBuffer
{
public:
ByteBuffer(uint32_t capacity = DEFAULT_BUFFER_SIZE, const char* name = "")
: mark_(-1),
limit_(capacity),
position_(0),
capacity_(capacity),
name_(name)
{
p_buffer_ = NULL;
p_buffer_ = (uint8_t*)calloc(capacity_, sizeof(uint8_t));
}
ByteBuffer(uint8_t* arr, uint32_t length, const char* name = "")
: mark_(-1),
limit_(length),
position_(0),
capacity_(length),
name_(name)
{
p_buffer_ = NULL;
p_buffer_ = (uint8_t*)calloc(capacity_, sizeof(uint8_t));
putBytes(arr, capacity_);
clear();
}
~ByteBuffer()
{
if (p_buffer_)
{
free(p_buffer_);
p_buffer_ = NULL;
}
}
// Write Methods
ByteBuffer& put(ByteBuffer* bb)
{
for (uint32_t i = 0; i < bb->limit(); i++)
append<uint8_t>(bb->get(i));
return *this;
}
ByteBuffer& put(uint8_t value)
{
append<uint8_t>(value);
return *this;
}
ByteBuffer& put(uint8_t value, uint32_t index)
{
insert<uint8_t>(value, index);
return *this;
}
ByteBuffer& putBytes(const uint8_t* buf, uint32_t len)
{
for (uint32_t i = 0; i < len; i++)
append<uint8_t>(buf[i]);
return *this;
}
ByteBuffer& putBytes(const uint8_t* buf, uint32_t len, uint32_t index)
{
position_ = index;
for (uint32_t i = 0; i < len; i++)
append<uint8_t>(buf[i]);
return *this;
}
ByteBuffer& putChar(char value)
{
append<uint8_t>(value);
return *this;
}
ByteBuffer& putChar(char value, uint32_t index)
{
insert<uint8_t>(value, index);
return *this;
}
ByteBuffer& putShort(uint16_t value)
{
append<uint16_t>(value);
return *this;
}
ByteBuffer& putShort(uint16_t value, uint32_t index)
{
insert<uint16_t>(value, index);
return *this;
}
ByteBuffer& putInt(uint32_t value)
{
append<uint32_t>(value);
return *this;
}
ByteBuffer& putInt(uint32_t value, uint32_t index)
{
insert<uint32_t>(value, index);
return *this;
}
ByteBuffer& putLong(uint64_t value)
{
append<uint64_t>(value);
return *this;
}
ByteBuffer& putLong(uint64_t value, uint32_t index)
{
insert<uint64_t>(value, index);
return *this;
}
ByteBuffer& putFloat(float value)
{
append<float>(value);
return *this;
}
ByteBuffer& putFloat(float value, uint32_t index)
{
insert<float>(value, index);
return *this;
}
ByteBuffer& putDouble(double value)
{
append<double>(value);
return *this;
}
ByteBuffer& putDouble(double value, uint32_t index)
{
insert<double>(value, index);
return *this;
}
// Read Methods
uint8_t get()
{
return read<uint8_t>();
}
uint8_t get(uint32_t index) const
{
return read<uint8_t>(index);
}
void getBytes(uint8_t* buf, uint32_t len)
{
// 合法性检测
if (!p_buffer_ || position_ + len > limit_)
return;
for (uint32_t i = 0; i < len; i++)
{
buf[i] = p_buffer_[position_++];
}
}
void getBytes(uint32_t index, uint8_t* buf, uint32_t len) const
{
// 合法性检测
if (!p_buffer_ || index + len > limit_)
return;
uint32_t pos = index;
for (uint32_t i = 0; i < len; i++)
{
buf[i] = p_buffer_[pos++];
}
}
char getChar()
{
return read<uint8_t>();
}
char getChar(uint32_t index) const
{
return read<uint8_t>(index);
}
uint16_t getShort()
{
return read<uint16_t>();
}
uint16_t getShort(uint32_t index) const
{
return read<uint16_t>(index);
}
uint32_t getInt()
{
return read<uint32_t>();
}
uint32_t getInt(uint32_t index) const
{
return read<uint32_t>(index);
}
uint64_t getLong()
{
return read<uint64_t>();
}
uint64_t getLong(uint32_t index) const
{
return read<uint64_t>(index);
}
float getFloat()
{
return read<float>();
}
float getFloat(uint32_t index) const
{
return read<float>(index);
}
double getDouble()
{
return read<double>();
}
double getDouble(uint32_t index) const
{
return read<double>(index);
}
bool equals(ByteBuffer* other)
{
uint32_t len = limit();
if (len != other->limit())
return false;
for (uint32_t i = 0; i < len; i++)
{
if (get(i) != other->get(i))
return false;
}
return true;
}
ByteBuffer* duplicate()
{
ByteBuffer* newBuffer = new ByteBuffer(capacity_);
// copy data
newBuffer->put(this);
newBuffer->limit(limit_);
newBuffer->position(position_);
return newBuffer;
}
ByteBuffer& clear()
{
position_ = 0;
mark_ = -1;
limit_ = capacity_;
return *this;
}
ByteBuffer& flip()
{
limit_ = position_;
position_ = 0;
mark_ = -1;
return *this;
}
ByteBuffer& mark()
{
mark_ = position_;
return *this;
}
ByteBuffer& discardMark()
{
mark_ = -1;
return *this;
}
ByteBuffer& reset()
{
if (mark_ >= 0)
position_ = mark_;
return *this;
}
ByteBuffer& rewind()
{
mark_ = -1;
position_ = 0;
return *this;
}
ByteBuffer& compact()
{
do
{
if (position_ >= limit_)
{
position_ = 0;
break;
}
for (uint32_t i = 0; i < limit_ - position_; i++)
{
p_buffer_[i] = p_buffer_[position_ + i];
}
position_ = limit_ - position_;
} while (0);
limit_ = capacity_;
return *this;
}
bool hasRemaining()
{
return limit_ > position_;
}
uint32_t remaining() const
{
return position_ < limit_ ? limit_ - position_ : 0;
}
uint32_t capacity() const
{
return capacity_;
}
uint32_t position() const
{
return position_;
}
uint32_t limit() const
{
return limit_;
}
ByteBuffer& limit(uint32_t newLimit)
{
if (position_ > newLimit)
position_ = newLimit;
if (mark_ > newLimit)
mark_ = -1;
return *this;
}
ByteBuffer& position(uint32_t newPosition)
{
position_ = newPosition;
}
void printInfo() const
{
std::cout << "ByteBuffer " << name_ << ":\n"
<< "\tmark(" << mark_ << "), "
<< "position(" << position_ << "), "
<< "limit(" << limit_ << "), "
<< "capacity(" << capacity_ << ")." << std::endl;
}
private:
template <typename T>
T read(uint32_t index) const
{
if (!p_buffer_ || index + sizeof(T) > limit_)
return 0;
return *((T*)&p_buffer_[index]);
}
template <typename T>
T read()
{
T data = read<T>(position_);
position_ += sizeof(T);
return data;
}
template<typename T>
void append(T data)
{
if (!p_buffer_)
return;
uint32_t s = sizeof(data);
checkSize(s);
memcpy(&p_buffer_[position_], (uint8_t*)&data, s);
position_ += s;
}
template<typename T>
void insert(T data, uint32_t index)
{
uint32_t s = sizeof(data);
checkSize(index, s);
position_ = index;
append<T>(data);
}
void checkSize(uint32_t increase)
{
checkSize(position_, increase);
}
void checkSize(uint32_t index, uint32_t increase)
{
if (index + increase <= capacity_)
return;
uint32_t newSize = capacity_ + (increase + BUFFER_SIZE_INCREASE - 1) /
BUFFER_SIZE_INCREASE * BUFFER_SIZE_INCREASE;
uint8_t* pBuf = (uint8_t*)realloc(p_buffer_, newSize);
if (!pBuf)
{
std::cout << "relloc failed!" << std::endl;
exit(1);
}
p_buffer_ = pBuf;
capacity_ = newSize;
}
private:
const uint32_t BUFFER_SIZE_INCREASE = 2048;
std::string name_;
uint8_t* p_buffer_;
int32_t mark_;
uint32_t limit_;
uint32_t position_;
uint32_t capacity_;
};
#endif