Skip to content

Commit

Permalink
implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
suztomo committed May 15, 2024
1 parent cdc5b28 commit 0a4bd64
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
*/
@Deprecated
public class Base64 {
// Special decoders that discards the new line character so that the behavior matches what
// we had with Apache Commmons Codec's decodeBase64.
private static final BaseEncoding BASE64 = BaseEncoding.base64().withSeparator("\n", 64);
private static final BaseEncoding BASE64URL = BaseEncoding.base64Url().withSeparator("\n", 64);

/**
* Encodes binary data using the base64 algorithm but does not chunk the output.
Expand Down Expand Up @@ -92,6 +96,9 @@ public static byte[] decodeBase64(byte[] base64Data) {
* Decodes a Base64 String into octets. Note that this method handles both URL-safe and
* non-URL-safe base 64 encoded strings.
*
* <p>For the compatibility with the old version that used Apache Commons Coded's decodeBase64,
* this method discards new line characters and trailing whitespaces.
*
* @param base64String String containing Base64 data or {@code null} for {@code null} result
* @return Array containing decoded data or {@code null} for {@code null} input
*/
Expand All @@ -100,10 +107,10 @@ public static byte[] decodeBase64(String base64String) {
return null;
}
try {
return BaseEncoding.base64().decode(base64String);
return BASE64.decode(base64String);
} catch (IllegalArgumentException e) {
if (e.getCause() instanceof DecodingException) {
return BaseEncoding.base64Url().decode(base64String.trim());
return BASE64URL.decode(base64String.trim());
}
throw e;
}
Expand Down

0 comments on commit 0a4bd64

Please sign in to comment.