Author: fanningpj
Date: Wed Feb 16 21:15:06 2022
New Revision: 1898132

URL: http://svn.apache.org/viewvc?rev=1898132&view=rev
Log:
try to use java.nio.file.Files to create temp files

Modified:
    xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/common/IOUtil.java
    
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/SchemaCodeGenerator.java
    xmlbeans/trunk/src/test/java/tools/util/JarUtil.java

Modified: 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/common/IOUtil.java
URL: 
http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/common/IOUtil.java?rev=1898132&r1=1898131&r2=1898132&view=diff
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/common/IOUtil.java 
(original)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/common/IOUtil.java 
Wed Feb 16 21:15:06 2022
@@ -15,11 +15,21 @@
 
 package org.apache.xmlbeans.impl.common;
 
+import sun.security.action.GetPropertyAction;
+
 import java.io.*;
 import java.net.URI;
 import java.nio.channels.FileChannel;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import static java.security.AccessController.doPrivileged;
 
 public class IOUtil {
+
+    // temporary directory location
+    private static final Path tmpdir = 
Paths.get(System.getProperty("java.io.tmpdir"));
+
     public static void copyCompletely(InputStream input, OutputStream output)
         throws IOException {
         //if both are file streams, use channel IO
@@ -109,4 +119,8 @@ public class IOUtil {
         assert (created) : "Could not create " + newdir.getAbsolutePath();
         return newdir;
     }
+
+    public static Path getTempDir() {
+        return tmpdir;
+    }
 }

Modified: 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/SchemaCodeGenerator.java
URL: 
http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/SchemaCodeGenerator.java?rev=1898132&r1=1898131&r2=1898132&view=diff
==============================================================================
--- 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/SchemaCodeGenerator.java
 (original)
+++ 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/SchemaCodeGenerator.java
 Wed Feb 16 21:15:06 2022
@@ -17,13 +17,14 @@ package org.apache.xmlbeans.impl.tool;
 
 import org.apache.xmlbeans.Filer;
 import org.apache.xmlbeans.SchemaTypeSystem;
-import org.apache.xmlbeans.SystemProperties;
 import org.apache.xmlbeans.XmlOptions;
+import org.apache.xmlbeans.impl.common.IOUtil;
 import org.apache.xmlbeans.impl.repackage.Repackager;
 import org.apache.xmlbeans.impl.util.FilerImpl;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Files;
 import java.util.*;
 
 public class SchemaCodeGenerator {
@@ -97,13 +98,14 @@ public class SchemaCodeGenerator {
 // Some beta builds of JDK1.5 are having troubles creating temp directories
 // if the java.io.tmpdir doesn't exist.  This seems to help.
         try {
-            File tmpDirFile = new 
File(SystemProperties.getProperty("java.io.tmpdir"));
+            File tmpDirFile = IOUtil.getTempDir().toFile();
             tmpDirFile.mkdirs();
         } catch (Exception e) {
             e.printStackTrace();
         }
 
-        File tmpFile = File.createTempFile("xbean", null);
+        File tmpFile = Files.createTempFile(IOUtil.getTempDir(), "xbean", 
".tmp").toFile();
+
         String path = tmpFile.getAbsolutePath();
         if (!path.endsWith(".tmp")) {
             throw new IOException("Error: createTempFile did not create a file 
ending with .tmp");

Modified: xmlbeans/trunk/src/test/java/tools/util/JarUtil.java
URL: 
http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/tools/util/JarUtil.java?rev=1898132&r1=1898131&r2=1898132&view=diff
==============================================================================
--- xmlbeans/trunk/src/test/java/tools/util/JarUtil.java (original)
+++ xmlbeans/trunk/src/test/java/tools/util/JarUtil.java Wed Feb 16 21:15:06 
2022
@@ -14,8 +14,11 @@
  */
 package tools.util;
 
+import org.apache.xmlbeans.impl.common.IOUtil;
+
 import java.io.*;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 
 /**
  * A utility class for getting data from jar files
@@ -23,7 +26,7 @@ import java.nio.charset.StandardCharsets
 public class JarUtil {
 
     /**
-     * returns an File Object within the given jarFile as a String. jarFile 
must exist in classpath
+     * returns a File Object within the given jarFile as a String. jarFile 
must exist in classpath
      * pre: jar containing resource is in the classpath
      */
     public static File getResourceFromJarasFile(String pathToResource)
@@ -34,7 +37,7 @@ public class JarUtil {
         tokens = fileName.split("\\.");
         String extension = (tokens.length < 2) ? null : "." + tokens[1];
         String prefix = (tokens[0].length() < 3) ? tokens[0] + "abc" : 
tokens[0];
-        File temp = File.createTempFile(prefix, extension);
+        File temp = Files.createTempFile(IOUtil.getTempDir(), prefix, 
extension).toFile();
         temp.deleteOnExit();
         try (PrintWriter pr = new PrintWriter(new FileWriter(temp))) {
             String content = getResourceFromJar(pathToResource);
@@ -50,20 +53,17 @@ public class JarUtil {
     public static String getResourceFromJar(String pathToResource)
         throws IOException {
 
-        BufferedReader in = null;
-        try {
-            InputStream is = getResourceFromJarasStream(pathToResource);
-            in = new BufferedReader(new InputStreamReader(is, 
StandardCharsets.UTF_8));
+        try (
+                InputStream is = getResourceFromJarasStream(pathToResource);
+                BufferedReader in = new BufferedReader(new 
InputStreamReader(is, StandardCharsets.UTF_8))
+        ) {
+
             StringBuilder sb = new StringBuilder();
             char[] buf = new char[1024];
             for (int readChr; (readChr = in.read(buf)) > -1; ) {
                 sb.append(buf, 0, readChr);
             }
             return sb.toString();
-        } finally {
-            if (in != null) {
-                in.close();
-            }
         }
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to