more code
[iotcloud.git] / src / java / iotcloud / KeyValue.java
index fd74dc1..ac19759 100644 (file)
@@ -1,6 +1,47 @@
 package iotcloud;
 package iotcloud;
+import java.nio.ByteBuffer;
 
 
-public class KeyValue extends Entry {
+class KeyValue extends Entry {
+       private IoTString key;
+       private IoTString value;
        
        
+       KeyValue(Slot slot, IoTString _key, IoTString _value) {
+               super(slot);
+               key=_key;
+               value=_value;
+       }
 
 
+       IoTString getKey() {
+               return key;
+       }
+
+       IoTString getValue() {
+               return value;
+       }
+       
+       static Entry decode(Slot slot, ByteBuffer bb) {
+               int keylength=bb.getInt();
+               int valuelength=bb.getInt();
+               byte[] key=new byte[keylength];
+               byte[] value=new byte[valuelength];
+               bb.get(key);
+               bb.get(value);
+               return new KeyValue(slot, IoTString.shallow(key), IoTString.shallow(value));
+       }
+
+       void encode(ByteBuffer bb) {
+               bb.put(Entry.TypeKeyValue);
+               bb.putInt(key.length());
+               bb.putInt(value.length());
+               bb.put(key.internalBytes());
+               bb.put(value.internalBytes());
+       }
+
+       int getSize() {
+               return 2*Integer.BYTES+key.length()+value.length()+Byte.BYTES;
+       }
+
+       byte getType() {
+               return Entry.TypeKeyValue;
+       }
 }
 }