From 1ac240dcf95343fbdd4c40563203bab5fd204c8f Mon Sep 17 00:00:00 2001 From: Jakob Heher Date: Thu, 5 May 2022 14:08:07 +0200 Subject: (make an attempt to) make java close files; cf. #86 --- .../pdfover/gui/workflow/states/OpenState.java | 3 +++ .../gui/workflow/states/PositioningState.java | 31 +++++++++++++++------- 2 files changed, 24 insertions(+), 10 deletions(-) (limited to 'pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states') diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/OpenState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/OpenState.java index dab181af..9fca3d2c 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/OpenState.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/OpenState.java @@ -88,6 +88,9 @@ public class OpenState extends State { status.setBKU(config.getDefaultBKU()); status.setDocument(null); status.setSignaturePosition(config.getDefaultSignaturePosition()); + + /* ensure that files get closed */ + status.getPreviousState().cleanUp(); } if (status.getDocument() == null) { diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java index 9a881687..6aec43fe 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java @@ -66,16 +66,27 @@ public class PositioningState extends State { private SignaturePosition previousPosition = null; - private File previousDocument = null; + private File loadedDocumentPath = null; private PDFFile document = null; - private PDFFile getPDFDocument() throws IOException { + private void closePDFDocument() { + + if (this.document != null) + { + this.document = null; + System.gc(); /* try to get Java to close the mapped file... */ + } + this.loadedDocumentPath = null; + } + + private void openPDFDocument() throws IOException { + closePDFDocument(); + File documentPath = getStateMachine().getStatus().getDocument(); PDFFile pdf = null; - RandomAccessFile rafile = new RandomAccessFile(getStateMachine().getStatus().getDocument(), "r"); + RandomAccessFile rafile = new RandomAccessFile(documentPath, "r"); FileChannel chan = rafile.getChannel(); - ByteBuffer buf = chan - .map(FileChannel.MapMode.READ_ONLY, 0, chan.size()); + ByteBuffer buf = chan.map(FileChannel.MapMode.READ_ONLY, 0, chan.size()); chan.close(); rafile.close(); try @@ -95,7 +106,8 @@ public class PositioningState extends State { else throw new IOException(Messages.getString("error.MayNotBeAPDF"), e); } - return pdf; + this.document = pdf; + this.loadedDocumentPath = documentPath; } private PositioningComposite getPositioningComposite(PDFFile document) { @@ -143,12 +155,10 @@ public class PositioningState extends State { } if ((this.document == null) || - (this.previousDocument != getStateMachine().getStatus().getDocument())) { - this.document = null; + (this.loadedDocumentPath != getStateMachine().getStatus().getDocument())) { log.debug("Checking PDF document for encryption"); //$NON-NLS-1$ try { - this.document = getPDFDocument(); - this.previousDocument = getStateMachine().getStatus().getDocument(); + openPDFDocument(); } catch (IOException e) { this.positionComposite = null; log.error("Failed to display PDF document", e); //$NON-NLS-1$ @@ -203,6 +213,7 @@ public class PositioningState extends State { public void cleanUp() { if (this.positionComposite != null) this.positionComposite.dispose(); + closePDFDocument(); } /* (non-Javadoc) -- cgit v1.2.3