You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2018/03/27 09:48:28 UTC
svn commit: r1827821 -
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
Author: msahyoun
Date: Tue Mar 27 09:48:28 2018
New Revision: 1827821
URL: http://svn.apache.org/viewvc?rev=1827821&view=rev
Log:
PDFBOX-4158: try closing all open IO ressources even if there is an error when closing one of these
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java?rev=1827821&r1=1827820&r2=1827821&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java Tue Mar 27 09:48:28 2018
@@ -31,6 +31,9 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSDictionary;
@@ -71,6 +74,11 @@ import org.apache.pdfbox.pdmodel.interac
*/
public class PDFMergerUtility
{
+ /**
+ * Log instance.
+ */
+ private static final Log LOG = LogFactory.getLog(PDFMergerUtility.class);
+
private final List<InputStream> sources;
private final List<FileInputStream> fileInputStreams;
private String destinationFileName;
@@ -263,6 +271,14 @@ public class PDFMergerUtility
{
if (sources != null && !sources.isEmpty())
{
+ // Make sure that:
+ // - first Exception is kept
+ // - all PDDocuments are closed
+ // - all FileInputStreams are closed
+ // - there's a way to see which errors occured
+
+ IOException firstException = null;
+
List<PDDocument> tobeclosed = new ArrayList<>();
MemoryUsageSetting partitionedMemSetting = memUsageSetting != null ?
memUsageSetting.getPartitionedCopy(sources.size()+1) :
@@ -286,24 +302,63 @@ public class PDFMergerUtility
destination.getDocumentCatalog().setMetadata(destinationMetadata);
}
- if (destinationStream == null)
+ try
{
- destination.save(destinationFileName);
+ if (destinationStream == null)
+ {
+ destination.save(destinationFileName);
+ }
+ else
+ {
+ destination.save(destinationStream);
+ }
}
- else
+ catch (IOException ioe)
{
- destination.save(destinationStream);
+ LOG.warn("Couldn't save destination", ioe);
+ if (firstException == null)
+ {
+ firstException = ioe;
+ }
}
}
finally
{
for (PDDocument doc : tobeclosed)
{
- doc.close();
+ try
+ {
+ doc.close();
+ }
+ catch (IOException ioe)
+ {
+ LOG.warn("Couldn't close PDDocument", ioe);
+ if (firstException == null)
+ {
+ firstException = ioe;
+ }
+ }
}
for (FileInputStream stream : fileInputStreams)
{
- stream.close();
+ try
+ {
+ stream.close();
+ }
+ catch (IOException ioe)
+ {
+ LOG.warn("Couldn't close FileInputStream", ioe);
+ if (firstException == null)
+ {
+ firstException = ioe;
+ }
+ }
+ }
+
+ // rethrow first exception to keep method contract
+ if (firstException != null)
+ {
+ throw firstException;
}
}
}