pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1553018 - in /pdfbox/branches/1.8: ./ pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java
Date Sun, 22 Dec 2013 18:53:34 GMT
Author: lehmi
Date: Sun Dec 22 18:53:34 2013
New Revision: 1553018

URL: http://svn.apache.org/r1553018
Log:
PDFBOX-1777: release/close all resources based on a proposal by Robert Scharpf

Modified:
    pdfbox/branches/1.8/   (props changed)
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java

Propchange: pdfbox/branches/1.8/
------------------------------------------------------------------------------
  Merged /pdfbox/trunk:r1553017

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java?rev=1553018&r1=1553017&r2=1553018&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java Sun Dec
22 18:53:34 2013
@@ -40,7 +40,7 @@ import org.apache.pdfbox.persistence.uti
  * close() on this object when you are done using it!!
  *
  * @author <a href="ben@benlitchfield.com">Ben Litchfield</a>
- * @version $Revision: 1.28 $
+ * 
  */
 public class COSDocument extends COSBase
 {
@@ -566,6 +566,35 @@ public class COSDocument extends COSBase
             {
                 tmpFile.delete();
             }
+            if (trailer != null)
+            {
+            	trailer.clear();
+            	trailer = null;
+            }
+            // Clear object pool
+            List<COSObject> list = getObjects();
+            if (list != null && !list.isEmpty()) 
+            {
+                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;
         }
     }

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java?rev=1553018&r1=1553017&r2=1553018&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java Sun Dec
22 18:53:34 2013
@@ -24,6 +24,8 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.util.List;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.pdfbox.filter.Filter;
 import org.apache.pdfbox.filter.FilterManager;
 import org.apache.pdfbox.pdfparser.PDFStreamParser;
@@ -41,6 +43,11 @@ import org.apache.pdfbox.io.RandomAccess
  */
 public class COSStream extends COSDictionary
 {
+    /**
+     * Log instance.
+     */
+    private static final Log LOG = LogFactory.getLog(COSStream.class);
+
     private static final int BUFFER_SIZE=16384;
 
     private RandomAccess file;
@@ -273,6 +280,7 @@ public class COSStream extends COSDictio
             //if the length is zero then don't bother trying to decode
             //some filters don't work when attempting to decode
             //with a zero length stream.  See zlib_error_01.pdf
+        	IOUtils.closeQuietly(unFilteredStream);
             unFilteredStream = new RandomAccessFileOutputStream( file );
             done = true;
         }
@@ -288,6 +296,7 @@ public class COSStream extends COSDictio
                 {
                     input = new BufferedInputStream(
                         new RandomAccessFileInputStream( file, position, length ), BUFFER_SIZE
);
+                	IOUtils.closeQuietly(unFilteredStream);
                     unFilteredStream = new RandomAccessFileOutputStream( file );
                     filter.decode( input, unFilteredStream, this, filterIndex );
                     done = true;
@@ -315,6 +324,7 @@ public class COSStream extends COSDictio
                     {
                         input = new BufferedInputStream(
                             new RandomAccessFileInputStream( file, position, length ), BUFFER_SIZE
);
+                    	IOUtils.closeQuietly(unFilteredStream);
                         unFilteredStream = new RandomAccessFileOutputStream( file );
                         filter.decode( input, unFilteredStream, this, filterIndex );
                         done = true;
@@ -383,6 +393,7 @@ public class COSStream extends COSDictio
         InputStream input = new BufferedInputStream(
             new RandomAccessFileInputStream( file, filteredStream.getPosition(),
                                                    filteredStream.getLength() ), BUFFER_SIZE
);
+        IOUtils.closeQuietly(filteredStream);
         filteredStream = new RandomAccessFileOutputStream( file );
         filter.encode( input, filteredStream, this, filterIndex );
         IOUtils.closeQuietly(input);
@@ -413,8 +424,10 @@ public class COSStream extends COSDictio
      */
     public OutputStream createFilteredStream() throws IOException
     {
+    	IOUtils.closeQuietly(unFilteredStream);
+    	unFilteredStream = null;
+    	IOUtils.closeQuietly(filteredStream);
         filteredStream = new RandomAccessFileOutputStream( file );
-        unFilteredStream = null;
         return new BufferedOutputStream( filteredStream, BUFFER_SIZE );
     }
 
@@ -431,9 +444,11 @@ public class COSStream extends COSDictio
      */
     public OutputStream createFilteredStream( COSBase expectedLength ) throws IOException
     {
+      	IOUtils.closeQuietly(unFilteredStream);
+       	unFilteredStream = null;
+    	IOUtils.closeQuietly(filteredStream);
         filteredStream = new RandomAccessFileOutputStream( file );
         filteredStream.setExpectedLength( expectedLength );
-        unFilteredStream = null;
         return new BufferedOutputStream( filteredStream, BUFFER_SIZE );
     }
 
@@ -448,6 +463,7 @@ public class COSStream extends COSDictio
     {
         setItem(COSName.FILTER, filters);
         // kill cached filtered streams
+    	IOUtils.closeQuietly(filteredStream);
         filteredStream = null;
     }
 
@@ -460,8 +476,35 @@ public class COSStream extends COSDictio
      */
     public OutputStream createUnfilteredStream() throws IOException
     {
-        unFilteredStream = new RandomAccessFileOutputStream( file );
+        IOUtils.closeQuietly(filteredStream);
         filteredStream = null;
+    	IOUtils.closeQuietly(unFilteredStream);
+        unFilteredStream = new RandomAccessFileOutputStream( file );
         return new BufferedOutputStream( unFilteredStream, BUFFER_SIZE );
     }
+    
+    public void close()
+    {
+    	try
+    	{
+    		if (file != null)
+    		{
+    			file.close();
+    			file = null;
+    		}
+    	}
+    	catch (IOException exception)
+    	{
+    		LOG.error("Exception occured when closing the file.", exception);
+    	}
+    	if (filteredStream != null)
+    	{
+    		IOUtils.closeQuietly(filteredStream);
+    	}
+    	if (unFilteredStream != null)
+    	{
+    		IOUtils.closeQuietly(unFilteredStream);
+    	}
+    	clear();
+    }
 }



Mime
View raw message