Skip to content

Commit da600a7

Browse files
authored
Map decodeBase64 to getMimeDecoder() to preserve URL-safe decoding (#133)
Apache's `Base64.decodeBase64` accepts both the standard (`+/`) and URL-safe (`-_`) alphabets, whereas `java.util.Base64.getDecoder()` throws `IllegalArgumentException` on URL-safe input. `getMimeDecoder()` is the closest equivalent that handles both variants. Fixes #132
1 parent c28f139 commit da600a7

3 files changed

Lines changed: 8 additions & 5 deletions

File tree

src/main/java/org/openrewrite/apache/commons/codec/ApacheBase64ToJavaBase64.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
6060
} else if (apacheEncode64.matches(mi)) {
6161
templatePrefix = "Base64.getEncoder().encode(#{anyArray()})";
6262
} else if (apacheDecode.matches(mi)) {
63-
templatePrefix = "Base64.getDecoder().decode(#{any(String)})";
63+
// Apache's `decodeBase64` accepts both the standard (`+/`) and URL-safe (`-_`) alphabets.
64+
// `getMimeDecoder()` is the closest `java.util.Base64` equivalent that decodes both variants,
65+
// whereas `getDecoder()` throws `IllegalArgumentException` on URL-safe input.
66+
templatePrefix = "Base64.getMimeDecoder().decode(#{any(String)})";
6467
} else if (apacheEncode64UrlSafe.matches(mi)) {
6568
templatePrefix = "Base64.getUrlEncoder().withoutPadding().encode(#{anyArray()})";
6669
} else if (apacheEncode64UrlSafeString.matches(mi)) {

src/main/resources/META-INF/rewrite/examples.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ examples:
4545
4646
class Test {
4747
static byte[] decodeBytes(byte[] encodedBytes) {
48-
return Base64.getDecoder().decode(encodedBytes);
48+
return Base64.getMimeDecoder().decode(encodedBytes);
4949
}
5050
static byte[] decodeToBytes(String encodedString) {
51-
return Base64.getDecoder().decode(encodedString);
51+
return Base64.getMimeDecoder().decode(encodedString);
5252
}
5353
static String encodeToString(byte[] decodedByteArr) {
5454
return Base64.getEncoder().encodeToString(decodedByteArr);

src/test/java/org/openrewrite/apache/commons/codec/ApacheBase64ToJavaBase64Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ static String encodeBytesUrlSafeString(byte [] encodeBytes) {
6767
6868
class Test {
6969
static byte[] decodeBytes(byte[] encodedBytes) {
70-
return Base64.getDecoder().decode(encodedBytes);
70+
return Base64.getMimeDecoder().decode(encodedBytes);
7171
}
7272
static byte[] decodeToBytes(String encodedString) {
73-
return Base64.getDecoder().decode(encodedString);
73+
return Base64.getMimeDecoder().decode(encodedString);
7474
}
7575
static String encodeToString(byte[] decodedByteArr) {
7676
return Base64.getEncoder().encodeToString(decodedByteArr);

0 commit comments

Comments
 (0)