Cleaned up git
[iotcloud.git] / version1 / src / js / iotjs / orig / keyvalue.js
diff --git a/version1/src/js/iotjs/orig/keyvalue.js b/version1/src/js/iotjs/orig/keyvalue.js
new file mode 100644 (file)
index 0000000..375b29d
--- /dev/null
@@ -0,0 +1,65 @@
+class KeyValue extends Entry{
+       constructor(slot,_key,_value){
+               if(!(slot instanceof Slot && _key instanceof IoTString && _value instanceof IoTString)){
+                       throw new Error('Argument error ');
+               }
+               super(slot);
+               this.key = _key;
+               this.value = _value;
+       }
+       getKey(){
+               return this.key;
+       }
+       getValue(){
+               return this.value;
+       }
+       decode(slot,bb){
+               if(!(slot instanceof Slot && bb instanceof ByteBuffer)){
+                       throw new Error('Argument error');
+               }
+               var keylength = bb.getByte();
+               var valuelength = bb.getByte();
+               var key = new Uint8Array(keylength);
+               var value = new Uint8Array(valuelength);
+               var keystring = '';
+               for(var i =0 ;i<keylength ; i++){
+                       keystring += bb.readByte();
+               }
+               var valuestring= '';
+               for(var j =0 ;j<keylength ; j++){
+                       valuestring += bb.readByte();
+               }
+               for(var k = 0; k< keystring.length; k++){
+                       key[k] = keystring.charCodeAt(k);
+               }
+               for(var l = 0; l< valuestring.length; l++){
+                       value[l] = valuestring.charCodeAt(l);
+               }
+               return new KeyValue(slot,IoTString.shallow(key),IoTString.shallow(value));
+       }
+
+       encode(bb){
+               if(!(bb instanceof ByteBuffer)){
+                       throw new Error('argument error');
+               }
+               bb.writeByte(Entry.TypeKeyValue);
+               bb.writeByte(this.key.length());
+               bb.writeByte(this.value.length());
+               bb.concat(this.key.internalBytes());
+               bb.concat(this.value.internalBytes());
+       }
+       getSize(){
+               return 2*4+this.jey.length()+this.value.length()+1;
+       }
+
+       getType(){
+               return Entry.TypeKeyValue;
+       }
+
+       toString(){
+               return this.value.toString();
+       }
+       getCopy(s){
+               return new KeyValue(s,this.key,this.value);
+       }
+}