[ https://issues.apache.org/jira/browse/PDFBOX-4241?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Tilman Hausherr reassigned PDFBOX-4241: --------------------------------------- Assignee: Tilman Hausherr > PDDocument.save double-closes stream causing exceptions with well-behaving > streams > ---------------------------------------------------------------------------------- > > Key: PDFBOX-4241 > URL: https://issues.apache.org/jira/browse/PDFBOX-4241 > Project: PDFBox > Issue Type: Bug > Components: Writing > Affects Versions: 2.0.4, 2.0.9 > Reporter: Harald Kuhr > Assignee: Tilman Hausherr > Priority: Major > Fix For: 2.0.12, 3.0.0 PDFBox > > Attachments: PDFBox424.java > > > 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: > {code:java} > COSWriter writer = new COSWriter(output); > try > { > writer.write(this); > } > finally > { > writer.close(); > }{code} > With {{COSWriter.close()}} being: > {code:java} > public void close() throws IOException > { > if (getStandardOutput() != null) > { > getStandardOutput().close(); > } > if (getOutput() != null) > { > getOutput().close(); > } > if (incrementalOutput != null) > { > incrementalOutput.close(); > } > } > {code} > 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: > {noformat} > 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 (v7.6.3#76005) --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@pdfbox.apache.org For additional commands, e-mail: dev-h...@pdfbox.apache.org