Simplify Base64Utils by using java.util.Base64

This commit simplifies the `Base64Utils` implementation by relying more
on the `Base64` methods that perform similar work.

See gh-28434
This commit is contained in:
j3graham
2022-05-09 18:46:54 -04:00
committed by Brian Clozel
parent 37217a63e1
commit f7e07322ab
2 changed files with 26 additions and 19 deletions

View File

@@ -16,8 +16,6 @@
package org.springframework.util;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/**
@@ -32,9 +30,6 @@ import java.util.Base64;
*/
public abstract class Base64Utils {
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
/**
* Base64-encode the given byte array.
* @param src the original byte array
@@ -96,7 +91,7 @@ public abstract class Base64Utils {
if (src.length == 0) {
return "";
}
return new String(encode(src), DEFAULT_CHARSET);
return Base64.getEncoder().encodeToString(src);
}
/**
@@ -108,7 +103,7 @@ public abstract class Base64Utils {
if (src.isEmpty()) {
return new byte[0];
}
return decode(src.getBytes(DEFAULT_CHARSET));
return Base64.getDecoder().decode(src);
}
/**
@@ -118,7 +113,7 @@ public abstract class Base64Utils {
* @return the encoded byte array as a UTF-8 String
*/
public static String encodeToUrlSafeString(byte[] src) {
return new String(encodeUrlSafe(src), DEFAULT_CHARSET);
return Base64.getUrlEncoder().encodeToString(src);
}
/**
@@ -128,7 +123,7 @@ public abstract class Base64Utils {
* @return the original byte array
*/
public static byte[] decodeFromUrlSafeString(String src) {
return decodeUrlSafe(src.getBytes(DEFAULT_CHARSET));
return Base64.getUrlDecoder().decode(src);
}
}