Skip to content

Commit

Permalink
PDFBOX-5894: keep and close the source, as suggested by Derek Wickern
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1921731 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
THausherr committed Nov 1, 2024
1 parent 1ea3ae6 commit f6e0b63
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public FDFDocument parse() throws IOException
}
initialParse();
exceptionOccurred = false;
return new FDFDocument(document);
return new FDFDocument(document, source);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@
import java.io.Writer;
import java.nio.charset.StandardCharsets;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.io.RandomAccessRead;
import org.apache.pdfbox.pdfwriter.COSWriter;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

Expand All @@ -41,14 +47,19 @@
*/
public class FDFDocument implements Closeable
{
private static final Logger LOG = LogManager.getLogger(FDFDocument.class);

private final COSDocument document;

private final RandomAccessRead fdfSource;

/**
* Constructor, creates a new FDF document.
*
*/
public FDFDocument()
{
fdfSource = null;
document = new COSDocument();
document.getDocumentState().setParsing(false);
document.setVersion(1.2f);
Expand All @@ -65,11 +76,25 @@ public FDFDocument()
* Constructor that uses an existing document. The COSDocument that is passed in must be valid.
*
* @param doc The COSDocument that this document wraps.
* @deprecated Use {@link #FDFDocument(COSDocument, RandomAccessRead) }
*/
@Deprecated
public FDFDocument(COSDocument doc)
{
this(doc, null);
}

/**
* Constructor that uses an existing document. The COSDocument that is passed in must be valid.
*
* @param doc The COSDocument that this document wraps.
* @param source The source that will be closed when this document gets closed, can be null.
*/
public FDFDocument(COSDocument doc, RandomAccessRead source)
{
document = doc;
document.getDocumentState().setParsing(false);
fdfSource = source;
}

/**
Expand Down Expand Up @@ -249,6 +274,24 @@ public void saveXFDF(Writer output) throws IOException
@Override
public void close() throws IOException
{
document.close();
if (!document.isClosed())
{
IOException firstException = null;

// close all intermediate I/O streams
firstException = IOUtils.closeAndLogException(document, LOG, "COSDocument", firstException);

// close the source PDF stream, if we read from one
if (fdfSource != null)
{
firstException = IOUtils.closeAndLogException(fdfSource, LOG, "RandomAccessRead pdfSource", firstException);
}

// rethrow first exception to keep method contract
if (firstException != null)
{
throw firstException;
}
}
}
}

0 comments on commit f6e0b63

Please sign in to comment.