1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.config.plugins.convert;
18
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21
22 import org.apache.logging.log4j.Logger;
23 import org.apache.logging.log4j.status.StatusLogger;
24 import org.apache.logging.log4j.util.LoaderUtil;
25
26
27
28
29 public class Base64Converter {
30
31 private static final Logger LOGGER = StatusLogger.getLogger();
32 private static Method method = null;
33 private static Object decoder = null;
34
35 static {
36 try {
37
38 Class<?> clazz = LoaderUtil.loadClass("java.util.Base64");
39 final Method getDecoder = clazz.getMethod("getDecoder", (Class[]) null);
40 decoder = getDecoder.invoke(null, (Object[]) null);
41 clazz = decoder.getClass();
42 method = clazz.getMethod("decode", String.class);
43 } catch (final ClassNotFoundException ex) {
44
45 } catch (final NoSuchMethodException ex) {
46
47 } catch (final IllegalAccessException ex) {
48
49 } catch (final InvocationTargetException ex) {
50
51 }
52 if (method == null) {
53 try {
54
55 final Class<?> clazz = LoaderUtil.loadClass("javax.xml.bind.DatatypeConverter");
56 method = clazz.getMethod("parseBase64Binary", String.class);
57 } catch (final ClassNotFoundException ex) {
58 LOGGER.error("No Base64 Converter is available");
59 } catch (final NoSuchMethodException ex) {
60
61 }
62 }
63 }
64
65 public static byte[] parseBase64Binary(final String encoded) {
66 if (method == null) {
67 LOGGER.error("No base64 converter");
68 } else {
69 try {
70 return (byte[]) method.invoke(decoder, encoded);
71 } catch (final IllegalAccessException ex) {
72 LOGGER.error("Error decoding string - " + ex.getMessage());
73 } catch (final InvocationTargetException ex) {
74 LOGGER.error("Error decoding string - " + ex.getMessage());
75 }
76 }
77 return new byte[0];
78 }
79 }