6f620fab9e4f7260d2cf354e37cca133d041a915
[iotcloud.git] / src / java / iotcloud / KeyValue.java
1 package iotcloud;
2 import java.nio.ByteBuffer;
3
4 class KeyValue extends Entry {
5         private IoTString key;
6         private IoTString value;
7         
8         KeyValue(IoTString _key, IoTString _value) {
9                 key=_key;
10                 value=_value;
11         }
12
13         IoTString getKey() {
14                 return key;
15         }
16
17         IoTString getValue() {
18                 return value;
19         }
20         
21         static Entry decode(ByteBuffer bb) {
22                 int keylength=bb.getInt();
23                 int valuelength=bb.getInt();
24                 byte[] key=new byte[keylength];
25                 byte[] value=new byte[valuelength];
26                 bb.get(key);
27                 bb.get(value);
28                 return new KeyValue(IoTString.shallow(key), IoTString.shallow(value));
29         }
30
31         void encode(ByteBuffer bb) {
32                 bb.put(Entry.TypeKeyValue);
33                 bb.putInt(key.length());
34                 bb.putInt(value.length());
35                 bb.put(key.internalBytes());
36                 bb.put(value.internalBytes());
37         }
38
39         int getSize() {
40                 return 2*Integer.BYTES+key.length()+value.length()+Byte.BYTES;
41         }
42
43         byte getType() {
44                 return Entry.TypeKeyValue;
45         }
46 }