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