Skip to content

Commit ebbe41a

Browse files
committed
Have org.apache.logging.log4j.util.Base64Util invoke java.util.Base64 directly instead of reflectively (#3686)
1 parent 2bc484c commit ebbe41a

File tree

2 files changed

+11
-37
lines changed

2 files changed

+11
-37
lines changed

log4j-api/src/main/java/org/apache/logging/log4j/util/Base64Util.java

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
*/
1717
package org.apache.logging.log4j.util;
1818

19-
import java.lang.reflect.Method;
2019
import java.nio.charset.Charset;
21-
import org.apache.logging.log4j.Logger;
22-
import org.apache.logging.log4j.LoggingException;
23-
import org.apache.logging.log4j.status.StatusLogger;
20+
import java.util.Base64;
2421

2522
/**
2623
* Base64 encodes Strings. This utility is only necessary because the mechanism to do this changed in Java 8 and
@@ -30,27 +27,7 @@
3027
*/
3128
public final class Base64Util {
3229

33-
private static final Logger LOGGER = StatusLogger.getLogger();
34-
35-
private static Method encodeMethod = null;
36-
private static Object encoder = null;
37-
38-
static {
39-
try {
40-
final Class<?> clazz = LoaderUtil.loadClass("java.util.Base64");
41-
final Class<?> encoderClazz = LoaderUtil.loadClass("java.util.Base64$Encoder");
42-
final Method method = clazz.getMethod("getEncoder");
43-
encoder = method.invoke(null);
44-
encodeMethod = encoderClazz.getMethod("encodeToString", byte[].class);
45-
} catch (Exception ex) {
46-
try {
47-
final Class<?> clazz = LoaderUtil.loadClass("javax.xml.bind.DataTypeConverter");
48-
encodeMethod = clazz.getMethod("printBase64Binary");
49-
} catch (Exception ex2) {
50-
LOGGER.error("Unable to create a Base64 Encoder", ex2);
51-
}
52-
}
53-
}
30+
private static final Base64.Encoder ENCODER = Base64.getEncoder();
5431

5532
private Base64Util() {}
5633

@@ -60,17 +37,6 @@ private Base64Util() {}
6037
*/
6138
@Deprecated
6239
public static String encode(final String str) {
63-
if (str == null) {
64-
return null;
65-
}
66-
final byte[] data = str.getBytes(Charset.defaultCharset());
67-
if (encodeMethod != null) {
68-
try {
69-
return (String) encodeMethod.invoke(encoder, data);
70-
} catch (Exception ex) {
71-
throw new LoggingException("Unable to encode String", ex);
72-
}
73-
}
74-
throw new LoggingException("No Encoder, unable to encode string");
40+
return str != null ? ENCODER.encodeToString(str.getBytes(Charset.defaultCharset())) : null;
7541
}
7642
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="changed">
6+
<issue id="3686" link="https://github.com/apache/logging-log4j2/issues/3686"/>
7+
<description format="asciidoc">Have org.apache.logging.log4j.util.Base64Util invoke java.util.Base64 directly instead of reflectively.</description>
8+
</entry>

0 commit comments

Comments
 (0)