From 1f16143f776b7f107bc6c03dbbf547f312d77d6e Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Thu, 12 Jul 2012 15:50:37 +0200 Subject: [PATCH] Temp dirs/files creation reorganized --- .../intellij/openapi/util/io/FileUtilRt.java | 87 +++++++++---------- 1 file changed, 42 insertions(+), 45 deletions(-) diff --git a/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java b/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java index a095a5fe9fc3..910aeb48134b 100644 --- a/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java +++ b/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java @@ -134,49 +134,31 @@ public class FileUtilRt { return createTempDirectory(prefix, suffix, true); } - public static File createTempDirectory(@NotNull @NonNls String prefix, @Nullable @NonNls String suffix, boolean deleteOnExit) throws IOException { - File file = doCreateTempFile(prefix, suffix); - file.delete(); - file.mkdir(); - if (deleteOnExit) { - file.deleteOnExit(); - } - return file; + public static File createTempDirectory(@NotNull @NonNls String prefix, @Nullable @NonNls String suffix, + boolean deleteOnExit) throws IOException { + final File dir = new File(getTempDirectory()); + return createTempDirectory(dir, prefix, suffix, deleteOnExit); } @NotNull - public static File createTempDirectory(@NotNull File dir, @NotNull @NonNls String prefix, @Nullable @NonNls String suffix) throws IOException { - return createTempDirectory(dir, prefix, suffix,true); + public static File createTempDirectory(@NotNull File dir, + @NotNull @NonNls String prefix, @Nullable @NonNls String suffix) throws IOException { + return createTempDirectory(dir, prefix, suffix, true); } + @NotNull - public static File createTempDirectory(@NotNull File dir, @NotNull @NonNls String prefix, @Nullable @NonNls String suffix,boolean deleteOnExit) throws IOException { + public static File createTempDirectory(@NotNull File dir, + @NotNull @NonNls String prefix, @Nullable @NonNls String suffix, + boolean deleteOnExit) throws IOException { File file = doCreateTempFile(dir, prefix, suffix); - file.delete(); - file.mkdir(); if (deleteOnExit) { file.deleteOnExit(); } - return file; - } - - @NotNull - public static File createTempFile(@NonNls File dir, @NotNull @NonNls String prefix, @Nullable @NonNls String suffix, boolean create) throws IOException { - return createTempFile(dir, prefix, suffix, create, true); - } - - @NotNull - public static File createTempFile(@NonNls final File dir, - @NotNull @NonNls String prefix, - @Nullable @NonNls String suffix, - final boolean create, - boolean deleteOnExit) throws IOException { - File file = doCreateTempFile(dir, prefix, suffix); - file.delete(); - if (create) { - file.createNewFile(); + if (!file.delete() && file.exists()) { + throw new IOException("Cannot delete file: " + file); } - if (deleteOnExit) { - file.deleteOnExit(); + if (!file.mkdir() && !file.isDirectory()) { + throw new IOException("Cannot create directory: " + file); } return file; } @@ -187,23 +169,39 @@ public class FileUtilRt { } @NotNull - public static File createTempFile(@NotNull @NonNls String prefix, @Nullable @NonNls String suffix, boolean deleteOnExit) throws IOException { - File file = doCreateTempFile(prefix, suffix); - file.delete(); - file.createNewFile(); + public static File createTempFile(@NotNull @NonNls String prefix, @Nullable @NonNls String suffix, + boolean deleteOnExit) throws IOException { + final File dir = new File(getTempDirectory()); + return createTempFile(dir, prefix, suffix, true, deleteOnExit); + } + + @NotNull + public static File createTempFile(@NonNls File dir, + @NotNull @NonNls String prefix, @Nullable @NonNls String suffix, + boolean create) throws IOException { + return createTempFile(dir, prefix, suffix, create, true); + } + + @NotNull + public static File createTempFile(@NonNls File dir, + @NotNull @NonNls String prefix, @Nullable @NonNls String suffix, + boolean create, boolean deleteOnExit) throws IOException { + File file = doCreateTempFile(dir, prefix, suffix); if (deleteOnExit) { file.deleteOnExit(); } + if (!create) { + if (!file.delete() && file.exists()) { + throw new IOException("Cannot delete file: " + file); + } + } return file; } @NotNull - private static File doCreateTempFile(String prefix, String suffix) throws IOException { - return doCreateTempFile(new File(getTempDirectory()), prefix, suffix); - } - - @NotNull - private static File doCreateTempFile(@NotNull File dir, @NotNull String prefix, String suffix) throws IOException { + private static File doCreateTempFile(@NotNull File dir, + @NotNull @NonNls String prefix, @Nullable @NonNls String suffix) throws IOException { + //noinspection ResultOfMethodCallIgnored dir.mkdirs(); if (prefix.length() < 3) { @@ -246,8 +244,7 @@ public class FileUtilRt { return canonical; } } - catch (IOException ignore) { - } + catch (IOException ignore) { } return file.getAbsolutePath(); }