Skip to content

Commit 209e959

Browse files
committed
Update Encoding API
1 parent 6c949fe commit 209e959

14 files changed

+306
-29
lines changed

src/main/java/io/nats/json/Encoding.java

Lines changed: 200 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,41 @@
1616
import java.io.UnsupportedEncodingException;
1717
import java.net.URLDecoder;
1818
import java.nio.charset.StandardCharsets;
19+
import java.util.Arrays;
1920
import java.util.Base64;
2021

2122
public abstract class Encoding {
2223
private Encoding() {} /* ensures cannot be constructed */
2324

25+
/**
26+
* base64 encode a byte array to a byte array
27+
* @param input the input byte array to encode
28+
* @return the encoded byte array
29+
*/
30+
public static byte[] base64BasicEncode(byte[] input) {
31+
return Base64.getEncoder().encode(input);
32+
}
33+
34+
/**
35+
* base64 encode a byte array to a byte array
36+
* @param input the input byte array to encode
37+
* @return the encoded byte array
38+
*/
39+
public static String base64BasicEncodeToString(byte[] input) {
40+
return Base64.getEncoder().encodeToString(input);
41+
}
42+
2443
/**
2544
* base64 url encode a byte array to a byte array
2645
* @param input the input byte array to encode
2746
* @return the encoded byte array
28-
* @deprecated prefer base64UrlEncode
2947
*/
30-
@Deprecated
31-
public static byte[] base64Encode(byte[] input) {
32-
return Base64.getUrlEncoder().withoutPadding().encode(input);
48+
public static String base64BasicEncodeToString(String input) {
49+
return Base64.getEncoder()
50+
.encodeToString(input.getBytes(StandardCharsets.UTF_8));
3351
}
3452

53+
3554
/**
3655
* base64 url encode a byte array to a byte array
3756
* @param input the input byte array to encode
@@ -42,21 +61,50 @@ public static byte[] base64UrlEncode(byte[] input) {
4261
}
4362

4463
/**
45-
* base64 url encode a byte array to a string
64+
* base64 url encode a byte array to a byte array
4665
* @param input the input byte array to encode
47-
* @return the encoded string
66+
* @return the encoded byte array
4867
*/
49-
public static String toBase64Url(byte[] input) {
50-
return new String(base64UrlEncode(input));
68+
public static String base64UrlEncodeToString(byte[] input) {
69+
return Base64.getUrlEncoder().withoutPadding().encodeToString(input);
5170
}
5271

5372
/**
54-
* base64 url encode a string to a string
55-
* @param input the input string to encode
56-
* @return the encoded string
73+
* base64 url encode a byte array to a byte array
74+
* @param input the input byte array to encode
75+
* @return the encoded byte array
5776
*/
58-
public static String toBase64Url(String input) {
59-
return new String(base64UrlEncode(input.getBytes(StandardCharsets.US_ASCII)));
77+
public static String base64UrlEncodeToString(String input) {
78+
return Base64.getUrlEncoder()
79+
.withoutPadding()
80+
.encodeToString(input.getBytes(StandardCharsets.UTF_8));
81+
}
82+
83+
/**
84+
* base64 decode a byte array
85+
* @param input the input byte array to decode
86+
* @return the decoded byte array
87+
*/
88+
public static byte[] base64BasicDecode(byte[] input) {
89+
return Base64.getDecoder().decode(input);
90+
}
91+
92+
/**
93+
* base64 decode a base64 encoded string
94+
* @param input the input string to decode
95+
* @return the decoded byte array
96+
*/
97+
public static byte[] base64BasicDecode(String input) {
98+
return Base64.getDecoder().decode(input);
99+
}
100+
101+
/**
102+
* base64 decode a base64 encoded string
103+
* @param input the input string to decode
104+
* @return the decoded string
105+
*/
106+
public static String base64BasicDecodeToString(String input) {
107+
return new String(Base64.getDecoder().decode(input));
60108
}
61109

62110
/**
@@ -69,12 +117,104 @@ public static byte[] base64UrlDecode(byte[] input) {
69117
}
70118

71119
/**
72-
* get a string from a base64 url encoded byte array
120+
* base64 url decode a base64 url encoded string
121+
* @param input the input string to decode
122+
* @return the decoded byte array
123+
*/
124+
public static byte[] base64UrlDecode(String input) {
125+
return Base64.getUrlDecoder().decode(input);
126+
}
127+
128+
/**
129+
* base64 url decode a base64 url encoded string
73130
* @param input the input string to decode
74131
* @return the decoded string
75132
*/
76-
public static String fromBase64Url(String input) {
77-
return new String(base64UrlDecode(input.getBytes(StandardCharsets.US_ASCII)));
133+
public static String base64UrlDecodeToString(String input) {
134+
return new String(Base64.getUrlDecoder().decode(input));
135+
}
136+
137+
// http://en.wikipedia.org/wiki/Base_32
138+
private static final String BASE32_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
139+
private static final int[] BASE32_LOOKUP;
140+
private static final int MASK = 31;
141+
private static final int SHIFT = 5;
142+
public static char[] base32Encode(final byte[] input) {
143+
int last = input.length;
144+
char[] charBuff = new char[(last + 7) * 8 / SHIFT];
145+
int offset = 0;
146+
int buffer = input[offset++];
147+
int bitsLeft = 8;
148+
int i = 0;
149+
150+
while (bitsLeft > 0 || offset < last) {
151+
if (bitsLeft < SHIFT) {
152+
if (offset < last) {
153+
buffer <<= 8;
154+
buffer |= (input[offset++] & 0xff);
155+
bitsLeft += 8;
156+
} else {
157+
int pad = SHIFT - bitsLeft;
158+
buffer <<= pad;
159+
bitsLeft += pad;
160+
}
161+
}
162+
int index = MASK & (buffer >> (bitsLeft - SHIFT));
163+
bitsLeft -= SHIFT;
164+
charBuff[i] = BASE32_CHARS.charAt(index);
165+
i++;
166+
}
167+
168+
int nonBlank;
169+
170+
for (nonBlank=charBuff.length-1;nonBlank>=0;nonBlank--) {
171+
if (charBuff[nonBlank] != 0) {
172+
break;
173+
}
174+
}
175+
176+
char[] retVal = new char[nonBlank+1];
177+
178+
System.arraycopy(charBuff, 0, retVal, 0, retVal.length);
179+
180+
Arrays.fill(charBuff, '\0');
181+
182+
return retVal;
183+
}
184+
static {
185+
BASE32_LOOKUP = new int[256];
186+
187+
Arrays.fill(BASE32_LOOKUP, 0xFF);
188+
189+
for (int i = 0; i < BASE32_CHARS.length(); i++) {
190+
int index = BASE32_CHARS.charAt(i) - '0';
191+
BASE32_LOOKUP[index] = i;
192+
}
193+
}
194+
195+
public static byte[] base32Decode(final char[] input) {
196+
byte[] bytes = new byte[input.length * SHIFT / 8];
197+
int buffer = 0;
198+
int next = 0;
199+
int bitsLeft = 0;
200+
201+
for (int i = 0; i < input.length; i++) {
202+
int lookup = input[i] - '0';
203+
204+
if (lookup < 0 || lookup >= BASE32_LOOKUP.length) {
205+
continue;
206+
}
207+
208+
int c = BASE32_LOOKUP[lookup];
209+
buffer <<= SHIFT;
210+
buffer |= c & MASK;
211+
bitsLeft += SHIFT;
212+
if (bitsLeft >= 8) {
213+
bytes[next++] = (byte) (buffer >> (bitsLeft - 8));
214+
bitsLeft -= 8;
215+
}
216+
}
217+
return bytes;
78218
}
79219

80220
public static String jsonDecode(String s) {
@@ -86,7 +226,6 @@ public static String jsonDecode(String s) {
86226
char nextChar = (x == len - 1) ? '\\' : s.charAt(x + 1);
87227
switch (nextChar) {
88228
case '\\':
89-
ch = '\\';
90229
break;
91230
case 'b':
92231
ch = '\b';
@@ -178,4 +317,48 @@ public static String uriDecode(String source) {
178317
return source;
179318
}
180319
}
320+
321+
/**
322+
* @deprecated Use {@link #base64UrlEncode(byte[])} instead.
323+
* base64 url encode a byte array to a byte array
324+
* @param input the input byte array to encode
325+
* @return the encoded byte array
326+
*/
327+
@Deprecated
328+
public static byte[] base64Encode(byte[] input) {
329+
return Base64.getUrlEncoder().withoutPadding().encode(input);
330+
}
331+
332+
/**
333+
* @deprecated Use {@link #base64UrlEncodeToString(byte[])} instead.
334+
* base64 url encode a byte array to a string
335+
* @param input the input byte array to encode
336+
* @return the encoded string
337+
*/
338+
@Deprecated
339+
public static String toBase64Url(byte[] input) {
340+
return base64UrlEncodeToString(input);
341+
}
342+
343+
/**
344+
* @deprecated Use {@link #base64UrlEncodeToString(String)} instead.
345+
* base64 url encode a string to a string
346+
* @param input the input string to encode
347+
* @return the encoded string
348+
*/
349+
@Deprecated
350+
public static String toBase64Url(String input) {
351+
return base64UrlEncodeToString(input);
352+
}
353+
354+
/**
355+
* @deprecated Use {@link #base64UrlDecodeToString(String)} instead.
356+
* get a string from a base64 url encoded byte array
357+
* @param input the input string to decode
358+
* @return the decoded string
359+
*/
360+
@Deprecated
361+
public static String fromBase64Url(String input) {
362+
return base64UrlDecodeToString(input);
363+
}
181364
}

0 commit comments

Comments
 (0)