You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2016/06/21 06:19:18 UTC
svn commit: r1749432 - in
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: cos/COSString.java
filter/ASCIIHexFilter.java pdfwriter/COSWriter.java util/Hex.java
Author: lehmi
Date: Tue Jun 21 06:19:18 2016
New Revision: 1749432
URL: http://svn.apache.org/viewvc?rev=1749432&view=rev
Log:
PDFBOX-3382: move optimized hex encoding code to utility class, optimize remaining methods as well based on Tilmans changes
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSString.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/ASCIIHexFilter.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSString.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSString.java?rev=1749432&r1=1749431&r2=1749432&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSString.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSString.java Tue Jun 21 06:19:18 2016
@@ -227,12 +227,7 @@ public final class COSString extends COS
*/
public String toHexString()
{
- StringBuilder sb = new StringBuilder(bytes.length * 2);
- for (byte b : bytes)
- {
- sb.append(Hex.getString(b));
- }
- return sb.toString();
+ return Hex.getString(bytes);
}
/**
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/ASCIIHexFilter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/ASCIIHexFilter.java?rev=1749432&r1=1749431&r2=1749432&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/ASCIIHexFilter.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/ASCIIHexFilter.java Tue Jun 21 06:19:18 2016
@@ -116,7 +116,7 @@ final class ASCIIHexFilter extends Filte
int byteRead;
while ((byteRead = input.read()) != -1)
{
- encoded.write(Hex.getBytes((byte)byteRead));
+ Hex.writeHexByte((byte)byteRead, encoded);
}
encoded.flush();
}
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=1749432&r1=1749431&r2=1749432&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 Tue Jun 21 06:19:18 2016
@@ -67,6 +67,7 @@ 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.
@@ -158,11 +159,6 @@ 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
@@ -772,16 +768,14 @@ public class COSWriter implements ICOSVi
new ByteArrayInputStream(signBuffer));
// sign the bytes
- byte[] sign = signatureInterface.sign(signStream);
- String signature = new COSString(sign).toHexString();
+ byte[] signatureBytes = Hex.getBytes(signatureInterface.sign(signStream));
// substract 2 bytes because of the enclosing "<>"
- if (signature.length() > signatureLength - 2)
+ if (signatureBytes.length > signatureLength - 2)
{
throw new IOException("Can't write signature, not enough space");
}
// overwrite the signature Contents in the buffer
- byte[] signatureBytes = signature.getBytes(Charsets.ISO_8859_1);
System.arraycopy(signatureBytes, 0, buffer, bufSignatureOffset + 1, signatureBytes.length);
// write the data to the incremental output stream
@@ -1427,12 +1421,7 @@ public class COSWriter implements ICOSVi
{
// write hex string
output.write('<');
- for (byte b : bytes)
- {
- // https://stackoverflow.com/questions/2817752/java-code-to-convert-byte-to-hexadecimal
- output.write(HEXES[(b & 0xF0) >> 4]);
- output.write(HEXES[b & 0x0F]);
- }
+ Hex.writeHexBytes(bytes, output);
output.write('>');
}
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java?rev=1749432&r1=1749431&r2=1749432&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java Tue Jun 21 06:19:18 2016
@@ -17,6 +17,9 @@
package org.apache.pdfbox.util;
+import java.io.IOException;
+import java.io.OutputStream;
+
/**
* Utility functions for hex encoding.
*
@@ -24,6 +27,16 @@ package org.apache.pdfbox.util;
*/
public final class Hex
{
+ /**
+ * for hex conversion.
+ *
+ * https://stackoverflow.com/questions/2817752/java-code-to-convert-byte-to-hexadecimal
+ *
+ */
+ private static final String HEXES_STRING = "0123456789ABCDEF";
+
+ private static final byte[] HEXES = HEXES_STRING.getBytes(Charsets.US_ASCII);
+
private Hex() {}
/**
@@ -31,7 +44,21 @@ public final class Hex
*/
public static String getString(byte b)
{
- return Integer.toHexString(0x100 | b & 0xff).substring(1).toUpperCase();
+ char[] chars = new char[]{HEXES_STRING.charAt(getHighNibble(b)), HEXES_STRING.charAt(getLowNibble(b))};
+ return new String(chars);
+ }
+
+ /**
+ * Returns a hex string of the given byte array.
+ */
+ public static String getString(byte[] bytes)
+ {
+ StringBuilder string = new StringBuilder(bytes.length * 2);
+ for (byte b : bytes)
+ {
+ string.append(HEXES_STRING.charAt(getHighNibble(b))).append(HEXES_STRING.charAt(getLowNibble(b)));
+ }
+ return string.toString();
}
/**
@@ -39,6 +66,68 @@ public final class Hex
*/
public static byte[] getBytes(byte b)
{
- return getString(b).getBytes(Charsets.US_ASCII);
+ return new byte[]{HEXES[getHighNibble(b)], HEXES[getLowNibble(b)]};
+ }
+
+ /**
+ * Returns the bytes corresponding to the ASCII hex encoding of the given bytes.
+ */
+ public static byte[] getBytes(byte[] bytes)
+ {
+ byte[] asciiBytes = new byte[bytes.length*2];
+ for(int i=0; i< bytes.length; i++)
+ {
+ asciiBytes[i*2] = HEXES[getHighNibble(bytes[i])];
+ asciiBytes[i*2+1] = HEXES[getLowNibble(bytes[i])];
+ }
+ return asciiBytes;
+ }
+
+ /**
+ * Writes the given byte as hex value to the given output stream.
+ * @param b the byte to be written
+ * @param output the output stream to be written to
+ * @throws IOException exception if anything went wrong
+ */
+ public static void writeHexByte(byte b, OutputStream output) throws IOException
+ {
+ output.write(HEXES[getHighNibble(b)]);
+ output.write(HEXES[getLowNibble(b)]);
+ }
+
+ /**
+ * Writes the given byte array as hex value to the given output stream.
+ * @param b the byte array to be written
+ * @param output the output stream to be written to
+ * @throws IOException exception if anything went wrong
+ */
+ public static void writeHexBytes(byte[] bytes, OutputStream output) throws IOException
+ {
+ for (byte b : bytes)
+ {
+ writeHexByte(b, output);
+ }
+ }
+
+ /**
+ * Get the high nibble of the given byte.
+ *
+ * @param b the given byte
+ * @return the high nibble
+ */
+ private static int getHighNibble(byte b)
+ {
+ return (b & 0xF0) >> 4;
+ }
+
+ /**
+ * Get the low nibble of the given byte.
+ *
+ * @param b the given byte
+ * @return the low nibble
+ */
+ private static int getLowNibble(byte b)
+ {
+ return b & 0x0F;
}
}