pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jahew...@apache.org
Subject svn commit: r1651536 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: cos/ io/ pdfparser/ pdmodel/
Date Tue, 13 Jan 2015 23:36:46 GMT
Author: jahewson
Date: Tue Jan 13 23:36:46 2015
New Revision: 1651536

URL: http://svn.apache.org/r1651536
Log:
PDFBOX-2592: Don't overwrite COS objects with null once COSDocument is closed

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBufferedFileInputStream.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessFile.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessRead.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFStreamParser.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/XrefTrailerResolver.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java?rev=1651536&r1=1651535&r2=1651536&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java Tue Jan 13 23:36:46
2015
@@ -510,40 +510,32 @@ public class COSDocument extends COSBase
     {
         if (!closed) 
         {
-            if (trailer != null)
-            {
-                trailer.clear();
-                trailer = null;
-            }
-            // Clear object pool
+            // close all open I/O streams
             List<COSObject> list = getObjects();
-            if (list != null && !list.isEmpty()) 
+            if (list != null) 
             {
                 for (COSObject object : list) 
                 {
                     COSBase cosObject = object.getObject();
-                    // clear the resources of the pooled objects
                     if (cosObject instanceof COSStream)
                     {
                         ((COSStream)cosObject).close();
                     }
-                    else if (cosObject instanceof COSDictionary)
-                    {
-                        ((COSDictionary)cosObject).clear();
-                    }
-                    else if (cosObject instanceof COSArray)
-                    {
-                        ((COSArray)cosObject).clear();
-                    }
-                    // TODO are there other kind of COSObjects to be cleared?
                 }
-                list.clear();
             }
             closed = true;
         }
     }
 
     /**
+     * Returns true if this document has been closed.
+     */
+    public boolean isClosed()
+    {
+        return closed;
+    }
+
+    /**
      * Warn the user in the finalizer if he didn't close the PDF document. The method also
      * closes the document just in case, to avoid abandoned temporary files. It's still a
good
      * idea for the user to close the PDF document at the earliest possible to conserve resources.

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java?rev=1651536&r1=1651535&r2=1651536&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java Tue Jan 13 23:36:46
2015
@@ -56,7 +56,7 @@ public class COSStream extends COSDictio
     /**
      * internal buffer, either held in memory or within a scratch file.
      */
-    private RandomAccess buffer;
+    private final RandomAccess buffer;
     /**
      * The stream with all of the filters applied.
      */
@@ -100,9 +100,9 @@ public class COSStream extends COSDictio
         super();
         if (useScratchFiles)
         {
-            createScratchFile(scratchDirectory);
+            buffer = createScratchFile(scratchDirectory);
         }
-        if (buffer == null)
+        else
         {
             buffer = new RandomAccessBuffer();
         }
@@ -121,9 +121,9 @@ public class COSStream extends COSDictio
         super( dictionary );
         if (useScratchFiles)
         {
-            createScratchFile(scratchDirectory);
+            buffer = createScratchFile(scratchDirectory);
         }
-        if (buffer == null)
+        else
         {
             buffer = new RandomAccessBuffer();
         }
@@ -135,18 +135,19 @@ public class COSStream extends COSDictio
      * @param scratchDirectory directory to be used to create the scratch file. If null java.io.temp
is used instead.
      * 
      */
-    private void createScratchFile(File scratchDirectory)
+    private RandomAccess createScratchFile(File scratchDirectory)
     {
         try 
         {
             File scratchFile = File.createTempFile("PDFBox", null, scratchDirectory);
             // mark scratch file to deleted automatically after usage
             scratchFile.deleteOnExit();
-            buffer = new RandomAccessFile(scratchFile, "rw");
+            return new RandomAccessFile(scratchFile, "rw");
         }
         catch (IOException exception)
         {
             LOG.error("Can't create temp file, using memory buffer instead", exception);
+            return new RandomAccessBuffer();
         }
     }
 
