From da862fff7fc3219f3749a1a3c215b78f1cc7c165 Mon Sep 17 00:00:00 2001 From: Peter Hancock Date: Tue, 16 Apr 2013 10:48:44 +0000 Subject: [PATCH] Fix & improve the handling of temporary files using the new URI resource resolvers Thanks to Alexios Giotis for valuable contribution git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1468361 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/apps/io/ResourceResolverFactory.java | 56 +++++++++++++++---- status.xml | 3 + 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/java/org/apache/fop/apps/io/ResourceResolverFactory.java b/src/java/org/apache/fop/apps/io/ResourceResolverFactory.java index cdc9438b9..28b8c8b12 100644 --- a/src/java/org/apache/fop/apps/io/ResourceResolverFactory.java +++ b/src/java/org/apache/fop/apps/io/ResourceResolverFactory.java @@ -21,12 +21,15 @@ package org.apache.fop.apps.io; import java.io.File; import java.io.FileOutputStream; +import java.io.FilterInputStream; import java.io.IOException; import java.io.OutputStream; +import java.net.MalformedURLException; import java.net.URI; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.apache.xmlgraphics.io.Resource; import org.apache.xmlgraphics.io.ResourceResolver; @@ -161,24 +164,57 @@ public final class ResourceResolverFactory { } private static class DefaultTempResourceResolver implements TempResourceResolver { - private static File getTempFile(String path) throws IOException { - File file = new File(System.getProperty("java.io.tmpdir"), path); - file.deleteOnExit(); - return file; + + private final ConcurrentHashMap tempFiles = new ConcurrentHashMap(); + + private File getTempFile(String uri) throws IllegalStateException { + File tempFile = tempFiles.remove(uri); + if (tempFile == null) { + throw new IllegalStateException(uri + " was never created or has been deleted"); + } + return tempFile; + } + + private File createTempFile(String path) throws IOException { + File tempFile = File.createTempFile(path, ".fop.tmp"); + File oldFile = tempFiles.put(path, tempFile); + if (oldFile != null) { + String errorMsg = oldFile.getAbsolutePath() + " has been already created for " + path; + boolean newTempDeleted = tempFile.delete(); + if (!newTempDeleted) { + errorMsg += ". " + tempFile.getAbsolutePath() + " was not deleted."; + } + throw new IOException(errorMsg); + } + return tempFile; } /** {@inheritDoc} */ public Resource getResource(String id) throws IOException { - return new Resource(getTempFile(id).toURI().toURL().openStream()); + return new Resource(new FileDeletingInputStream(getTempFile(id))); } /** {@inheritDoc} */ public OutputStream getOutputStream(String id) throws IOException { - File file = getTempFile(id); - if (file.createNewFile()) { - return new FileOutputStream(file); - } else { - throw new IOException("Filed to create temporary file: " + id); + return new FileOutputStream(createTempFile(id)); + } + } + + private static class FileDeletingInputStream extends FilterInputStream { + + private final File file; + + protected FileDeletingInputStream(File file) throws MalformedURLException, IOException { + super(file.toURI().toURL().openStream()); + this.file = file; + } + + @Override + public void close() throws IOException { + try { + super.close(); + } finally { + file.delete(); } } } diff --git a/status.xml b/status.xml index f19fd5963..d17168936 100644 --- a/status.xml +++ b/status.xml @@ -59,6 +59,9 @@ documents. Example: the fix of marks layering will be such a case when it's done. --> + + Fix and improve the handling of temporary files using the new URI resource resolvers + Image scaling change was adversely affecting other image types -- 2.39.5