From 764a978b3f030de08a350c2f368bad68c610bca2 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 14 Mar 2017 08:59:37 -0400 Subject: [PATCH] bug fixes to temporary file handling --- core/src/processing/core/PApplet.java | 34 +++++++++++++++++++++++---- core/todo.txt | 3 +++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 91d12d2f2..b3d166223 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -7444,10 +7444,9 @@ public class PApplet implements PConstants { static public boolean saveStream(File target, InputStream source) { File tempFile = null; try { - File parentDir = target.getParentFile(); // make sure that this path actually exists before writing createPath(target); - tempFile = File.createTempFile(target.getName(), null, parentDir); + tempFile = createTempFile(target); FileOutputStream targetStream = new FileOutputStream(tempFile); saveStream(targetStream, source); @@ -7523,6 +7522,34 @@ public class PApplet implements PConstants { } + /** + * Creates a temporary file based on the name/extension of another file + * and in the same parent directory. Ensures that the same extension is used + * (i.e. so that .gz files are gzip compressed on output) and that it's done + * from the same directory so that renaming the file later won't cross file + * system boundaries. + */ + static private File createTempFile(File file) throws IOException { + File parentDir = file.getParentFile(); + String name = file.getName(); + String prefix; + String suffix = null; + int dot = name.lastIndexOf('.'); + if (dot == -1) { + prefix = name; + } else { + // preserve the extension so that .gz works properly + prefix = name.substring(0, dot); + suffix = name.substring(dot); + } + // Prefix must be three characters + if (prefix.length() < 3) { + prefix += "processing"; + } + return File.createTempFile(prefix, suffix, parentDir); + } + + /** * @nowebref * Saves bytes to a specific File location specified by the user. @@ -7530,8 +7557,7 @@ public class PApplet implements PConstants { static public void saveBytes(File file, byte[] data) { File tempFile = null; try { - File parentDir = file.getParentFile(); - tempFile = File.createTempFile(file.getName(), null, parentDir); + tempFile = createTempFile(file); OutputStream output = createOutput(tempFile); saveBytes(output, data); diff --git a/core/todo.txt b/core/todo.txt index 0da0bfb70..62ba1004e 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -8,6 +8,9 @@ X createInput() and createOutput() now both use buffered streams by default X createInputRaw() does not, however X don't derive the font again if the size is unchanged X part of https://github.com/processing/processing/issues/4956 +X fix temporary file handling for saveBytes(), saveStream(), etc +X wasn't handling gzip output properly +X also could have problems w/ names under length 3 gohai