@@ -173,6 +174,12 @@ public class COSStream extends COSDictio
      */
     public InputStream getFilteredStream() throws IOException
     {
+        if (buffer.isClosed())
+        {
+            throw new IOException("COSStream has been closed and cannot be read. " +
+                                  "Perhaps its enclosing PDDocument has been closed?");
+        }
+
         if( filteredStream == null )
         {
             doEncode();
@@ -238,6 +245,12 @@ public class COSStream extends COSDictio
      */
     public InputStream getUnfilteredStream() throws IOException
     {
+        if (buffer.isClosed())
+        {
+            throw new IOException("COSStream has been closed and cannot be read. " +
+                                "Perhaps its enclosing PDDocument has been closed?");
+        }
+
         InputStream retval;
         if( unFilteredStream == null )
         {
@@ -593,28 +606,21 @@ public class COSStream extends COSDictio
     }
     
     @Override
-    public void close()
+    public void close() throws IOException
     {
-        try
+        if (buffer != null)
         {
-            if (buffer != null)
-            {
-                buffer.close();
-                buffer = null;
-            }
-        }
-        catch (IOException exception)
-        {
-            LOG.error("Exception occured when closing the file.", exception);
+            buffer.close();
         }
+
         if (filteredStream != null)
         {
-            IOUtils.closeQuietly(filteredStream);
+            filteredStream.close();
         }
+
         if (unFilteredStream != null)
         {
-            IOUtils.closeQuietly(unFilteredStream);
+            unFilteredStream.close();
         }
-        clear();
     }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java?rev=1651536&r1=1651535&r2=1651536&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessBuffer.java Tue Jan
13 23:36:46 2015
@@ -329,4 +329,10 @@ public class RandomAccessBuffer implemen
         }
         
     }
+
+    @Override
+    public boolean isClosed()
+    {
+        return currentBuffer == null;
+    }
 }

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=1651536&r1=1651535&r2=1651536&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
Tue Jan 13 23:36:46 2015
@@ -70,6 +70,7 @@ extends InputStream implements RandomAcc
     private final RandomAccessFile raFile;
     private final long fileLength;
     private long fileOffset = 0;
+    private boolean isClosed;
 
     // ------------------------------------------------------------------------
     /** Create input stream instance for given file. */
@@ -253,5 +254,12 @@ extends InputStream implements RandomAcc
     {
         raFile.close();
         pageCache.clear();
+        isClosed = true;
+    }
+
+    @Override
+    public boolean isClosed()
+    {
+        return isClosed;
     }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessFile.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessFile.java?rev=1651536&r1=1651535&r2=1651536&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessFile.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessFile.java Tue Jan 13
23:36:46 2015
@@ -31,6 +31,7 @@ import java.io.IOException;
 public class RandomAccessFile implements RandomAccess, Closeable
 {
     private java.io.RandomAccessFile ras;
+    private boolean isClosed;
 
     /**
      * Constructor.
@@ -51,6 +52,7 @@ public class RandomAccessFile implements
     public void close() throws IOException
     {
         ras.close();
+        isClosed = true;
     }
 
     /**
@@ -97,6 +99,12 @@ public class RandomAccessFile implements
         return ras.length();
     }
 
+    @Override
+    public boolean isClosed()
+    {
+        return isClosed;
+    }
+
     /**
      * {@inheritDoc}
      */

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessRead.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessRead.java?rev=1651536&r1=1651535&r2=1651536&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessRead.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/RandomAccessRead.java Tue Jan 13
23:36:46 2015
@@ -52,4 +52,8 @@ public interface RandomAccessRead extend
      */
     public long length() throws IOException;
 
+    /**
+     * Returns true if this stream has been closed.
+     */
+    public boolean isClosed();
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java?rev=1651536&r1=1651535&r2=1651536&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java Tue Jan
13 23:36:46 2015
@@ -19,6 +19,7 @@ package org.apache.pdfbox.pdfparser;
 import java.io.BufferedInputStream;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -49,7 +50,7 @@ import org.apache.pdfbox.persistence.uti
  * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
  * @version $Revision$
  */
-public abstract class BaseParser
+public abstract class BaseParser implements Closeable
 {
 
     private static final long OBJECT_NUMBER_THRESHOLD = 10000000000L;
@@ -1694,16 +1695,12 @@ public abstract class BaseParser
         }
     }
 
-    /**
-     * Release all used resources.
-     */
-    public void clearResources()
+    @Override
+    public void close() throws IOException
     {
-        document = null;
         if (pdfSource != null)
         {
-            IOUtils.closeQuietly(pdfSource);
-            pdfSource = null;
+            pdfSource.close();
         }
     }
 

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=1651536&r1=1651535&r2=1651536&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 Tue Jan 13
23:36:46 2015
@@ -986,22 +986,4 @@ public class PDFParser extends BaseParse
         }
         return false;
     }
