bytebuffer
[iotcloud.git] / version2 / src / C / KeyValue.cc
1 #include "KeyValue.h"
2 #include "ByteBuffer.h"
3 #include "IoTString.h"
4 /**
5  * KeyValue entry for Slot.
6  * @author Brian Demsky <bdemsky@uci.edu>
7  * @version 1.0
8  */
9
10 KeyValue::~KeyValue() {
11         delete key;
12         delete value;
13 }
14
15 KeyValue *KeyValue_decode(ByteBuffer *bb) {
16         int keylength = bb->getInt();
17         int valuelength = bb->getInt();
18         Array<char> *key = new Array<char>(keylength);
19         bb->get(key);
20
21         if (valuelength != 0) {
22                 Array<char> *value = new Array<char>(valuelength);
23                 bb->get(value);
24                 return new KeyValue(IoTString_shallow(key), IoTString_shallow(value));
25         }
26
27         return new KeyValue(IoTString_shallow(key), NULL);
28 }
29
30 void KeyValue::encode(ByteBuffer *bb) {
31         bb->putInt(key->length());
32         if (value != NULL) {
33                 bb->putInt(value->length());
34         } else {
35                 bb->putInt(0);
36         }
37         bb->put(key->internalBytes());
38         if (value != NULL) {
39                 bb->put(value->internalBytes());
40         }
41 }
42
43 int KeyValue::getSize() {
44         if (value != NULL)
45                 return 2 * sizeof(int32_t) + key->length() + value->length();
46         return 2 * sizeof(int32_t) + key->length();
47 }
48
49 KeyValue *KeyValue::getCopy() {
50         return new KeyValue(new IoTString(key), new IoTString(value));
51 }