Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
*/
package org.apache.logging.log4j.util;

import java.lang.reflect.Method;
import java.nio.charset.Charset;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LoggingException;
import org.apache.logging.log4j.status.StatusLogger;
import java.util.Base64;

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

private static final Logger LOGGER = StatusLogger.getLogger();

private static Method encodeMethod = null;
private static Object encoder = null;

static {
try {
final Class<?> clazz = LoaderUtil.loadClass("java.util.Base64");
final Class<?> encoderClazz = LoaderUtil.loadClass("java.util.Base64$Encoder");
final Method method = clazz.getMethod("getEncoder");
encoder = method.invoke(null);
encodeMethod = encoderClazz.getMethod("encodeToString", byte[].class);
} catch (Exception ex) {
try {
final Class<?> clazz = LoaderUtil.loadClass("javax.xml.bind.DataTypeConverter");
encodeMethod = clazz.getMethod("printBase64Binary");
} catch (Exception ex2) {
LOGGER.error("Unable to create a Base64 Encoder", ex2);
}
}
}
private static final Base64.Encoder ENCODER = Base64.getEncoder();

private Base64Util() {}

Expand All @@ -60,17 +37,6 @@ private Base64Util() {}
*/
@Deprecated
public static String encode(final String str) {
if (str == null) {
return null;
}
final byte[] data = str.getBytes(Charset.defaultCharset());
if (encodeMethod != null) {
try {
return (String) encodeMethod.invoke(encoder, data);
} catch (Exception ex) {
throw new LoggingException("Unable to encode String", ex);
}
}
throw new LoggingException("No Encoder, unable to encode string");
return str != null ? ENCODER.encodeToString(str.getBytes(Charset.defaultCharset())) : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://logging.apache.org/xml/ns"
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="changed">
<issue id="3686" link="https://github.com/apache/logging-log4j2/issues/3686"/>
<description format="asciidoc">Have org.apache.logging.log4j.util.Base64Util invoke java.util.Base64 directly instead of reflectively.</description>
</entry>