Skip to content

Commit 5645729

Browse files
committed
implement base64
1 parent 2ead653 commit 5645729

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/main/java/com/jsoniter/Jsoniter.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jsoniter;
22

3+
import java.io.ByteArrayOutputStream;
34
import java.io.Closeable;
45
import java.io.IOException;
56
import java.io.InputStream;
@@ -12,6 +13,22 @@ public class Jsoniter implements Closeable {
1213

1314
final static int[] digits = new int[256];
1415
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 };
1532
InputStream in;
1633
byte[] buf;
1734
int head;
@@ -375,8 +392,47 @@ public final Slice readSlice() throws IOException {
375392
}
376393

377394
public final byte[] readBase64() throws IOException {
395+
// from https://gist.github.com/EmilHernvall/953733
378396
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();
380436
}
381437

382438
public final String readString() throws IOException {

test/main/java/com/jsoniter/TestBase64.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.jsoniter;
22

3-
import com.jsoniter.Jsoniter;
43
import junit.framework.TestCase;
54

65
import java.io.IOException;

0 commit comments

Comments
 (0)