[ 
https://issues.apache.org/jira/browse/PDFBOX-5894?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17894472#comment-17894472
 ] 

ASF subversion and git services commented on PDFBOX-5894:
---------------------------------------------------------

Commit 1921703 from Tilman Hausherr in branch 'pdfbox/trunk'
[ https://svn.apache.org/r1921703 ]

PDFBOX-5894: don't close prematurely, as suggested by Derek Wickern

> 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
>            Priority: Major
>         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

Reply via email to