Skip to content

Commit d1c1826

Browse files
committed
Switch to Apache Encoder for Base 64
1 parent c727c91 commit d1c1826

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ plugins {
1111
id 'signing'
1212
}
1313

14-
def jarVersion = "2.21.0.TEST.3"
14+
def jarVersion = "2.21.0"
1515
group = 'io.nats'
1616

1717
def isMerge = System.getenv("BUILD_EVENT") == "push"
@@ -33,6 +33,8 @@ repositories {
3333
}
3434

3535
dependencies {
36+
implementation 'commons-codec:commons-codec:1.18.0'
37+
3638
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.1'
3739
testImplementation 'nl.jqno.equalsverifier:equalsverifier:4.0'
3840
}

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515

1616
import java.net.URLDecoder;
1717
import java.nio.charset.StandardCharsets;
18-
import java.util.Base64;
18+
19+
import org.apache.commons.codec.binary.Base64;
1920

2021
/**
2122
* Utilities for encoding, i.e., Base64, URI and JSON
2223
*/
2324
public abstract class Encoding {
25+
2426
private Encoding() {} /* ensures cannot be constructed */
2527

2628
/**
@@ -29,7 +31,7 @@ private Encoding() {} /* ensures cannot be constructed */
2931
* @return the encoded byte array
3032
*/
3133
public static byte[] base64BasicEncode(byte[] input) {
32-
return Base64.getEncoder().encode(input);
34+
return Base64.encodeBase64(input);
3335
}
3436

3537
/**
@@ -38,7 +40,7 @@ public static byte[] base64BasicEncode(byte[] input) {
3840
* @return the encoded byte array
3941
*/
4042
public static String base64BasicEncodeToString(byte[] input) {
41-
return Base64.getEncoder().encodeToString(input);
43+
return Base64.encodeBase64String(input);
4244
}
4345

4446
/**
@@ -47,8 +49,7 @@ public static String base64BasicEncodeToString(byte[] input) {
4749
* @return the encoded byte array
4850
*/
4951
public static String base64BasicEncodeToString(String input) {
50-
return Base64.getEncoder()
51-
.encodeToString(input.getBytes(StandardCharsets.UTF_8));
52+
return Base64.encodeBase64String(input.getBytes(StandardCharsets.UTF_8));
5253
}
5354

5455
/**
@@ -57,7 +58,7 @@ public static String base64BasicEncodeToString(String input) {
5758
* @return the encoded byte array
5859
*/
5960
public static byte[] base64UrlEncode(byte[] input) {
60-
return Base64.getUrlEncoder().withoutPadding().encode(input);
61+
return Base64.encodeBase64URLSafe(input);
6162
}
6263

6364
/**
@@ -66,7 +67,7 @@ public static byte[] base64UrlEncode(byte[] input) {
6667
* @return the encoded byte array
6768
*/
6869
public static String base64UrlEncodeToString(byte[] input) {
69-
return Base64.getUrlEncoder().withoutPadding().encodeToString(input);
70+
return Base64.encodeBase64URLSafeString(input);
7071
}
7172

7273
/**
@@ -75,9 +76,7 @@ public static String base64UrlEncodeToString(byte[] input) {
7576
* @return the encoded byte array
7677
*/
7778
public static String base64UrlEncodeToString(String input) {
78-
return Base64.getUrlEncoder()
79-
.withoutPadding()
80-
.encodeToString(input.getBytes(StandardCharsets.UTF_8));
79+
return Base64.encodeBase64URLSafeString(input.getBytes(StandardCharsets.UTF_8));
8180
}
8281

8382
/**
@@ -86,7 +85,7 @@ public static String base64UrlEncodeToString(String input) {
8685
* @return the decoded byte array
8786
*/
8887
public static byte[] base64BasicDecode(byte[] input) {
89-
return Base64.getDecoder().decode(input);
88+
return Base64.decodeBase64(input);
9089
}
9190

9291
/**
@@ -95,7 +94,7 @@ public static byte[] base64BasicDecode(byte[] input) {
9594
* @return the decoded byte array
9695
*/
9796
public static byte[] base64BasicDecode(String input) {
98-
return Base64.getDecoder().decode(input);
97+
return Base64.decodeBase64(input);
9998
}
10099

101100
/**
@@ -104,7 +103,7 @@ public static byte[] base64BasicDecode(String input) {
104103
* @return the decoded string
105104
*/
106105
public static String base64BasicDecodeToString(String input) {
107-
return new String(Base64.getDecoder().decode(input));
106+
return new String(Base64.decodeBase64(input));
108107
}
109108

110109
/**
@@ -113,7 +112,7 @@ public static String base64BasicDecodeToString(String input) {
113112
* @return the decoded byte array
114113
*/
115114
public static byte[] base64UrlDecode(byte[] input) {
116-
return Base64.getUrlDecoder().decode(input);
115+
return Base64.decodeBase64(input);
117116
}
118117

119118
/**
@@ -122,7 +121,7 @@ public static byte[] base64UrlDecode(byte[] input) {
122121
* @return the decoded byte array
123122
*/
124123
public static byte[] base64UrlDecode(String input) {
125-
return Base64.getUrlDecoder().decode(input);
124+
return Base64.decodeBase64(input);
126125
}
127126

128127
/**
@@ -131,7 +130,7 @@ public static byte[] base64UrlDecode(String input) {
131130
* @return the decoded string
132131
*/
133132
public static String base64UrlDecodeToString(String input) {
134-
return new String(Base64.getUrlDecoder().decode(input));
133+
return new String(Base64.decodeBase64(input));
135134
}
136135

137136
/**

0 commit comments

Comments
 (0)