375b29d21e5a658d0a7d6fad35d17aa5433c343e
[iotcloud.git] / src / js / iotjs / orig / keyvalue.js
1 class KeyValue extends Entry{
2         constructor(slot,_key,_value){
3                 if(!(slot instanceof Slot && _key instanceof IoTString && _value instanceof IoTString)){
4                         throw new Error('Argument error ');
5                 }
6                 super(slot);
7                 this.key = _key;
8                 this.value = _value;
9         }
10         getKey(){
11                 return this.key;
12         }
13         getValue(){
14                 return this.value;
15         }
16         decode(slot,bb){
17                 if(!(slot instanceof Slot && bb instanceof ByteBuffer)){
18                         throw new Error('Argument error');
19                 }
20                 var keylength = bb.getByte();
21                 var valuelength = bb.getByte();
22                 var key = new Uint8Array(keylength);
23                 var value = new Uint8Array(valuelength);
24                 var keystring = '';
25                 for(var i =0 ;i<keylength ; i++){
26                         keystring += bb.readByte();
27                 }
28                 var valuestring= '';
29                 for(var j =0 ;j<keylength ; j++){
30                         valuestring += bb.readByte();
31                 }
32                 for(var k = 0; k< keystring.length; k++){
33                         key[k] = keystring.charCodeAt(k);
34                 }
35                 for(var l = 0; l< valuestring.length; l++){
36                         value[l] = valuestring.charCodeAt(l);
37                 }
38                 return new KeyValue(slot,IoTString.shallow(key),IoTString.shallow(value));
39         }
40
41         encode(bb){
42                 if(!(bb instanceof ByteBuffer)){
43                         throw new Error('argument error');
44                 }
45                 bb.writeByte(Entry.TypeKeyValue);
46                 bb.writeByte(this.key.length());
47                 bb.writeByte(this.value.length());
48                 bb.concat(this.key.internalBytes());
49                 bb.concat(this.value.internalBytes());
50         }
51         getSize(){
52                 return 2*4+this.jey.length()+this.value.length()+1;
53         }
54
55         getType(){
56                 return Entry.TypeKeyValue;
57         }
58
59         toString(){
60                 return this.value.toString();
61         }
62         getCopy(s){
63                 return new KeyValue(s,this.key,this.value);
64         }
65 }