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 2015/06/05 16:34:06 UTC
svn commit: r1683767 - in
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox:
io/RandomAccessBufferedFileInputStream.java pdfparser/PDFParser.java
Author: lehmi
Date: Fri Jun 5 14:34:05 2015
New Revision: 1683767
URL: http://svn.apache.org/r1683767
Log:
PDFBOX-2301: moved the temp file handling from PDFParser to RandomAccessBufferedFileInputStream
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBufferedFileInputStream.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBufferedFileInputStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBufferedFileInputStream.java?rev=1683767&r1=1683766&r2=1683767&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBufferedFileInputStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBufferedFileInputStream.java Fri Jun 5 14:34:05 2015
@@ -17,6 +17,8 @@
package org.apache.pdfbox.io;
import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
@@ -36,11 +38,16 @@ import java.util.Map;
public class RandomAccessBufferedFileInputStream
extends InputStream implements RandomAccessRead
{
+ /**
+ * The prefix for the temp file being used.
+ */
+ private static final String TMP_FILE_PREFIX = "tmpPDFBox";
private int pageSizeShift = 12;
private int pageSize = 1 << pageSizeShift;
private long pageOffsetMask = -1L << pageSizeShift;
private int maxCachedPages = 1000;
+ private File tempFile;
private byte[] lastRemovedCachePage = null;
@@ -71,13 +78,58 @@ extends InputStream implements RandomAcc
private long fileOffset = 0;
private boolean isClosed;
- /** Create input stream instance for given file. */
- public RandomAccessBufferedFileInputStream( File file ) throws IOException
+ /**
+ * Create a random access input stream instance for the given file.
+ *
+ * @param the file to be read
+ * @exception if the given file can't be found
+ */
+ public RandomAccessBufferedFileInputStream( File file ) throws FileNotFoundException
{
raFile = new RandomAccessFile(file, "r");
fileLength = file.length();
+ }
+
+ /**
+ * Create a random access input stream for the given input stream
+ * by copying the data to a temporary file.
+ *
+ * @param the input stream to be read
+ * @exception if something went wrong while creating the temporary file
+ */
+ public RandomAccessBufferedFileInputStream( InputStream input ) throws IOException
+ {
+ tempFile = createTmpFile(input);
+ fileLength = tempFile.length();
+ raFile = new RandomAccessFile(tempFile, "r");
+ }
+
+ private File createTmpFile(InputStream input) throws IOException
+ {
+ FileOutputStream fos = null;
+ try
+ {
+ File tmpFile = File.createTempFile(TMP_FILE_PREFIX, ".pdf");
+ fos = new FileOutputStream(tmpFile);
+ IOUtils.copy(input, fos);
+ return tmpFile;
+ }
+ finally
+ {
+ IOUtils.closeQuietly(input);
+ IOUtils.closeQuietly(fos);
+ }
+ }
- seek(0);
+ /**
+ * Remove the temporary file. A temporary file is created if this class is instantiated with an InputStream
+ */
+ private void deleteTempFile()
+ {
+ if (tempFile != null)
+ {
+ tempFile.delete();
+ }
}
/** Returns offset in file at which next byte would be read. */
@@ -238,6 +290,7 @@ extends InputStream implements RandomAcc
public void close() throws IOException
{
raFile.close();
+ deleteTempFile();
pageCache.clear();
isClosed = true;
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java?rev=1683767&r1=1683766&r2=1683767&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java Fri Jun 5 14:34:05 2015
@@ -31,7 +31,6 @@ import org.apache.pdfbox.cos.COSNull;
import org.apache.pdfbox.cos.COSObject;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.io.RandomAccessBufferedFileInputStream;
-import org.apache.pdfbox.io.RandomAccessFile;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.DecryptionMaterial;
@@ -49,8 +48,6 @@ public class PDFParser extends COSParser
private AccessPermission accessPermission;
- private File tempPDFFile;
-
/**
* Constructs parser for given file using memory buffer.
*
@@ -252,9 +249,7 @@ public class PDFParser extends COSParser
{
if (useScratchFiles)
{
- tempPDFFile = createTmpFile(input);
- fileLen = tempPDFFile.length();
- pdfSource = new RandomAccessBufferedFileInputStream(tempPDFFile);
+ pdfSource = new RandomAccessBufferedFileInputStream(input);
}
else
{
@@ -361,7 +356,6 @@ public class PDFParser extends COSParser
{
IOUtils.closeQuietly(pdfSource);
IOUtils.closeQuietly(keyStoreInputStream);
- deleteTempFile();
if (exceptionOccurred && document != null)
{
@@ -370,27 +364,6 @@ public class PDFParser extends COSParser
}
}
}
-
- /**
- * Remove the temporary file. A temporary file is created if this class is instantiated with an InputStream
- */
- private void deleteTempFile()
- {
- if (tempPDFFile != null)
- {
- try
- {
- if (!tempPDFFile.delete())
- {
- LOG.warn("Temporary file '" + tempPDFFile.getName() + "' can't be deleted");
- }
- }
- catch (SecurityException e)
- {
- LOG.warn("Temporary file '" + tempPDFFile.getName() + "' can't be deleted", e);
- }
- }
- }
/**
* Prepare for decryption.