pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1852045 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java
Date Thu, 24 Jan 2019 16:43:51 GMT
Author: tilman
Date: Thu Jan 24 16:43:51 2019
New Revision: 1852045

URL: http://svn.apache.org/viewvc?rev=1852045&view=rev
Log:
PDFBOX-4437: decode ASCII Hex and base64

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java?rev=1852045&r1=1852044&r2=1852045&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java Thu Jan 24 16:43:51
2019
@@ -17,8 +17,12 @@
 
 package org.apache.pdfbox.util;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.lang.reflect.Method;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * Utility functions for hex encoding.
@@ -27,6 +31,8 @@ import java.io.OutputStream;
  */
 public final class Hex
 {
+    private static final Log LOG = LogFactory.getLog(Hex.class);
+
     /**
      * for hex conversion.
      * 
@@ -173,4 +179,78 @@ public final class Hex
     {
         return b & 0x0F;
     }
+
+    /**
+     * Decode a base64 String.
+     *
+     * @param base64Value a base64 encoded String.
+     *
+     * @return the decoded String as a byte array.
+     *
+     * @throws IllegalArgumentException if this isn't a base64 encoded string.
+     */
+    public static byte[] decodeBase64(String base64Value) throws IllegalArgumentException
+    {
+        // https://stackoverflow.com/questions/469695/decode-base64-data-in-java
+        try
+        {
+            // jdk8 and higher? java.util.Base64.getDecoder().decode()
+            Class<?> b64Class = Class.forName("java.util.Base64");
+            Method getDecoderMethod = b64Class.getMethod("getDecoder");
+            Object base64Decoder = getDecoderMethod.invoke(b64Class);
+            Method decodeMethod = base64Decoder.getClass().getMethod("decode", String.class);
+            return (byte[]) decodeMethod.invoke(base64Decoder, base64Value.replaceAll("\\s",
""));
+        }
+        catch (ReflectiveOperationException ex)
+        {
+            LOG.debug(ex);
+        }
+        catch (SecurityException ex)
+        {
+            LOG.debug(ex);
+        }
+        try
+        {
+            // up to java7? javax.xml.bind.DatatypeConverter.parseBase64Binary()
+            Class<?> datatypeConverterClass = Class.forName("javax.xml.bind.DatatypeConverter");
+            Method parseBase64BinaryMethod = datatypeConverterClass.getMethod("parseBase64Binary",
String.class);
+            return (byte[]) parseBase64BinaryMethod.invoke(null, base64Value);
+        }
+        catch (ReflectiveOperationException ex)
+        {
+            LOG.debug(ex);
+        }
+        catch (SecurityException ex)
+        {
+            LOG.debug(ex);
+        }
+        LOG.error("Can't decode base64 value, try adding javax.xml.bind:jaxb-api to your
build");
+        return new byte[0];
+    }
+
+    /**
+     * Decodes a hex String into a byte array.
+     *
+     * @param s A String with ASCII hex.
+     * @return decoded byte array.
+     * @throws IOException
+     */
+    public static byte[] decodeHex(String s) throws IOException
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        for (int i = 0; i < s.length() - 1; i += 2)
+        {
+            String hexByte = s.substring(i, i + 2);
+            try
+            {
+                baos.write(Integer.parseInt(hexByte, 16)); // Byte.parseByte won't work with
"9C"
+            }
+            catch (NumberFormatException ex)
+            {
+                LOG.error("Can't parse " + hexByte + ", aborting decode", ex);
+                break;
+            }
+        }
+        return baos.toByteArray();
+    }
 }



Mime
View raw message