summaryrefslogtreecommitdiff
path: root/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java
diff options
context:
space:
mode:
authorJakob Heher <jakob.heher@iaik.tugraz.at>2022-05-05 14:08:07 +0200
committerJakob Heher <jakob.heher@iaik.tugraz.at>2022-05-05 14:08:07 +0200
commit1ac240dcf95343fbdd4c40563203bab5fd204c8f (patch)
treee4ded440baefb10c0037e0609e988459a63120b5 /pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java
parente00f1a6ed5766f80357661f47686c0822e1f7e0e (diff)
downloadpdf-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/pdfover/gui/workflow/states/PositioningState.java')
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java31
1 files changed, 21 insertions, 10 deletions
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)