This is an automated email from the ASF dual-hosted git repository. sunlan pushed a commit to branch GROOVY_3_0_X in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 55184df987d6b2dfd2cb7a65b8fd89597e79cb69 Author: Daniel Sun <[email protected]> AuthorDate: Sat Apr 11 20:24:51 2020 +0800 Trivial refactoring: Standard Charset object can be used (cherry picked from commit ab91f6bb795a251f053a01b80703032e504dbb82) --- src/main/java/groovy/util/CharsetToolkit.java | 11 ++++++----- .../java/org/codehaus/groovy/control/io/FileReaderSource.java | 3 ++- .../org/codehaus/groovy/runtime/EncodingGroovyMethods.java | 7 +------ .../codehaus/groovy/transform/ASTTransformationVisitor.java | 3 ++- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/groovy/util/CharsetToolkit.java b/src/main/java/groovy/util/CharsetToolkit.java index 7b2d0d8..89d977e 100644 --- a/src/main/java/groovy/util/CharsetToolkit.java +++ b/src/main/java/groovy/util/CharsetToolkit.java @@ -27,6 +27,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Collection; /** @@ -164,11 +165,11 @@ public class CharsetToolkit { // if the file has a Byte Order Marker, we can assume the file is in UTF-xx // otherwise, the file would not be human readable if (hasUTF8Bom()) - return Charset.forName("UTF-8"); + return StandardCharsets.UTF_8; if (hasUTF16LEBom()) - return Charset.forName("UTF-16LE"); + return StandardCharsets.UTF_16LE; if (hasUTF16BEBom()) - return Charset.forName("UTF-16BE"); + return StandardCharsets.UTF_16BE; // if a byte has its most significant bit set, the file is in UTF-8 or in the default encoding // otherwise, the file is in US-ASCII @@ -259,12 +260,12 @@ public class CharsetToolkit { if (this.enforce8Bit) return this.defaultCharset; else - return Charset.forName("US-ASCII"); + return StandardCharsets.US_ASCII; } // if no invalid UTF-8 were encountered, we can assume the encoding is UTF-8, // otherwise the file would not be human readable if (validU8Char) - return Charset.forName("UTF-8"); + return StandardCharsets.UTF_8; // finally, if it's not UTF-8 nor US-ASCII, let's assume the encoding is the default encoding return this.defaultCharset; } 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 07700d3..8e117d9 100644 --- a/src/main/java/org/codehaus/groovy/control/io/FileReaderSource.java +++ b/src/main/java/org/codehaus/groovy/control/io/FileReaderSource.java @@ -29,13 +29,14 @@ import java.io.InputStreamReader; import java.io.Reader; import java.net.URI; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; /** * A ReaderSource for source files. */ public class FileReaderSource extends AbstractReaderSource { private final File file; // The File from which we produce Readers. - private final Charset UTF8 = Charset.forName("UTF-8"); + private final Charset UTF8 = StandardCharsets.UTF_8; /** * Creates the ReaderSource from a File descriptor. diff --git a/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java index 2d4ba4f..ee8abcd 100644 --- a/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java +++ b/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java @@ -24,7 +24,6 @@ import org.apache.groovy.io.StringBuilderWriter; import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.io.Writer; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -285,11 +284,7 @@ public class EncodingGroovyMethods { if (byteShift == 0) byteShift = 4; } - try { - return buffer.toString().getBytes("ISO-8859-1"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("Base 64 decode produced byte values > 255"); // TODO: change this exception type - } + return buffer.toString().getBytes(StandardCharsets.ISO_8859_1); } /** diff --git a/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java b/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java index ee9bebc..48249d3 100644 --- a/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java +++ b/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java @@ -45,6 +45,7 @@ import java.io.InputStreamReader; import java.lang.reflect.InvocationTargetException; import java.net.URISyntaxException; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; @@ -241,7 +242,7 @@ public final class ASTTransformationVisitor extends ClassCodeVisitorSupport { while (globalServices.hasMoreElements()) { URL service = globalServices.nextElement(); String className; - try (BufferedReader svcIn = new BufferedReader(new InputStreamReader(URLStreams.openUncachedStream(service), "UTF-8"))) { + try (BufferedReader svcIn = new BufferedReader(new InputStreamReader(URLStreams.openUncachedStream(service), StandardCharsets.UTF_8))) { try { className = svcIn.readLine(); } catch (IOException ioe) {