-    
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void clearResources() {
-        super.clearResources();
-        if (conflictList != null)
-        {
-            conflictList.clear();
-            conflictList = null;
-        }
-        if (xrefTrailerResolver != null)
-        {
-            xrefTrailerResolver.clearResources();
-            xrefTrailerResolver = null;
-        }
-    }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFStreamParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFStreamParser.java?rev=1651536&r1=1651535&r2=1651536&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFStreamParser.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFStreamParser.java Tue
Jan 13 23:36:46 2015
@@ -122,16 +122,6 @@ public class PDFStreamParser extends Bas
     }
 
     /**
-     * This will close the underlying pdfSource object.
-     * 
-     * @throws IOException If there is an error releasing resources.
-     */
-    public void close() throws IOException
-    {
-        pdfSource.close();
-    }
-
-    /**
      * This will get an iterator which can be used to parse the stream
      * one token after the other.
      *
@@ -521,18 +511,4 @@ public class PDFStreamParser extends Bas
     {
         return isSpaceOrReturn( pdfSource.peek() );
     }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void clearResources()
-    {
-        super.clearResources();
-        if (streamObjects != null)
-        {
-            streamObjects.clear();
-            streamObjects = null;
-        }
-    }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/XrefTrailerResolver.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/XrefTrailerResolver.java?rev=1651536&r1=1651535&r2=1651536&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/XrefTrailerResolver.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/XrefTrailerResolver.java
Tue Jan 13 23:36:46 2015
@@ -74,22 +74,6 @@ public class XrefTrailerResolver
         {
             xrefType = XRefType.TABLE;
         }
-        
-        /**
-         * Release all used resources.
-         */
-        public void clearResources()
-        {
-            if (trailer != null)
-            {
-                trailer.clear();
-                trailer = null;
-            }
-            if (xrefTable != null)
-            {
-                xrefTable.clear();
-            }
-        }
     }
 
     /** 
@@ -347,26 +331,4 @@ public class XrefTrailerResolver
         }
         return refObjNrs;
     }
-    
-    /**
-     * Release all used resources.
-     */
-    public void clearResources()
-    {
-        if (curXrefTrailerObj != null)
-        {
-            curXrefTrailerObj.clearResources();
-            curXrefTrailerObj = null;
-        }
-        if (resolvedXrefTrailer != null)
-        {
-            resolvedXrefTrailer.clearResources();
-            resolvedXrefTrailer = null;
-        }
-        if (bytePosToXrefMap != null)
-        {
-            bytePosToXrefMap.clear();
-        }
-    }
-
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1651536&r1=1651535&r2=1651536&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Tue Jan 13
23:36:46 2015
@@ -70,7 +70,7 @@ import org.apache.pdfbox.pdmodel.interac
  */
 public class PDDocument implements Closeable
 {
-    private COSDocument document;
+    private final COSDocument document;
 
     // cached values
     private PDDocumentInformation documentInformation;
@@ -88,7 +88,7 @@ public class PDDocument implements Close
     private Long documentId;
 
     // the PDF parser
-    private BaseParser parser;
+    private final BaseParser parser;
 
     // the File to read incremental data from
     private File incrementalFile;
@@ -103,6 +103,7 @@ public class PDDocument implements Close
     public PDDocument()
     {
         document = new COSDocument();
+        parser = null;
 
         // First we need a trailer
         COSDictionary trailer = new COSDictionary();
@@ -1060,7 +1061,7 @@ public class PDDocument implements Close
      */
     public void save(OutputStream output) throws IOException
     {
-        if (document == null)
+        if (document.isClosed())
         {
             throw new IOException("Cannot save a document which has been closed");
         }
@@ -1140,20 +1141,17 @@ public class PDDocument implements Close
     @Override
     public void close() throws IOException
     {
-        documentCatalog = null;
-        documentInformation = null;
-        encryption = null;
-        if (document != null)
+        if (!document.isClosed())
         {
+            // close all intermediate I/O streams
             document.close();
-            document = null;
-        }
-        if (parser != null)
-        {
-            parser.clearResources();
-            parser = null;
+            
+            // close the source PDF stream, if we read from one
+            if (parser != null)
+            {
+                parser.close();
+            }
         }
-        accessPermission = null;
     }
 
     /**



Mime
View raw message