pdfbox-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Harald Kuhr (JIRA)" <j...@apache.org>
Subject [jira] [Created] (PDFBOX-4241) PDDocument.save double-closes stream causing exceptions with well-behaving streams
Date Wed, 06 Jun 2018 20:23:00 GMT
Harald Kuhr created PDFBOX-4241:

             Summary: PDDocument.save double-closes stream causing exceptions with well-behaving
                 Key: PDFBOX-4241
                 URL: https://issues.apache.org/jira/browse/PDFBOX-4241
             Project: PDFBox
          Issue Type: Bug
    Affects Versions: 2.0.9, 2.0.4
            Reporter: Harald Kuhr

This might be a duplicate of PDFBOX-273, although closed, it is clearly not fixed.

PDDocument save (through COSWriter) closes the output, and to make things worse, it closes
it _twice_. Also, it is generally not good practice to close streams, unless you opened them
yourself, so I think the streams should just be left alone, and instead the references should
be cleared.

The critical code in {{PDDocument.save(OutputStream output)}} is:
COSWriter writer = new COSWriter(output);
With {{COSWriter.close()}} being:
public void close() throws IOException
    if (getStandardOutput() != null)
    if (getOutput() != null)
    if (incrementalOutput != null)
The problem here is that {{standardOutput}} in this case _wraps_  {{output}}, thus causing
a double {{close()}} on {{output}} (as {{FilterOutputStream}} already closes the stream
it wraps).

Double closing itself might not have been a problem, but again {{FilterOutputStream.close()}}
invokes {{flush()}} in its standard implementation,  and you cannot flush a closed stream...

Example stack trace:
Exception in thread "main" java.io.IOException: stream already closed
at com.twelvemonkeys.imageio.util.IIOOutputStreamAdapter.assertOpen(IIOOutputStreamAdapter.java:80)
at com.twelvemonkeys.imageio.util.IIOOutputStreamAdapter.flush(IIOOutputStreamAdapter.java:75)
at java.io.BufferedOutputStream.flush(Unknown Source)
at java.io.FilterOutputStream.close(Unknown Source)
at org.apache.pdfbox.pdfwriter.COSWriter.close(COSWriter.java:315)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1236)
at com.twelvemonkeys.imageio.plugins.pdf.PDFImageWriter.endWriteSequence(PDFImageWriter.java:83)
at com.twelvemonkeys.imageio.plugins.pdf.PDFImageWriter.main(PDFImageWriter.java:183){noformat}
I tagged the issue with 2.0.4 and 2.0.9 as those are the versions I tested, but I assume the
problem is there even before 2.0.4.

This message was sent by Atlassian JIRA

To unsubscribe, e-mail: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org

View raw message