You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2022/06/25 07:42:02 UTC

[groovy] branch master updated: Construct `InputStream` and `OutputStream` with `Files` methods

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 0c1917456b Construct `InputStream` and `OutputStream` with `Files` methods
0c1917456b is described below

commit 0c1917456b56bb30cd7e1f1f0fca5da41991e2ec
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Jun 25 15:41:39 2022 +0800

    Construct `InputStream` and `OutputStream` with `Files` methods
---
 src/main/java/groovy/util/CharsetToolkit.java              |  3 ++-
 .../org/codehaus/groovy/control/io/FileReaderSource.java   |  4 ++--
 .../codehaus/groovy/reflection/GeneratedMetaMethod.java    |  5 +++--
 .../org/codehaus/groovy/runtime/ResourceGroovyMethods.java | 14 +++++++-------
 src/main/java/org/codehaus/groovy/tools/GroovyStarter.java |  5 +++--
 .../org/codehaus/groovy/tools/javac/JavaStubGenerator.java |  1 -
 6 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/src/main/java/groovy/util/CharsetToolkit.java b/src/main/java/groovy/util/CharsetToolkit.java
index 6e2d1351f1..fe933016fe 100644
--- a/src/main/java/groovy/util/CharsetToolkit.java
+++ b/src/main/java/groovy/util/CharsetToolkit.java
@@ -28,6 +28,7 @@ import java.io.InputStreamReader;
 import java.io.LineNumberReader;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.util.Collection;
 
 /**
@@ -75,7 +76,7 @@ public class CharsetToolkit {
         this.file = file;
         this.defaultCharset = getDefaultSystemCharset();
         this.charset = null;
-        try (InputStream input = new FileInputStream(file)) {
+        try (InputStream input = Files.newInputStream(file.toPath())) {
             byte[] bytes = new byte[4096];
             int bytesRead = input.read(bytes);
             if (bytesRead == -1) {
diff --git a/src/main/java/org/codehaus/groovy/control/io/FileReaderSource.java b/src/main/java/org/codehaus/groovy/control/io/FileReaderSource.java
index 44caa25eea..1dec51dea9 100644
--- a/src/main/java/org/codehaus/groovy/control/io/FileReaderSource.java
+++ b/src/main/java/org/codehaus/groovy/control/io/FileReaderSource.java
@@ -22,7 +22,6 @@ import org.codehaus.groovy.control.CompilerConfiguration;
 
 import java.io.BufferedInputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -30,6 +29,7 @@ import java.io.Reader;
 import java.net.URI;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 
 /**
  *  A ReaderSource for source files.
@@ -60,7 +60,7 @@ public class FileReaderSource extends AbstractReaderSource {
        // we want to remove the BOM windows adds from a file if the encoding is UTF-8
        // in other cases we depend on the charsets
        Charset cs = Charset.forName(configuration.getSourceEncoding());
-       InputStream in = new BufferedInputStream(new FileInputStream(file));
+       InputStream in = new BufferedInputStream(Files.newInputStream(file.toPath()));
        if (UTF8.name().equalsIgnoreCase(cs.name())) {
            in.mark(3);
            boolean hasBOM = true;
diff --git a/src/main/java/org/codehaus/groovy/reflection/GeneratedMetaMethod.java b/src/main/java/org/codehaus/groovy/reflection/GeneratedMetaMethod.java
index f373e1c08f..5d61d65d62 100644
--- a/src/main/java/org/codehaus/groovy/reflection/GeneratedMetaMethod.java
+++ b/src/main/java/org/codehaus/groovy/reflection/GeneratedMetaMethod.java
@@ -25,11 +25,12 @@ import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.Serializable;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Modifier;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
@@ -135,7 +136,7 @@ public abstract class GeneratedMetaMethod extends MetaMethod {
             try (DataOutputStream out =
                          new DataOutputStream(
                                  new BufferedOutputStream(
-                                         new FileOutputStream(file)))) {
+                                         Files.newOutputStream(Paths.get(file))))) {
                 Map<String, Integer> classes = new LinkedHashMap<String, Integer>();
 
                 int nextClassId = 0;
diff --git a/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java
index 9a25ed154c..2decfce7be 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ResourceGroovyMethods.java
@@ -131,7 +131,7 @@ public class ResourceGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 1.5.0
      */
     public static ObjectOutputStream newObjectOutputStream(File file) throws IOException {
-        return new ObjectOutputStream(new FileOutputStream(file));
+        return new ObjectOutputStream(Files.newOutputStream(file.toPath()));
     }
 
     /**
@@ -159,7 +159,7 @@ public class ResourceGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 1.5.0
      */
     public static ObjectInputStream newObjectInputStream(File file) throws IOException {
-        return new ObjectInputStream(new FileInputStream(file));
+        return new ObjectInputStream(Files.newInputStream(file.toPath()));
     }
 
     /**
@@ -172,7 +172,7 @@ public class ResourceGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 1.5.0
      */
     public static ObjectInputStream newObjectInputStream(File file, final ClassLoader classLoader) throws IOException {
-        return IOGroovyMethods.newObjectInputStream(new FileInputStream(file), classLoader);
+        return IOGroovyMethods.newObjectInputStream(Files.newInputStream(file.toPath()), classLoader);
     }
 
     /**
@@ -685,7 +685,7 @@ public class ResourceGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 1.7.1
      */
     public static byte[] getBytes(File file) throws IOException {
-        return IOGroovyMethods.getBytes(new FileInputStream(file));
+        return IOGroovyMethods.getBytes(Files.newInputStream(file.toPath()));
     }
 
     /**
@@ -740,7 +740,7 @@ public class ResourceGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 1.7.1
      */
     public static void setBytes(File file, byte[] bytes) throws IOException {
-        IOGroovyMethods.setBytes(new FileOutputStream(file), bytes);
+        IOGroovyMethods.setBytes(Files.newOutputStream(file.toPath()), bytes);
     }
 
     /**
@@ -1875,7 +1875,7 @@ public class ResourceGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 1.0
      */
     public static BufferedOutputStream newOutputStream(File file) throws IOException {
-        return new BufferedOutputStream(new FileOutputStream(file));
+        return new BufferedOutputStream(Files.newOutputStream(file.toPath()));
     }
 
     /**
@@ -1887,7 +1887,7 @@ public class ResourceGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 1.5.0
      */
     public static DataOutputStream newDataOutputStream(File file) throws IOException {
-        return new DataOutputStream(new FileOutputStream(file));
+        return new DataOutputStream(Files.newOutputStream(file.toPath()));
     }
 
     /**
diff --git a/src/main/java/org/codehaus/groovy/tools/GroovyStarter.java b/src/main/java/org/codehaus/groovy/tools/GroovyStarter.java
index 2dd9a596ab..2e1fce7edc 100644
--- a/src/main/java/org/codehaus/groovy/tools/GroovyStarter.java
+++ b/src/main/java/org/codehaus/groovy/tools/GroovyStarter.java
@@ -18,9 +18,10 @@
  */
 package org.codehaus.groovy.tools;
 
-import java.io.FileInputStream;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.security.PrivilegedAction;
 
 /**
@@ -90,7 +91,7 @@ public class GroovyStarter {
         // load configuration file
         if (conf!=null) {
             try {
-                lc.configure(new FileInputStream(conf));
+                lc.configure(Files.newInputStream(Paths.get(conf)));
             } catch (Exception e) {
                 System.err.println("exception while configuring main class loader:");
                 exit(e);
diff --git a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
index 31a51afbcf..205720d846 100644
--- a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
+++ b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -58,7 +58,6 @@ import org.objectweb.asm.Opcodes;
 
 import javax.tools.FileObject;
 import javax.tools.JavaFileObject;
-
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;