You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2016/06/18 14:10:48 UTC

svn commit: r1749052 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java

Author: tilman
Date: Sat Jun 18 14:10:48 2016
New Revision: 1749052

URL: http://svn.apache.org/viewvc?rev=1749052&view=rev
Log:
PDFBOX-3382: speed up hex writing, don't check for hex if it is already forced

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java?rev=1749052&r1=1749051&r2=1749052&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java Sat Jun 18 14:10:48 2016
@@ -67,7 +67,6 @@ import org.apache.pdfbox.pdmodel.encrypt
 import org.apache.pdfbox.pdmodel.fdf.FDFDocument;
 import org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureInterface;
 import org.apache.pdfbox.util.Charsets;
-import org.apache.pdfbox.util.Hex;
 
 /**
  * This class acts on a in-memory representation of a PDF document.
@@ -159,6 +158,11 @@ public class COSWriter implements ICOSVi
      */
     public static final byte[] ENDSTREAM = "endstream".getBytes(Charsets.US_ASCII);
     
+    /**
+     * for hex conversion.
+     */
+    private static final byte[] HEXES = "0123456789ABCDEF".getBytes(Charsets.US_ASCII);
+    
     private final NumberFormat formatXrefOffset = new DecimalFormat("0000000000");
 
     // the decimal format for the xref object generation number data
@@ -1380,19 +1384,22 @@ public class COSWriter implements ICOSVi
     {
         // check for non-ASCII characters
         boolean isASCII = true;
-        for (byte b : bytes)
+        if (!forceHex)
         {
-            // if the byte is negative then it is an eight bit byte and is outside the ASCII range
-            if (b < 0)
-            {
-                isASCII = false;
-                break;
-            }
-            // PDFBOX-3107 EOL markers within a string are troublesome
-            if (b == 0x0d || b == 0x0a)
+            for (byte b : bytes)
             {
-                isASCII = false;
-                break;
+                // if the byte is negative then it is an eight bit byte and is outside the ASCII range
+                if (b < 0)
+                {
+                    isASCII = false;
+                    break;
+                }
+                // PDFBOX-3107 EOL markers within a string are troublesome
+                if (b == 0x0d || b == 0x0a)
+                {
+                    isASCII = false;
+                    break;
+                }
             }
         }
 
@@ -1422,7 +1429,9 @@ public class COSWriter implements ICOSVi
             output.write('<');
             for (byte b : bytes)
             {
-                output.write(Hex.getBytes(b));
+                // https://stackoverflow.com/questions/2817752/java-code-to-convert-byte-to-hexadecimal
+                output.write(HEXES[(b & 0xF0) >> 4]);
+                output.write(HEXES[b & 0x0F]);
             }
             output.write('>');
         }