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

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


The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
     new 98dc5d7  GROOVY-9824: replace some old api usage with newer available 
variants (port to 2_5_X)
98dc5d7 is described below

commit 98dc5d713926cd81b006c510a1546ccd520fe17f
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 2_5_X)
---
 .../groovy/runtime/DefaultGroovyStaticMethods.java | 60 ++++++++--------------
 1 file changed, 20 insertions(+), 40 deletions(-)

diff --git 
a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java 
b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java
index 7d94771..583cfe9 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java
@@ -24,6 +24,8 @@ import 
org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
@@ -213,54 +215,32 @@ 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();
     }
 
     /**
      * Get the current time in seconds
      *
-     * @param self   placeholder variable used by Groovy categories; ignored 
for default static methods
-     * @return  the difference, measured in seconds, between
-     *          the current time and midnight, January 1, 1970 UTC.
-     * @see     System#currentTimeMillis()
+     * @param self placeholder variable used by Groovy categories; ignored for 
default static methods
+     * @return the difference, measured in seconds, between the current time 
and midnight, January 1, 1970 UTC.
+     * @see System#currentTimeMillis()
      */
-    public static long currentTimeSeconds(System self){
-    return System.currentTimeMillis() / 1000;
-  }
+    public static long currentTimeSeconds(System self) {
+        return System.currentTimeMillis() / 1000;
+    }
 
 }

Reply via email to