Adding files to initialize, update, and read key-value (to be used together with...
[iotcloud.git] / version2 / src / C / ByteBuffer.cpp
1 #include "ByteBuffer.h"
2 #include <string.h>
3
4 ByteBuffer::ByteBuffer(Array<char> *array) :
5         buffer(array),
6         offset(0) {
7 }
8
9 void ByteBuffer::put(char c) {
10         buffer->set(offset++, c);
11 }
12
13 void ByteBuffer::putInt(int32_t l) {
14         buffer->set(offset++, (char)(l >> 24));
15         buffer->set(offset++, (char)((l >> 16) & 0xff));
16         buffer->set(offset++, (char)((l >> 8) & 0xff));
17         buffer->set(offset++, (char)(l & 0xff));
18 }
19
20 void ByteBuffer::putLong(int64_t l) {
21         buffer->set(offset++, (char)(l >> 56));
22         buffer->set(offset++, (char)((l >> 48) & 0xff));
23         buffer->set(offset++, (char)((l >> 40) & 0xff));
24         buffer->set(offset++, (char)((l >> 32) & 0xff));
25         buffer->set(offset++, (char)((l >> 24) & 0xff));
26         buffer->set(offset++, (char)((l >> 16) & 0xff));
27         buffer->set(offset++, (char)((l >> 8) & 0xff));
28         buffer->set(offset++, (char)(l & 0xff));
29 }
30
31 void ByteBuffer::put(Array<char> *array) {
32         memcpy(&buffer->internalArray()[offset], array->internalArray(), array->length());
33         offset += array->length();
34 }
35
36 int64_t ByteBuffer::getLong() {
37         char *array = &buffer->internalArray()[offset];
38         offset += 8;
39         return (((int64_t)(unsigned char)array[0]) << 56) |
40                                  (((int64_t)(unsigned char)array[1]) << 48) |
41                                  (((int64_t)(unsigned char)array[2]) << 40) |
42                                  (((int64_t)(unsigned char)array[3]) << 32) |
43                                  (((int64_t)(unsigned char)array[4]) << 24) |
44                                  (((int64_t)(unsigned char)array[5]) << 16) |
45                                  (((int64_t)(unsigned char)array[6]) << 8) |
46                                  (((int64_t)(unsigned char)array[7]));
47 }
48
49 int32_t ByteBuffer::getInt() {
50         char *array = &buffer->internalArray()[offset];
51         offset += 4;
52         return (((int32_t)(unsigned char)array[0]) << 24) |
53                                  (((int32_t)(unsigned char)array[1]) << 16) |
54                                  (((int32_t)(unsigned char)array[2]) << 8) |
55                                  (((int32_t)(unsigned char)array[3]));
56 }
57
58 char ByteBuffer::get() {
59         return buffer->get(offset++);
60 }
61
62 void ByteBuffer::get(Array<char> *array) {
63         memcpy(array->internalArray(), &buffer->internalArray()[offset], array->length());
64         offset += array->length();
65 }
66
67 void ByteBuffer::position(int32_t newPosition) {
68         offset = newPosition;
69 }
70
71 Array<char> *ByteBuffer::array() {
72         return buffer;
73 }
74
75 ByteBuffer *ByteBuffer_wrap(Array<char> *array) {
76         return new ByteBuffer(array);
77 }
78
79 ByteBuffer *ByteBuffer_allocate(uint size) {
80         return new ByteBuffer(new Array<char>(size));
81 }