add md5 in debug mode
*/
package org.apache.tika.parser.pdf;
+import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.file.Files;
import java.nio.file.Path;
+import java.security.DigestInputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
@@ -160,8 +165,9 @@ public class PDFParser extends AbstractParser implements RenderingParser, Initia
} else {
tstream = TikaInputStream.cast(stream);
}
- if (LOG.isDebugEnabled()) {
- LOG.debug("File: " + tstream.getFile() + ", length: " + tstream.getLength());
+ if (LOG.isDebugEnabled() && tstream != null) {
+ LOG.debug("File: " + tstream.getPath() + ", length: " + tstream.getLength() +
+ ", md5: " + calcMD5(tstream.getPath()));
}
password = getPassword(metadata, context);
MemoryUsageSetting memoryUsageSetting = MemoryUsageSetting.setupMainMemoryOnly();
@@ -769,6 +775,32 @@ public class PDFParser extends AbstractParser implements RenderingParser, Initia
defaultConfig.setImageStrategy(imageStrategy);
}
+ private String calcMD5(Path path) throws IOException {
+ MessageDigest md;
+ try {
+ md = MessageDigest.getInstance("MD5");
+ }
+ catch (NoSuchAlgorithmException ex) {
+ return "No MD5";
+ }
+
+ try (InputStream is = new BufferedInputStream(Files.newInputStream(path));
+ DigestInputStream dis = new DigestInputStream(is, md)) {
+ while (dis.read() >= 0)
+ ;
+ }
+ byte[] digest = md.digest();
+ StringBuilder hexString = new StringBuilder();
+ for (byte by : digest) {
+ int ih = 0xFF & by;
+ if (ih < 16) {
+ hexString.append('0');
+ }
+ hexString.append(Integer.toHexString(ih));
+ }
+ return hexString.toString();
+ }
+
/**
* Copied from AcroformDefaultFixup minus generation of appearances and handling of orphan
* widgets, which we don't need.
