[ https://issues.apache.org/jira/browse/PDFBOX-6016?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17956585#comment-17956585 ]
Pavel edited comment on PDFBOX-6016 at 6/6/25 12:28 PM: -------------------------------------------------------- I do close it. The thing is, I've been using the version 2 of pdfbox for years without problems and faced described problem only after migration to 3.x this is my code (simplified version) {code:java} private boolean validatePdf(File file) { var parser = new MyPreflightParser(file); var doc = parser.parse(); IOUtils.closeQuietly(doc); IOUtils.closeQuietly(parser); return true; } {code} now in MyPreflightParser I print the closed state instead of closing the source {code:java} class MyPreflightParser extends PreflightParser implements Closeable { public MyPreflightParser(File file) throws IOException { super(file); } @Override public void close() throws IOException { //org.apache.commons.io.IOUtils.closeQuietly(super.source); System.out.println(source.isClosed()); } } {code} and the output is "false" - the source is still opened when doc.closed() was already called. Maybe there is a bug in PDDocument, if it is supposed to close the underlying source was (Author: rodimusprime): I do close it. The thing is, I've been using the version 2 of pdfbox for years without problems and faced described problem only after migration to 3.x this is my code (simplified version) {code:java} private boolean validatePdf(File file) { var parser = new MyPreflightParser(file); var doc = parser.parse(); IOUtils.closeQuietly(doc); IOUtils.closeQuietly(parser); return true; } {code} now in MyPreflightParser I print the closed state instead of closing the source {code:java} class MyPreflightParser extends PreflightParser implements Closeable { public MyPreflightParser(File file) throws IOException { super(file); } @Override public void close() throws IOException { //org.apache.commons.io.IOUtils.closeQuietly(super.source); System.out.println(source.isClosed()); } } {code} it the output is "false" - the source is still opened when doc.closed() was already called. Maybe there is a bug in PDDocument, if it is supposed to close the underlying source > PreflightParser doesn't close RandomAccessReadBufferedFile causing file locks > on Windows > ---------------------------------------------------------------------------------------- > > Key: PDFBOX-6016 > URL: https://issues.apache.org/jira/browse/PDFBOX-6016 > Project: PDFBox > Issue Type: Bug > Components: Preflight > Affects Versions: 3.0.5 PDFBox > Reporter: Pavel > Priority: Major > > I've occasionally stumbled upon a problem with locked files on Windows - a > validated with PreflightParser PDF file cannot be deleted under Windows for a > certain amount of time, causing or sorts of troubles. After investigating the > problem, I've noticed, that PreflightParser creates > RandomAccessReadBufferedFile and passes it to super class into the protected > "source" field. This source field is never closed in either PreflightParser > or any of its parents. I think it is a duty of the caller to close it, so > PreflightParser should have closed it. Perhaps it is a good idea to implement > Closeable as well. My prove-of-concept class solved the original problem of > files being locked > {code:java} > class MyPreflightParser extends PreflightParser implements Closeable > { > public MyPreflightParser(File file) throws IOException > { > super(file); > } > @Override > public void close() throws IOException > { > IOUtils.closeQuietly(super.source); > } > } > {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