-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtalk.sm.java
More file actions
1287 lines (1189 loc) · 35.7 KB
/
Copy pathrtalk.sm.java
File metadata and controls
1287 lines (1189 loc) · 35.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package rtalk.sm;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
public class RiCborBuffer {
private byte[] buf;
private int curpos=0;
private long twoTo32 = 4254967296L; //65536 * 65536; //Java doesn't like literal 4254967296
/** Constr */
public RiCborBuffer() { buf=new byte[4000]; }
/**Constr*/
public RiCborBuffer(int capacity) { buf=new byte[capacity]; }
/**Constr*/
public RiCborBuffer(byte[] initialEntries) { buf=initialEntries; }
/// helper static methods to convert java objects to CBOR encoded byte[]
/**
* converts a java string to a utf8 ASCCI limited as CBOR String
* @param value
* @return
*/
public static byte[] convertCBOR(String value){
RiCborBuffer buffer = new RiCborBuffer();
buffer.writeCborString(GuruUtilities.asBytes(value));
return buffer.toBytes();
}
/**
* converts a CBOR byte[] to a java string
* @param value
* @return
*/
public static String stringFromCBOR(byte[] value){
RiCborBuffer buffer = new RiCborBuffer(value);
return buffer.nextCborString();
}
/**
* converts a java byte[] to a CBOR byte[]
* @param value
* @return
*/
public static byte[] convertCBOR(byte[] value){
RiCborBuffer buffer = new RiCborBuffer();
buffer.writeCborBytes(value);
return buffer.toBytes();
}
/**
* converts a CBOR byte[] to a java byte[]
* @param value
* @return
*/
public static byte[] bytesFromCBOR(byte[] value){
RiCborBuffer buffer = new RiCborBuffer(value);
return buffer.nextCborBytes();
}
/**
* converts a java SMOrderedMap to a CBOR byte[]
* @param value
* @return
*/
public static byte[] convertCBOR(SmOrderedMap value){
RiCborBuffer buffer = new RiCborBuffer();
buffer.writeCborOrderedMap(value);
return buffer.toBytes();
}
/**
* converts a CBOR byte[] to a java SmOrderedMap
* @param value
* @return
*/
public static SmOrderedMap smMapFromCBOR(byte[] value){
RiCborBuffer buffer = new RiCborBuffer(value);
return buffer.nextCborOrderedMap();
}
/**
* converts a CBOR byte[] to a java HashMap<String,String>
* @param value
* @return
*/
public static HashMap<String,String> hashMapFromCBOR(byte[] value){
RiCborBuffer buffer = new RiCborBuffer(value);
return buffer.nextCborMap();
}
/**
* converts a java HashMap to a CBOR byte[]
* @param value
* @return
*/
public static byte[] convertCBOR(HashMap<String,String> value){
RiCborBuffer buffer = new RiCborBuffer();
buffer.writeCborHashMap(value);
return buffer.toBytes();
}
// instance support
/**Append to this buffer*/
public void append(byte[] data) {
int len=data == null ? 0 : data.length;
append(data, 0, len);
}
/**Append to this buffer*/
public void append(byte[] data, int loc, int cnt) {
if((curpos + cnt) >= buf.length) {
int newSize=buf.length * 2; //assumption, grow by factor of two, at least
int needed=curpos + cnt; //min size needed for the new data
if(needed > newSize) newSize=(needed * 3) / 2;
growTo(newSize);
}
for(int i=loc; i < (loc+cnt); i++) //changed 2/24/2016
buf[curpos++]=data[i];
}
public void appendHex(String s) {
int len = s.length();
for (int i = 0; i < len; i += 2)
append((byte) Integer.valueOf(s.substring(i, i + 2), 16).intValue());
}
/**Append to this buffer*/
public void append(byte b) {
int curSize=buf.length;
if((curpos + 1) >= curSize) growTo(curSize * 2); //double each time
buf[curpos++]=b;
}
/** Clears out the contents of the buffer */
public void clear() { curpos=0; }
/**Returns the current capacity of this buffer,
* ie: how many entries it can hold without 'growing'*/
public int capacity() { return buf.length; }
/**Returns element at location i
* Note: Throws ArrayIndexOutOfBounds on attempt to fetch illegal index*/
public byte get(int loc) { return buf[loc]; }
/**Get a subrange of entries, places it in 'dst' at 'dstBegin'
* inclusive of srcBegin, exclusive of srcEnd
* Won't overfill dst array.
*/
public void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) {
int len=srcEnd - srcBegin;
int dstLen=dst.length - dstBegin;
if(dstLen < len) len=dstLen; //Just in case dst is too small
int i=srcBegin;
int j=dstBegin;
int end=srcBegin + len;
try{
while(i < end) dst[j++]=buf[i++];
}catch(Exception ex){
System.out.println(ex);
}
}
/** Grows to at least length minSize */
synchronized private void growTo(int minSize) {
int newLen=buf.length * 2;
if(newLen < minSize) newLen=minSize;
byte[] newBuf=new byte[newLen];
int length=buf.length;
for(int i=0; i < length; i++) newBuf[i]=buf[i];
buf=newBuf;
}
/**Set buffer location to specified (byte) value*/
public void set(int loc, int val) {
if(loc >= curpos) growTo(loc + 10);
buf[loc]=(byte)(val & 0xff);
}
/**
* set the cursor to the start poisition
*/
public void rewind(){
curpos = 0;
}
public void setCursorTo(int pos){
curpos = pos;
}
public int getCursonPosition(){
return curpos;
}
/**Returns current number of entries in this buffer*/
public int size() { return curpos; }
/**Sets size to specified value. If val, compared to current size, is:
* -Less: Truncates the buffer down to the specified size.
* (Note: does not reduce size of buffer)
* -Greater: Zeroes elements between current size and new one. Grows capacity as required.
*/
public void size(int val) {
if(val < 0) return;
if(val < curpos) curpos=val; //truncate size
if(capacity() < val) growTo(val);
while(curpos < val) buf[curpos++]=0;
}
/**Returns this buffer as a byte array*/
public byte[] toBytes() {
if(curpos == buf.length) return buf;
byte[] ba=new byte[curpos];
for(int i=0; i < curpos; i++) ba[i]=buf[i];
return ba;
}
/**Override: Returns this buffer as a String*/
public String toString() { return new String(buf, 0, curpos); }
/**
* Add cr/lf
*
*/
public void eol(){
append((byte)0xd);
append((byte)0xa);
}
/**
* Add riri sep ^ + ~ `
*
*/
public void sep(Character c){
if( c == '^') append((byte)0x1c);
if( c == '+') append((byte)0x1d);
if( c == '~') append((byte)0x1e);
if( c == '`') append((byte)0x1f);
if( c == '=') append((byte) 61);
}
/**
* Convert string to bytes and append
* @param text
*/
public void append(String text){
try {
append(text.getBytes("ISO-8859-1"));
}
catch (UnsupportedEncodingException e) {
}
}
public void appendLine(String text){
append(text);
eol();
}
/**
* Append a string mapping riri seps to bytes ^ + ~ `
* @param string
*/
public void appendSm(String string){
// replaces ^ + ~ ' with1C 1D 1E 1F
byte[] buffer = null;
try {
buffer=string.getBytes("ISO-8859-1");
}
catch (UnsupportedEncodingException e) {
}
for(int i = 0; i < buffer.length; i++ ){
switch(buffer[i]) {
case 94: append((byte)0x1c); break; //level 1
case 43: append((byte)0x1d); break; //level 2
case 126: append((byte)0x1e); break; //level 3
case 96: append((byte)0x1f); break; //level 4
default: append(buffer[i]); break; //normal case
}
}
}
/**Returns true if no more to process*/
public boolean atEnd() {
if(buf.length == 0) return true;
return curpos >= buf.length;
}
public byte nextByte(){
// get the byte at the cursor and advance it
byte rtn = get(curpos);
curpos++;
return rtn;
}
public byte peekByte(){
// get the byte at the cursor
byte rtn = get(curpos);
return rtn;
}
public void putByte(byte val){
// put the byte at the cursor
buf[curpos] = val;
curpos++;
}
// CBOR support
/**skips until it finds a tag
* only works for tag,byte[],tag,byte[]....
* need to extend to support skipping arrays and maps
*
*/
// TODO extend to skipping other types
public int nextCborTag() {
while(!atEnd()){
byte tag = peekByte();
int size = nextCborSize();
if((tag & (byte)0xe0) == (byte)0xc0)return size;
if((tag & (byte)0xe0) == (byte)0x40) curpos = curpos + size; // skip byte[]
if((tag & (byte)0xe0) == (byte)0x60) curpos = curpos + size; // skip char[] assumes ASCII
}
return 0;
}
/** Computes the CBOR size of the next element
* limited to 31 bits, positive int
* */
public int nextCborSize() {
int tag = (int)nextByte() & 0x1f;
if( tag < 24) return (int)tag;
int cnt = 0;
if(tag == 24) cnt = 1;
if(tag == 25) cnt = 2;
if(tag == 26) cnt = 4;
if(tag == 27) cnt = 8;
int tmp = 0;
for( int i = 0; i < cnt; i++){
tmp = tmp << 8;
tmp = tmp + ((int)nextByte() & 0xff);
}
return tmp;
}
/**
* given the first byte in a CBOR encoded byte[] returns the number
* of bytes including the tag
* @param val
* @return
*/
private int numberOfSizeBytes(byte val){
int tag = val & 0x1f;
if( tag < 24) return 1;
if(tag == 24) return 2;
if(tag == 25) return 3;
if(tag == 26) return 5;
return 9;
}
/** Computes the CBOR size of the next element (this == value for int data items)
* limited to 31 bits, positive int
* */
public int peekCborSize() {
int tag = (int)get(curpos) & 0x1f;
if( tag < 24) return (int)tag; //should this return a long?
int cnt = 0;
if(tag == 24) cnt = 1;
if(tag == 25) cnt = 2;
if(tag == 26) cnt = 4;
if(tag == 27) cnt = 8;
int tmp = 0;
for( int i = 0; i < cnt; i++){
tmp = tmp << 8;
tmp = tmp + ((int)get(curpos + 1 + i) & 0xff);
}
return tmp;
}
/** Computes the CBOR size of the next element (this == value for int data items)
* limited to 31 bits, positive int
* (DWH extended to 63 bits)
* */
public long nextCborSizeLong() {
int tag = (int)nextByte() & 0x1f;
if( tag < 24) return (int)tag; //should this return a long?
int cnt = 0;
if(tag == 24) cnt = 1;
if(tag == 25) cnt = 2;
if(tag == 26) cnt = 4;
if(tag == 27) cnt = 8;
long tmp = 0;
for( int i = 0; i < cnt; i++){
tmp = tmp << 8;
tmp = tmp + ((int)nextByte() & 0xff);
}
return tmp;
}
public boolean nextCborBoolean(){
int tag = (int)nextByte() & 0xff;
if( tag == 0xf4) return false;
return true;
}
/**
* writes the size but only up to 31 bits (positive int)
* @param type
* @param size
*/
public void writeCborSize(int type, int size){
if(size < 24){
append((byte) (type + size));
return;
}
if(size < 256){
append((byte) (type + 24));
append((byte) size);
return;
}
if(size < 65536){
append((byte) (type + 25));
append((byte) (size >> 8));
append((byte) (size & 0xff));
return;
}
append((byte) (type + 26));
append((byte) (size >> 24));
append((byte) (size >> 16));
append((byte) (size >> 8));
append((byte) (size & 0xff));
return;
}
/**
* writes the size but only up to 31 bits (positive int)
* DWH - extended to allow up to 63 bits (positive int)
* @param type
* @param size
*/
public void writeCborSize(int type, long size){
if(size < 24){
append((byte) (type + size));
return;
}
if(size < 256){
append((byte) (type + 24));
append((byte) size);
return;
}
if(size < 65536){
append((byte) (type + 25));
append((byte) (size >> 8));
append((byte) (size & 0xff));
return;
} if(size < twoTo32) {
append((byte) (type + 26));
append((byte) (size >> 24));
append((byte) (size >> 16));
append((byte) (size >> 8));
append((byte) (size & 0xff));
return;
}
append((byte) (type + 27));
append((byte) (size >> 56));
append((byte) (size >> 48));
append((byte) (size >> 40));
append((byte) (size >> 32));
append((byte) (size >> 24));
append((byte) (size >> 16));
append((byte) (size >> 8));
append((byte) (size & 0xff));
return;
}
/** peeks the next byte and returns a string for the type
* */
public String nextCborType() {
if(atEnd()) return "end";
byte tag = peekByte();
if((tag & (byte)0xe0) == (byte)0x40) return "Bytes";
if((tag & (byte)0xe0) == (byte)0x60) return "String";
if((tag & (byte)0xe0) == (byte)0x00) return "Integer";
if((tag & (byte)0xe0) == (byte)0x20) return "Integer";
if((tag & (byte)0xe0) == (byte)0xe0) return "Float";
if(tag == (byte)0x9f){ // indefinite Array
if(get(curpos + 1) == (byte)0xf3){ // omap tag
return "Omap";
}else{
return "Array";
}
}
if((tag & (byte)0xe0) == (byte)0x80){
if(((tag & (byte)0x1f) == 0x0) && ((numberOfSizeBytes(tag)) == 1)) return "Array";
if(get(curpos + numberOfSizeBytes(tag)) == (byte)0xf3){ // omap tag
return "Omap";
}else{
return "Array";
}
}
if((tag & (byte)0xe0) == (byte)0xa0) return "Map";
if((tag & (byte)0xe0) == (byte)0xbf) return "indifinite Map";
if((tag & (byte)0xe0) == (byte)0xc0) return "Tag";
if(tag == (byte)0xf6) return "Null";
if(tag == (byte)0xf5) return "Boolean"; // true
if(tag == (byte)0xf4) return "Boolean"; // false
return "unknown";
}
/**Expects next value to be a string or a byte[] and returns its value
* as a java byte[], rtns null if not there
* throws an exception if not a valid encoding*/
public byte[] nextCborByteString() {
if(curpos >= buf.length) return null;
byte tag = (byte) ((byte)0xe0 & peekByte()) ;
if(!((tag == (byte) 0x60) || (tag == (byte) 0x40))) {
return null; // string or byte
}
if(tag == (byte) 0x60){
return GuruUtilities.asBytes(nextCborString());
}else{
return nextCborBytes();
}
}
/**Expects next value to be a string or a bytes[] which is ASCII
* and returns a java string. Returns null if not a string
* if its a null drops the null
* throws an exception if encoding issue*/
public String nextCborString() {
if(curpos >= buf.length) return null;
byte tag = (byte) peekByte();
if ((tag == (byte)0xf6) || (tag == (byte)0xf3)){
curpos = curpos + 1;
return null;
}
tag = (byte) ((byte)0xe0 & tag) ;
if(!((tag == (byte)0x60) || (tag == (byte)0x40))) {
return null; // string or byte
}
int size = nextCborSize();
byte[] rtn = new byte[size];
getBytes(curpos, curpos + size, rtn, 0);
curpos = curpos + size;
try {
return new String(rtn, "ISO-8859-1");
}
catch (UnsupportedEncodingException e) {
}
return "";
}
/**
* if the next CBOR is a byte[] or string convert to a java byte[]
* returns null if not a byte[]
* @return
*/
public byte[] nextCborBytes() {
byte tag = (byte) (0xe0 & peekByte()) ;
if(!(tag == (byte) 0x40)) {
return null; // not byte[]
}
int size = nextCborSize();
byte[] rtn = new byte[size];
getBytes(curpos, curpos + size, rtn, 0);
curpos = curpos + size;
return rtn;
}
/**
* return the CBOR byte[] for the next item, includes encoding
* supports string byte[] float, integer, map, array
* @return
*/
public byte[] nextCborItem() {
if(curpos >= buf.length) return null;
byte tag = peekByte();
if((tag & (byte)0xe0) == (byte)0x40){//byte[]
int size = numberOfSizeBytes(tag) + peekCborSize();
byte[] rtn = new byte[size];
getBytes(curpos, curpos + size, rtn, 0);
curpos = curpos + size;
return rtn;
}
if((tag & (byte)0xe0) == (byte)0x60){//String
int size = numberOfSizeBytes(tag) + peekCborSize();
byte[] rtn = new byte[size];
getBytes(curpos, curpos + size, rtn, 0);
curpos = curpos + size;
return rtn;
}
if((tag & (byte)0xe0) == (byte)0x00){//positive integer
int size = numberOfSizeBytes(tag);
byte[] rtn = new byte[size];
getBytes(curpos, curpos + size, rtn, 0);
curpos = curpos + size;
return rtn;
}
if((tag & (byte)0xe0) == (byte)0x20){//negative integer
int size = numberOfSizeBytes(tag);
byte[] rtn = new byte[size];
getBytes(curpos, curpos + size, rtn, 0);
curpos = curpos + size;
return rtn;
}
if((tag & (byte)0xe0) == (byte)0xe0){//Float
if((tag & (byte)0xff) == ((byte)0xe0 + 27)) { //double precision float
++curpos;
byte[] rtn = new byte[8];
getBytes(curpos,curpos+9, rtn, 0);
curpos += 9;
return rtn;
}
if((tag & (byte)0xff) == ((byte)0xe0 + 26)) { //single precision float
byte[] rtn = new byte[4];
getBytes(curpos,curpos+5, rtn, 0);
curpos += 5;
return rtn;
}
}
if((tag & (byte)0xe0) == (byte)0x80){//array of primitives byte[], string, int, float
int start = curpos;
int size = nextCborSize(); // number of items and skip to start of elements
//TODO generates alot garbage
for( int i = 0; i < size; i++){
nextCborItem();
}
byte[] rtn = new byte[curpos - start];
getBytes(start,curpos, rtn, 0);
return rtn;
}
if(tag == (byte)0x9f){//unbounded array of primitives byte[], string, int, float
int start = curpos;
nextCborItemList();
byte[] rtn = new byte[curpos - start];
getBytes(start,curpos, rtn, 0);
return rtn;
}
if((tag & (byte)0xe0) == (byte)0xa0){//map
int start = curpos;
int size = nextCborSize(); // number of items and skip to start of elements
//TODO generates alot garbage
for( int i = 0; i < size; i++){ // pairs of CBOR
nextCborItem();
nextCborItem();
}
byte[] rtn = new byte[curpos - start];
getBytes(start,curpos, rtn, 0);
return rtn;
}
if((tag & (byte)0xe0) == (byte)0xc0){//tag
int size = numberOfSizeBytes(tag);
byte[] rtn = new byte[size];
getBytes(curpos, curpos + size, rtn, 0);
curpos = curpos + size;
return rtn;
}
return null;
}
/** SmOrderedMaps are String, byte[]
* will process a CBOR map or an OrderedMap (Array with special first item)
*/
public SmOrderedMap nextCborOrderedMap(){
//TODO test
SmOrderedMap rtn = null;
if(nextCborType().equalsIgnoreCase("Omap")){
if(atEnd()) return rtn; // nothing to process
rtn = new SmOrderedMap(true); // allows duplicate keys
if(peekByte() == (byte)0x9f){
curpos++;
boolean first = true;
while(peekByte() != (byte)0xff){
String key = nextCborString();
if(first && key == null){
rtn.setTag(nextCborString());
first = false;
}else{
rtn.putCBOR(key, nextCborItem());
}
}
curpos = curpos + 1;// drop the ff
}else{
int size = nextCborSize() / 2;
for( int i = 0; i < size; i++){
String key = nextCborString();
if(i == 0 & key == null){
rtn.setTag(nextCborString());
}else{
rtn.putCBOR(key, nextCborItem());
}
}
}
return rtn;
}
if(nextCborType().equalsIgnoreCase("Map")){
if(atEnd()) return rtn; // nothing to process
rtn = new SmOrderedMap(false); // no duplicate keys
if(peekByte() == (byte)0xbf){
curpos++;
boolean first = true;
while(peekByte() != (byte)0xff){
String key = nextCborString();
if(first && key == null){
rtn.setTag(nextCborString());
first = false;
}else{
rtn.putCBOR(key, nextCborItem());
}
}
curpos = curpos + 1;// drop the ff
}else{
int size = nextCborSize();
for( int i = 0; i < size; i++){
String key = nextCborString();
if(i == 0 & key == null){
rtn.setTag(nextCborString());
}else{
rtn.putCBOR(key, nextCborItem());
}
}
}
return rtn;
}
return rtn;
}
/**
* assumes that its an array of byte[] or strings limited to ASCII
* must be a fixed size array
* @return
*/
public byte[][] nextCborArrayBytes(){
int size = nextCborSize();
byte[][] rtn = new byte[size][];
for( int i = 0; i < size; i++){
rtn[i] = nextCborByteString();
}
return rtn;
}
/**Next object is a CBOR map of Strings
*
* @return
*/
public HashMap<String,String> nextCborMap(){
//TODO test
if(!nextCborType().equalsIgnoreCase("Map")) return null;
HashMap<String,String> rtn = new HashMap<String,String>();
if(atEnd()) return rtn; // nothing to process
if(peekByte() == (byte)0xbf){
curpos++;
while(peekByte() != (byte)0xff){
rtn.put(nextCborString(), nextCborString());
}
curpos = curpos + 1;// drop the ff
}else{
int size = nextCborSize(); // get the number of elements
for( int i = 0; i < size; i++){
rtn.put(nextCborString(), nextCborString());
}
}
return rtn;
}
/**returns an list of java strings
*
* @return
*/
public ArrayList<String> nextCborStringList(){
//TODO test
if(!nextCborType().equalsIgnoreCase("Array")) return null;
ArrayList<String> rtn = new ArrayList<String>();
if(atEnd()) return rtn; // nothing to process
if(peekByte() == (byte)0x9f){
curpos++;
while(peekByte() != (byte)0xff){
rtn.add(nextCborString());
}
curpos = curpos + 1;// drop the ff
}else{
int size = nextCborSize(); // get the number of elements
for( int i = 0; i < size; i++){
rtn.add(nextCborString());
}
}
return rtn;
}
/**
* return array list of CBOR encoded byte[] for each element
* type must be an array, supports indefinite length
* @return
*/
public ArrayList<byte[]> nextCborItemList(){
if(!nextCborType().equalsIgnoreCase("Array")) return null;
ArrayList<byte[]> rtn = new ArrayList<byte[]>();
if(atEnd()) return rtn; // nothing to process
if(peekByte() == (byte)0x9f){
curpos++;
while(peekByte() != (byte)0xff){
rtn.add(nextCborItem());
}
curpos = curpos + 1;// drop the ff
}else{
int size = nextCborSize(); // get the number of elements
for( int i = 0; i < size; i++){
rtn.add(nextCborItem());
}
}
return rtn;
}
public byte[] peekFirstCborBytes() {
if(curpos <= 0) return null;
int lastCurpos = curpos;
curpos = 0;
byte tag = peekByte();
if((tag & (byte)0xe0) != (byte)0x40){
curpos = lastCurpos;
return null;
}
int size = nextCborSize();
byte[] rtn = new byte[size];
getBytes(curpos, curpos + size, rtn, 0);
curpos = lastCurpos;
return rtn;
}
/**
* Assumes the next item is a CBOR map<string,string>, takes the pairs
* of key, value and inserts into the provided map
* @param map
*/
public void fillCborMap(HashMap<String,String> map){
// starting at the current location which defines a map
// get key values and insert into the hashMap
int size = nextCborSize(); // number of map elements
for( int i = 0; i < size; i++){
String key = nextCborString();
String value = nextCborString();
map.put(key,value);
}
}
/**
* Converts java string to an ASCII byte limited utf-8
* @param value
*/
public void writeCborString(String value){
int size = value.length();
writeCborSize(0x60, size);
append(value);
}
/**
* Converts java byte[] to an ASCII byte limited utf-8
* @param value
*/
public void writeCborString(byte[] value){
int size = value.length;
writeCborSize(0x60, size);
append(value);
}
public void writeCborBytes(byte[] value){
int size = value.length;
writeCborSize(0x40, size);
append(value);
}
public void writeCborTag(int tag){
writeCborSize(0xc0, tag);
}
public void writeCborArraySize(int size){
writeCborSize(0x80, size);
}
/**
* starts an unbounded array
*/
public void writeCborArrayStart(){
append((byte)0x9f);
}
/**
* write the end byte for an unbounded array
*/
public void writeCborArrayEnd(){
append((byte)0xff);
}
/**
* write a true or false
* @param bool
*/
public void writeCborBoolean(boolean bool){
if(bool){
append((byte)0xf5);
}else{
append((byte)0xf4);
}
}
public void writeCborNull(){
append((byte)0xf6);
}
public void writeCborMapSize(int size){
writeCborSize(0xa0, size);
}
/**
* write a array of byte[]
* @param value
*/
public void writeCborArray(ArrayList<byte[]> value){
int size = value.size();
writeCborArraySize(size);
Iterator<byte[]> iter=value.iterator();
while (iter.hasNext()) {
writeCborBytes(iter.next());
}
} /**
* write a array of String
* @param value
*/
public void writeCborStringList(ArrayList<String> value){
int size = value.size();
writeCborArraySize(size);
Iterator<String> iter=value.iterator();
while (iter.hasNext()) {
writeCborString(iter.next());
}
}
/**
* write a hash map of <String, String> as CBOR strings
* @param map
*/
public void writeCborHashMap(HashMap<String,String> map){
int size = map.size();
writeCborMapSize(size);
Iterator<String> iter=map.keySet().iterator();
while (iter.hasNext()) {
String key=(String)iter.next(); //guru key name
String val=(String)map.get(key); //value for key
writeCborString(key);
if(val == null){
writeCborBytes(new byte[0]);
}else{
writeCborBytes(GuruUtilities.asBytes(val));
}
}
if(buf[curpos - 1] == 0) {
System.out.println("bad hash map write cbor");
}
}
/** SmOrderedMaps are String, CBOR encoded byte[]
* keys can be null in which case we use a CBOR null
* value can be null is which case we use a zero length byte[]
* values are byte[]
* tag is optional
* Written as a CBOR array of key,value pairs
*
*/
public void writeCborOrderedMap(SmOrderedMap map){
int size = map.size();
writeCborArraySize((size + 1) * 2);
append((byte)0xf3); // omap marker
if(map.getTag() != null){
writeCborString(map.getTag()); // tag
}else{
writeCborBytes(new byte[0]);
}
for( int i = 0; i < map.size(); i++){
String key = map.getKeyAtAsString(i);
byte[] val=(byte[])map.getCBOR(i);
writeCborString(key);
if(val == null){
writeCborBytes(new byte[0]);
}else{
append(val);
}
}
}
public void writeCborObject(Object val){
Class clazz = val.getClass();
if(clazz == String.class) writeCborString((String)val);