This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new f069c35  GROOVY-9824: replace some old api usage with newer available 
variants (port to 3_0_X)
f069c35 is described below

commit f069c35c4bbaf68547c76381509d59840a7ed32d
Author: Paul King <[email protected]>
AuthorDate: Wed Nov 18 23:08:16 2020 +1000

    GROOVY-9824: replace some old api usage with newer available variants (port 
to 3_0_X)
---
 .../groovy/runtime/DefaultGroovyStaticMethods.java | 50 ++++++++--------------
 1 file changed, 17 insertions(+), 33 deletions(-)

diff --git 
a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java 
b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java
index 39cf13c..d09ee98 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java
@@ -27,8 +27,13 @@ import java.io.IOException;
 import java.lang.management.ManagementFactory;
 import java.lang.management.ThreadInfo;
 import java.lang.management.ThreadMXBean;
+<<<<<<< HEAD
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+=======
+import java.nio.file.Files;
+import java.nio.file.Path;
+>>>>>>> bcbe5c4c76... GROOVY-9824: replace some old api usage with newer 
available variants (closes #1425)
 import java.util.Arrays;
 import java.util.Date;
 import java.util.Locale;
@@ -232,42 +237,21 @@ public class DefaultGroovyStaticMethods {
     }
 
     public static File createTempDir(File self) throws IOException {
-        return createTempDir(self, "groovy-generated-", "-tmpdir");
+        return createTempDir(self, "groovy-generated-", "tmpdir-");
+    }
+
+    public static File createTempDir(File self, final String prefix) throws 
IOException {
+        return createTempDirNio(prefix);
     }
 
     public static File createTempDir(File self, final String prefix, final 
String suffix) throws IOException {
-        final int MAXTRIES = 3;
-        int accessDeniedCounter = 0;
-        File tempFile=null;
-        for (int i=0; i<MAXTRIES; i++) {
-            try {
-                tempFile = File.createTempFile(prefix, suffix);
-                tempFile.delete();
-                tempFile.mkdirs();
-                break;
-            } catch (IOException ioe) {
-                if (ioe.getMessage().startsWith("Access is denied")) {
-                    accessDeniedCounter++;
-                    try { Thread.sleep(100); } catch (InterruptedException e) 
{}
-                }
-                if (i==MAXTRIES-1) {
-                    if (accessDeniedCounter==MAXTRIES) {
-                        String msg =
-                                "Access is denied.\nWe tried " +
-                                        + accessDeniedCounter+
-                                        " times to create a temporary 
directory"+
-                                        " and failed each time. If you are on 
Windows"+
-                                        " you are possibly victim to"+
-                                        " 
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6325169. "+
-                                        " this is no bug in Groovy.";
-                        throw new IOException(msg);
-                    } else {
-                        throw ioe;
-                    }
-                }
-            }
-        }
-        return tempFile;
+        // more secure Files api doesn't support suffix, so just append it to 
the prefix
+        return createTempDirNio(prefix + suffix);
+    }
+
+    private static File createTempDirNio(String prefix) throws IOException {
+        Path tempPath = Files.createTempDirectory(prefix);
+        return tempPath.toFile();
     }
 
     /**

Reply via email to