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 18:07:42 UTC
svn commit: r1827851 - in
/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox:
cos/COSDocument.java io/IOUtils.java multipdf/PDFMergerUtility.java
pdmodel/PDDocument.java
Author: msahyoun
Date: Tue Mar 27 18:07:42 2018
New Revision: 1827851
URL: http://svn.apache.org/viewvc?rev=1827851&view=rev
Log:
PDFBOX-4158: refactor; move common code to IOUtils
Modified:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/IOUtils.java
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java?rev=1827851&r1=1827850&r2=1827851&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java Tue Mar 27 18:07:42 2018
@@ -25,6 +25,8 @@ import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+
+import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.io.ScratchFile;
import org.apache.pdfbox.pdfparser.PDFObjectStreamParser;
@@ -441,53 +443,16 @@ public class COSDocument extends COSBase
COSBase cosObject = object.getObject();
if (cosObject instanceof COSStream)
{
- COSStream cosStream = (COSStream) cosObject;
- try
- {
- cosStream.close();
- }
- catch (IOException ioe)
- {
- LOG.warn("Error closing COSStream", ioe);
- if (firstException == null)
- {
- firstException = ioe;
- }
-
- }
+ firstException = IOUtils.closeAndLogException((COSStream) cosObject, LOG, "COSStream", firstException);
}
}
for (COSStream stream : streams)
{
- try
- {
- stream.close();
- }
- catch (IOException ioe)
- {
- LOG.warn("Error closing COSStream", ioe);
- if (firstException == null)
- {
- firstException = ioe;
- }
-
- }
+ firstException = IOUtils.closeAndLogException(stream, LOG, "COSStream", firstException);
}
if (scratchFile != null)
{
- try
- {
- scratchFile.close();
- }
- catch (IOException ioe)
- {
- LOG.warn("Error closing ScratchFile", ioe);
- if (firstException == null)
- {
- firstException = ioe;
- }
-
- }
+ firstException = IOUtils.closeAndLogException(scratchFile, LOG, "ScratchFile", firstException);
}
closed = true;
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/IOUtils.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/IOUtils.java?rev=1827851&r1=1827850&r2=1827851&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/IOUtils.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/io/IOUtils.java Tue Mar 27 18:07:42 2018
@@ -25,6 +25,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import org.apache.commons.logging.Log;
+
/**
* This class contains various I/O-related methods.
*/
@@ -115,4 +117,36 @@ public final class IOUtils
// ignore
}
}
+
+ /**
+ * Try to close an IO resource and log and return if there was an exception.
+ *
+ * <p>An exception is only returned if the IOException passed in is null.
+ *
+ * @param closeable to be closed
+ * @param logger the logger to be used so that logging appears under that log instance
+ * @param resourceName the name to appear in the log output
+ * @param initialException to be closed
+ * @return the IOException is there was any but only if initialException is null
+ */
+ public static IOException closeAndLogException(Closeable closeable, Log logger, String resourceName, IOException initialException)
+ {
+ try
+ {
+ closeable.close();
+ }
+ catch (IOException ioe)
+ {
+ logger.warn("Error closing " + resourceName, ioe);
+ if (initialException == null)
+ {
+ return ioe;
+ }
+ else
+ {
+ return null;
+ }
+ }
+ return null;
+ }
}
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java?rev=1827851&r1=1827850&r2=1827851&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java Tue Mar 27 18:07:42 2018
@@ -41,6 +41,7 @@ import org.apache.pdfbox.cos.COSInteger;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSNumber;
import org.apache.pdfbox.cos.COSStream;
+import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
@@ -314,48 +315,17 @@ public class PDFMergerUtility
{
if (destination != null)
{
- try
- {
- destination.close();
- }
- catch (IOException ioe)
- {
- LOG.warn("Couldn't close PDDocument", ioe);
- if (firstException == null)
- {
- firstException = ioe;
- }
- }
+ firstException = IOUtils.closeAndLogException(destination, LOG, "PDDocument", firstException);
}
+
for (PDDocument doc : tobeclosed)
{
- try
- {
- doc.close();
- }
- catch (IOException ioe)
- {
- LOG.warn("Couldn't close PDDocument", ioe);
- if (firstException == null)
- {
- firstException = ioe;
- }
- }
+ firstException = IOUtils.closeAndLogException(doc, LOG, "PDDocument", firstException);
}
+
for (FileInputStream stream : fileInputStreams)
{
- try
- {
- stream.close();
- }
- catch (IOException ioe)
- {
- LOG.warn("Couldn't close FileInputStream", ioe);
- if (firstException == null)
- {
- firstException = ioe;
- }
- }
+ firstException = IOUtils.closeAndLogException(stream, LOG, "FileInputStream", firstException);
}
// rethrow first exception to keep method contract
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1827851&r1=1827850&r2=1827851&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Tue Mar 27 18:07:42 2018
@@ -1430,49 +1430,22 @@ public class PDDocument implements Close
// close resources and COSWriter
if (signingSupport != null)
{
- try
- {
- signingSupport.close();
- }
- catch (IOException ioe)
- {
- LOG.warn("Error closing SigningSupport", ioe);
- if (firstException == null)
- {
- firstException = ioe;
- }
- }
+ firstException = IOUtils.closeAndLogException(signingSupport, LOG, "SigningSupport", firstException);
}
// close all intermediate I/O streams
- try
- {
- document.close();
- }
- catch (IOException ioe)
- {
- LOG.warn("Error closing COSDocument", ioe);
- if (firstException == null)
- {
- firstException = ioe;
- }
- }
+ firstException = IOUtils.closeAndLogException(document, LOG, "COSDocument", firstException);
// close the source PDF stream, if we read from one
if (pdfSource != null)
{
- try
- {
- pdfSource.close();
- }
- catch (IOException ioe)
- {
- LOG.warn("Error closing RandomAccessRead pdfSource", ioe);
- if (firstException == null)
- {
- firstException = ioe;
- }
- }
+ firstException = IOUtils.closeAndLogException(pdfSource, LOG, "RandomAccessRead pdfSource", firstException);
+ }
+
+ // rethrow first exception to keep method contract
+ if (firstException != null)
+ {
+ throw firstException;
}
}
}