[ https://issues.apache.org/jira/browse/PDFBOX-5894?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17894656#comment-17894656 ]
Derek Wickern commented on PDFBOX-5894: --------------------------------------- There is an issue now that {{Loader.loadFDF(String)}} and {{Loader.loadFDF(File)}} overloads will leak a {{{}FileChannel{}}}, because the {{RandomAccessReadBufferedFile}} won't be closed. {{PDDocument}} holds a reference to the {{RandomAccessRead}} and closes it when the document is closed: [https://github.com/apache/pdfbox/blob/1ea3ae6fda8558a4a006f982d93a49ff53c89f19/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java#L1272] Maybe {{FDFDocument}} should also keep a reference to the {{RandomAccessRead?}} > Problem loading FDF > ------------------- > > Key: PDFBOX-5894 > URL: https://issues.apache.org/jira/browse/PDFBOX-5894 > Project: PDFBox > Issue Type: Bug > Affects Versions: 3.0.3 PDFBox > Reporter: Derek Wickern > Assignee: Tilman Hausherr > Priority: Major > Fix For: 3.0.4 PDFBox, 4.0.0 > > Attachments: sticky-notes.fdf > > > Loading the attached {{sticky-notes.fdf}} produces incorrect results. PDFBox > logs a bunch of errors like this: > {noformat} > 15:59:55.131 [main] ERROR org.apache.pdfbox.cos.COSObject -- Can't > dereference COSObject{6 0 R} > java.io.IOException: org.apache.pdfbox.io.RandomAccessReadBufferedFile > already closed > at > org.apache.pdfbox.io.RandomAccessReadBufferedFile.checkClosed(RandomAccessReadBufferedFile.java:266) > at > org.apache.pdfbox.io.RandomAccessReadBufferedFile.getPosition(RandomAccessReadBufferedFile.java:118) > at > org.apache.pdfbox.pdfparser.COSParser.dereferenceCOSObject(COSParser.java:623) > at org.apache.pdfbox.cos.COSObject.getObject(COSObject.java:117) > at > org.apache.pdfbox.cos.COSDocument.getObjectsByType(COSDocument.java:273) > at > org.apache.pdfbox.cos.COSDocument.getObjectsByType(COSDocument.java:255) > at > org.apache.pdfbox.cos.COSDocument.getObjectsByType(COSDocument.java:241) > at > PdfboxLoadFDFRegressionTest.main(PdfboxLoadFDFRegressionTest.java:16){noformat} > The issue seems to be that {{Loader.loadFDF}} closes the > {{RandomAccessReadBuffer}} immediately after parsing, but the read buffer is > still being used. A workaround is to make sure the read buffer is kept open > until you're done with the {{{}FDFDocument{}}}. > Reproduction code: > {code:java} > import org.apache.pdfbox.Loader; > import org.apache.pdfbox.cos.COSName; > import org.apache.pdfbox.io.RandomAccessReadBuffer; > import org.apache.pdfbox.pdfparser.FDFParser; > import java.nio.file.Files; > import java.nio.file.Path; > public class PdfboxLoadFDFRegressionTest { > public static void main(String[] args) throws Exception { > var filename = "sticky-notes.fdf"; > // broken: > try (var fdf = Loader.loadFDF(filename)) { > var annots = fdf.getDocument().getObjectsByType(COSName.ANNOT); > System.out.println(annots); // [] > } > // workaround: > try (var readBuffer = new > RandomAccessReadBuffer(Files.readAllBytes(Path.of(filename))); > var fdf = new FDFParser(readBuffer).parse()) { > var annots = fdf.getDocument().getObjectsByType(COSName.ANNOT); > System.out.println(annots); // [COSObject{3 0 R}, COSObject{4 0 > R}, COSObject{5 0 R}, COSObject{6 0 R}] > } > } > }{code} > -- This message was sent by Atlassian Jira (v8.20.10#820010) --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@pdfbox.apache.org For additional commands, e-mail: dev-h...@pdfbox.apache.org