diff options
author | Jakob Heher <jakob.heher@iaik.tugraz.at> | 2022-05-05 14:08:07 +0200 |
---|---|---|
committer | Jakob Heher <jakob.heher@iaik.tugraz.at> | 2022-05-05 14:08:07 +0200 |
commit | 1ac240dcf95343fbdd4c40563203bab5fd204c8f (patch) | |
tree | e4ded440baefb10c0037e0609e988459a63120b5 /pdf-over-gui/src/main/java/at/asit | |
parent | e00f1a6ed5766f80357661f47686c0822e1f7e0e (diff) | |
download | pdf-over-1ac240dcf95343fbdd4c40563203bab5fd204c8f.tar.gz pdf-over-1ac240dcf95343fbdd4c40563203bab5fd204c8f.tar.bz2 pdf-over-1ac240dcf95343fbdd4c40563203bab5fd204c8f.zip |
(make an attempt to) make java close files; cf. #86
Diffstat (limited to 'pdf-over-gui/src/main/java/at/asit')
4 files changed, 50 insertions, 16 deletions
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java index 09b310fe..40199301 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java @@ -176,16 +176,27 @@ public class PositioningComposite extends StateComposite { PositioningComposite.this.frame.add(PositioningComposite.this.viewer, BorderLayout.CENTER); } }); - } else + } else { EventQueue.invokeLater(new Runnable() { @Override public void run() { PositioningComposite.this.viewer.setDocument(document); } }); - this.numPages = document.getNumPages(); - this.scrollbar.setValues(1, 1, this.numPages + 1, 1, 1, 1); - showPage(this.numPages); + } + + if (document != null) + { + this.numPages = document.getNumPages(); + PositioningComposite.this.scrollbar.setValues(1, 1, this.numPages + 1, 1, 1, 1); + showPage(this.numPages); + } + } + + @Override + public void dispose() { + this.viewer.setDocument(null); + super.dispose(); } /** diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SignaturePanel.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SignaturePanel.java index 34dd6c5e..1911ac67 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SignaturePanel.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SignaturePanel.java @@ -129,8 +129,17 @@ public class SignaturePanel extends JPanel { public void setDocument(PDFFile pdf) {
this.pdf = pdf;
this.sigPagePos = null;
- this.numPages = pdf.getNumPages();
- showPage(this.numPages);
+ if (pdf != null)
+ {
+ this.numPages = pdf.getNumPages();
+ showPage(this.numPages);
+ }
+ else
+ {
+ this.page = 0;
+ this.numPages = 0;
+ showPage(null);
+ }
}
/**
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) |