1
1
package com .jsoniter ;
2
2
3
+ import java .io .ByteArrayOutputStream ;
3
4
import java .io .Closeable ;
4
5
import java .io .IOException ;
5
6
import java .io .InputStream ;
@@ -12,6 +13,22 @@ public class Jsoniter implements Closeable {
12
13
13
14
final static int [] digits = new int [256 ];
14
15
final static ValueType [] valueTypes = new ValueType [256 ];
16
+ int [] base64Tbl = {
17
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
18
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
19
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 62 , -1 , -1 , -1 , 63 , 52 , 53 , 54 ,
20
+ 55 , 56 , 57 , 58 , 59 , 60 , 61 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 0 , 1 , 2 ,
21
+ 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 ,
22
+ 20 , 21 , 22 , 23 , 24 , 25 , -1 , -1 , -1 , -1 , -1 , -1 , 26 , 27 , 28 , 29 , 30 ,
23
+ 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 ,
24
+ 48 , 49 , 50 , 51 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
25
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
26
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
27
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
28
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
29
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
30
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
31
+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 };
15
32
InputStream in ;
16
33
byte [] buf ;
17
34
int head ;
@@ -375,8 +392,47 @@ public final Slice readSlice() throws IOException {
375
392
}
376
393
377
394
public final byte [] readBase64 () throws IOException {
395
+ // from https://gist.github.com/EmilHernvall/953733
378
396
Slice slice = readSlice ();
379
- return null ;
397
+ if (slice == null ) {
398
+ return null ;
399
+ }
400
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream ();
401
+ int end = slice .head + slice .len ;
402
+ for (int i = slice .head ; i < end ; i ++) {
403
+ int b = 0 ;
404
+ if (base64Tbl [slice .data [i ]] != -1 ) {
405
+ b = (base64Tbl [slice .data [i ]] & 0xFF ) << 18 ;
406
+ }
407
+ // skip unknown characters
408
+ else {
409
+ i ++;
410
+ continue ;
411
+ }
412
+
413
+ int num = 0 ;
414
+ if (i + 1 < end && base64Tbl [slice .data [i +1 ]] != -1 ) {
415
+ b = b | ((base64Tbl [slice .data [i +1 ]] & 0xFF ) << 12 );
416
+ num ++;
417
+ }
418
+ if (i + 2 < end && base64Tbl [slice .data [i +2 ]] != -1 ) {
419
+ b = b | ((base64Tbl [slice .data [i +2 ]] & 0xFF ) << 6 );
420
+ num ++;
421
+ }
422
+ if (i + 3 < end && base64Tbl [slice .data [i +3 ]] != -1 ) {
423
+ b = b | (base64Tbl [slice .data [i +3 ]] & 0xFF );
424
+ num ++;
425
+ }
426
+
427
+ while (num > 0 ) {
428
+ int c = (b & 0xFF0000 ) >> 16 ;
429
+ buffer .write ((char )c );
430
+ b <<= 8 ;
431
+ num --;
432
+ }
433
+ i += 4 ;
434
+ }
435
+ return buffer .toByteArray ();
380
436
}
381
437
382
438
public final String readString () throws IOException {
0 commit comments