Skip to content

Commit 3f218d9

Browse files
committed
refactor(api): move DeletingRandomAccessFile to CustomPDFDocumentFactory as a private static class
1 parent 82e1bd6 commit 3f218d9

2 files changed

Lines changed: 28 additions & 40 deletions

File tree

app/common/src/main/java/org/apache/pdfbox/examples/util/DeletingRandomAccessFile.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

app/common/src/main/java/stirling/software/common/service/CustomPDFDocumentFactory.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.util.function.Consumer;
1818

1919
import org.apache.pdfbox.Loader;
20-
import org.apache.pdfbox.examples.util.DeletingRandomAccessFile;
2120
import org.apache.pdfbox.io.IOUtils;
2221
import org.apache.pdfbox.io.MemoryUsageSetting;
2322
import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
@@ -729,4 +728,32 @@ private Path createTempFilePath(String prefix) throws IOException {
729728
p.toFile().deleteOnExit();
730729
return p;
731730
}
731+
732+
/** A custom RandomAccessRead implementation that deletes the file when closed */
733+
private static class DeletingRandomAccessFile extends RandomAccessReadBufferedFile {
734+
private final Path tempFilePath;
735+
736+
public DeletingRandomAccessFile(File file) throws IOException {
737+
super(file);
738+
this.tempFilePath = file.toPath();
739+
}
740+
741+
@Override
742+
public void close() throws IOException {
743+
try {
744+
super.close();
745+
} finally {
746+
try {
747+
boolean deleted = Files.deleteIfExists(tempFilePath);
748+
if (deleted) {
749+
log.info("Successfully deleted temp file: {}", tempFilePath);
750+
} else {
751+
log.warn("Failed to delete temp file (may not exist): {}", tempFilePath);
752+
}
753+
} catch (IOException e) {
754+
log.error("Error deleting temp file: {}", tempFilePath, e);
755+
}
756+
}
757+
}
758+
}
732759
}

0 commit comments

Comments
 (0)