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:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,11 +16,10 @@
|
||||
|
||||
package org.springframework.util;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import jakarta.xml.bind.DatatypeConverter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
@@ -29,19 +28,20 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
class Base64UtilsTests {
|
||||
|
||||
|
||||
@Test
|
||||
void encode() throws UnsupportedEncodingException {
|
||||
void encode() {
|
||||
byte[] bytes = new byte[]
|
||||
{-0x4f, 0xa, -0x73, -0x4f, 0x64, -0x20, 0x75, 0x41, 0x5, -0x49, -0x57, -0x65, -0x19, 0x2e, 0x3f, -0x1b};
|
||||
assertThat(Base64Utils.decode(Base64Utils.encode(bytes))).isEqualTo(bytes);
|
||||
|
||||
bytes = "Hello World".getBytes("UTF-8");
|
||||
bytes = "Hello World".getBytes(UTF_8);
|
||||
assertThat(Base64Utils.decode(Base64Utils.encode(bytes))).isEqualTo(bytes);
|
||||
|
||||
bytes = "Hello World\r\nSecond Line".getBytes("UTF-8");
|
||||
bytes = "Hello World\r\nSecond Line".getBytes(UTF_8);
|
||||
assertThat(Base64Utils.decode(Base64Utils.encode(bytes))).isEqualTo(bytes);
|
||||
|
||||
bytes = "Hello World\r\nSecond Line\r\n".getBytes("UTF-8");
|
||||
bytes = "Hello World\r\nSecond Line\r\n".getBytes(UTF_8);
|
||||
assertThat(Base64Utils.decode(Base64Utils.encode(bytes))).isEqualTo(bytes);
|
||||
|
||||
bytes = new byte[] { (byte) 0xfb, (byte) 0xf0 };
|
||||
@@ -53,24 +53,24 @@ class Base64UtilsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void encodeToStringWithJdk8VsJaxb() throws UnsupportedEncodingException {
|
||||
void encodeToStringWithJdk8VsJaxb() {
|
||||
byte[] bytes = new byte[]
|
||||
{-0x4f, 0xa, -0x73, -0x4f, 0x64, -0x20, 0x75, 0x41, 0x5, -0x49, -0x57, -0x65, -0x19, 0x2e, 0x3f, -0x1b};
|
||||
assertThat(DatatypeConverter.printBase64Binary(bytes)).isEqualTo(Base64Utils.encodeToString(bytes));
|
||||
assertThat(Base64Utils.decodeFromString(Base64Utils.encodeToString(bytes))).isEqualTo(bytes);
|
||||
assertThat(DatatypeConverter.parseBase64Binary(DatatypeConverter.printBase64Binary(bytes))).isEqualTo(bytes);
|
||||
|
||||
bytes = "Hello World".getBytes("UTF-8");
|
||||
bytes = "Hello World".getBytes(UTF_8);
|
||||
assertThat(DatatypeConverter.printBase64Binary(bytes)).isEqualTo(Base64Utils.encodeToString(bytes));
|
||||
assertThat(Base64Utils.decodeFromString(Base64Utils.encodeToString(bytes))).isEqualTo(bytes);
|
||||
assertThat(DatatypeConverter.parseBase64Binary(DatatypeConverter.printBase64Binary(bytes))).isEqualTo(bytes);
|
||||
|
||||
bytes = "Hello World\r\nSecond Line".getBytes("UTF-8");
|
||||
bytes = "Hello World\r\nSecond Line".getBytes(UTF_8);
|
||||
assertThat(DatatypeConverter.printBase64Binary(bytes)).isEqualTo(Base64Utils.encodeToString(bytes));
|
||||
assertThat(Base64Utils.decodeFromString(Base64Utils.encodeToString(bytes))).isEqualTo(bytes);
|
||||
assertThat(DatatypeConverter.parseBase64Binary(DatatypeConverter.printBase64Binary(bytes))).isEqualTo(bytes);
|
||||
|
||||
bytes = "Hello World\r\nSecond Line\r\n".getBytes("UTF-8");
|
||||
bytes = "Hello World\r\nSecond Line\r\n".getBytes(UTF_8);
|
||||
assertThat(DatatypeConverter.printBase64Binary(bytes)).isEqualTo(Base64Utils.encodeToString(bytes));
|
||||
assertThat(Base64Utils.decodeFromString(Base64Utils.encodeToString(bytes))).isEqualTo(bytes);
|
||||
assertThat(DatatypeConverter.parseBase64Binary(DatatypeConverter.printBase64Binary(bytes))).isEqualTo(bytes);
|
||||
@@ -85,5 +85,17 @@ class Base64UtilsTests {
|
||||
assertThat(Base64Utils.encodeToUrlSafeString(bytes)).isEqualTo("-_A=");
|
||||
assertThat(Base64Utils.decodeFromUrlSafeString(Base64Utils.encodeToUrlSafeString(bytes))).isEqualTo(bytes);
|
||||
}
|
||||
@Test
|
||||
void emptyInputs() {
|
||||
assertThat(Base64Utils.encode(new byte[0])).isEmpty();
|
||||
assertThat(Base64Utils.encodeToString(new byte[0])).isEmpty();
|
||||
assertThat(Base64Utils.encodeUrlSafe(new byte[0])).isEmpty();
|
||||
assertThat(Base64Utils.encodeToUrlSafeString(new byte[0])).isEmpty();
|
||||
|
||||
assertThat(Base64Utils.decode(new byte[0])).isEmpty();
|
||||
assertThat(Base64Utils.decodeFromString("")).isEmpty();
|
||||
assertThat(Base64Utils.decodeUrlSafe(new byte[0])).isEmpty();
|
||||
assertThat(Base64Utils.decodeFromUrlSafeString("")).isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user