diff --git a/src/share/classes/java/nio/charset/Charset.java b/src/share/classes/java/nio/charset/Charset.java index 51e21c39b6b079cad17e6f31ba2002b3dfbeb78d..51dbe1504dc4c7f13f7b13ee59e2a36ed8e95880 100644 --- a/src/share/classes/java/nio/charset/Charset.java +++ b/src/share/classes/java/nio/charset/Charset.java @@ -143,6 +143,8 @@ import sun.security.action.GetPropertyAction; * *

Standard charsets

* + * + * *

Every implementation of the Java platform is required to support the * following standard charsets. Consult the release documentation for your * implementation to see if any other charsets are supported. The behavior @@ -213,6 +215,8 @@ import sun.security.action.GetPropertyAction; * determined during virtual-machine startup and typically depends upon the * locale and charset being used by the underlying operating system.

* + *

The {@link StandardCharset} class defines constants for each of the + * standard charsets. * *

Terminology

* diff --git a/src/share/classes/java/nio/charset/StandardCharset.java b/src/share/classes/java/nio/charset/StandardCharset.java new file mode 100644 index 0000000000000000000000000000000000000000..edec058bc45d382fb3d2f58f750fe042ed5ee19b --- /dev/null +++ b/src/share/classes/java/nio/charset/StandardCharset.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.nio.charset; + +/** + * Constant definitions for the standard {@link Charset Charsets}. These + * charsets are guaranteed to be available on every implementation of the Java + * platform. + * + * @see
Standard Charsets + * @since 1.7 + */ +public final class StandardCharset { + + private StandardCharset() { + throw new AssertionError("No java.nio.charset.StandardCharset instances for you!"); + } + /** + * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the + * Unicode character set + */ + public static final Charset US_ASCII = Charset.forName("US-ASCII"); + /** + * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1 + */ + public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); + /** + * Eight-bit UCS Transformation Format + */ + public static final Charset UTF_8 = Charset.forName("UTF-8"); + /** + * Sixteen-bit UCS Transformation Format, big-endian byte order + */ + public static final Charset UTF_16BE = Charset.forName("UTF-16BE"); + /** + * Sixteen-bit UCS Transformation Format, little-endian byte order + */ + public static final Charset UTF_16LE = Charset.forName("UTF-16LE"); + /** + * Sixteen-bit UCS Transformation Format, byte order identified by an + * optional byte-order mark + */ + public static final Charset UTF_16 = Charset.forName("UTF-16"); +} diff --git a/src/share/classes/java/nio/file/Path.java b/src/share/classes/java/nio/file/Path.java index ccf0c808d61ed3c03d9a6e03a20a92c30b6a9a0b..2d2e977d49de54367f04e09978ccd9fc0a94c54b 100644 --- a/src/share/classes/java/nio/file/Path.java +++ b/src/share/classes/java/nio/file/Path.java @@ -72,7 +72,7 @@ import java.util.Iterator; * directory and is UTF-8 encoded. *
  *     Path path = FileSystems.getDefault().getPath("logs", "access.log");
- *     BufferReader reader = Files.newBufferedReader(path, Charset.forName("UTF-8"));
+ *     BufferReader reader = Files.newBufferedReader(path, StandardCharset.UTF_8);
  * 
* *

Interoperability

diff --git a/src/share/classes/java/util/zip/ZipCoder.java b/src/share/classes/java/util/zip/ZipCoder.java index 8e30fe5c8d76265af1a8f4abbbb79a51cf10cd12..a649c7056a34da3c3ce0dabaf0b5bddbf3637453 100644 --- a/src/share/classes/java/util/zip/ZipCoder.java +++ b/src/share/classes/java/util/zip/ZipCoder.java @@ -28,6 +28,7 @@ package java.util.zip; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetEncoder; import java.nio.charset.CoderResult; @@ -87,7 +88,7 @@ final class ZipCoder { if (isutf8) return getBytes(s); if (utf8 == null) - utf8 = new ZipCoder(Charset.forName("UTF-8")); + utf8 = new ZipCoder(StandardCharset.UTF_8); return utf8.getBytes(s); } @@ -96,7 +97,7 @@ final class ZipCoder { if (isutf8) return toString(ba, len); if (utf8 == null) - utf8 = new ZipCoder(Charset.forName("UTF-8")); + utf8 = new ZipCoder(StandardCharset.UTF_8); return utf8.toString(ba, len); } @@ -112,7 +113,7 @@ final class ZipCoder { private ZipCoder(Charset cs) { this.cs = cs; - this.isutf8 = cs.name().equals("UTF-8"); + this.isutf8 = cs.name().equals(StandardCharset.UTF_8.name()); } static ZipCoder get(Charset charset) { diff --git a/src/share/classes/java/util/zip/ZipFile.java b/src/share/classes/java/util/zip/ZipFile.java index 9cfbe826629542722cd31d383699dd9f1d0c0e11..d1861ad732c805781a1ce8130ff9311d1540ab07 100644 --- a/src/share/classes/java/util/zip/ZipFile.java +++ b/src/share/classes/java/util/zip/ZipFile.java @@ -31,6 +31,7 @@ import java.io.IOException; import java.io.EOFException; import java.io.File; import java.nio.charset.Charset; +import java.nio.charset.StandardCharset; import java.util.ArrayDeque; import java.util.Deque; import java.util.Enumeration; @@ -140,7 +141,7 @@ class ZipFile implements ZipConstants, Closeable { * @since 1.3 */ public ZipFile(File file, int mode) throws IOException { - this(file, mode, Charset.forName("UTF-8")); + this(file, mode, StandardCharset.UTF_8); } /** diff --git a/src/share/classes/java/util/zip/ZipInputStream.java b/src/share/classes/java/util/zip/ZipInputStream.java index 77d353a9a7a0a6de119a0440743b8e1a2206287c..ebfcce146ccf697b042a8397a4f8bee1e5b999f4 100644 --- a/src/share/classes/java/util/zip/ZipInputStream.java +++ b/src/share/classes/java/util/zip/ZipInputStream.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.io.EOFException; import java.io.PushbackInputStream; import java.nio.charset.Charset; +import java.nio.charset.StandardCharset; import static java.util.zip.ZipConstants64.*; /** @@ -75,7 +76,7 @@ class ZipInputStream extends InflaterInputStream implements ZipConstants { * @param in the actual input stream */ public ZipInputStream(InputStream in) { - this(in, Charset.forName("UTF-8")); + this(in, StandardCharset.UTF_8); } /** diff --git a/src/share/classes/java/util/zip/ZipOutputStream.java b/src/share/classes/java/util/zip/ZipOutputStream.java index fa7b96d9e1c4088784fe92372afe3fe300354aba..c0b59488002a924e845d8316bcfb9ec8200b1e0c 100644 --- a/src/share/classes/java/util/zip/ZipOutputStream.java +++ b/src/share/classes/java/util/zip/ZipOutputStream.java @@ -28,6 +28,7 @@ package java.util.zip; import java.io.OutputStream; import java.io.IOException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharset; import java.util.Vector; import java.util.HashSet; import static java.util.zip.ZipConstants64.*; @@ -100,7 +101,7 @@ class ZipOutputStream extends DeflaterOutputStream implements ZipConstants { * @param out the actual output stream */ public ZipOutputStream(OutputStream out) { - this(out, Charset.forName("UTF-8")); + this(out, StandardCharset.UTF_8); } /** diff --git a/src/share/classes/sun/awt/FontDescriptor.java b/src/share/classes/sun/awt/FontDescriptor.java index 704fbecb8eeb24e6415e3df61b4535b4dba95860..e98ec65e029c2833cb3d64a062f991dbc7072c73 100644 --- a/src/share/classes/sun/awt/FontDescriptor.java +++ b/src/share/classes/sun/awt/FontDescriptor.java @@ -26,6 +26,7 @@ package sun.awt; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; +import java.nio.charset.StandardCharset; import sun.nio.cs.HistoricallyNamedCharset; public class FontDescriptor implements Cloneable { @@ -104,8 +105,8 @@ public class FontDescriptor implements Cloneable { if (useUnicode && unicodeEncoder == null) { try { this.unicodeEncoder = isLE? - Charset.forName("UTF_16LE").newEncoder(): - Charset.forName("UTF_16BE").newEncoder(); + StandardCharset.UTF_16LE.newEncoder(): + StandardCharset.UTF_16BE.newEncoder(); } catch (IllegalArgumentException x) {} } return useUnicode; diff --git a/test/java/nio/charset/StandardCharset/Standard.java b/test/java/nio/charset/StandardCharset/Standard.java new file mode 100644 index 0000000000000000000000000000000000000000..a1b71a39c90316949c243fb9ddbdbd8f0a67cb2f --- /dev/null +++ b/test/java/nio/charset/StandardCharset/Standard.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 4884238 + * @summary Test standard charset name constants. + * @author Mike Duigou + * @run main Standard + */ + +import java.nio.charset.*; + +public class Standard { + + public static void realMain(String[] args) { + check(StandardCharset.US_ASCII instanceof Charset); + check(StandardCharset.ISO_8859_1 instanceof Charset); + check(StandardCharset.UTF_8 instanceof Charset); + check(StandardCharset.UTF_16BE instanceof Charset); + check(StandardCharset.UTF_16LE instanceof Charset); + check(StandardCharset.UTF_16 instanceof Charset); + + check("US-ASCII".equals(StandardCharset.US_ASCII.name()); + check("ISO-8859-1".equals(StandardCharset.ISO_8859_1.name()); + check("UTF-8".equals(StandardCharset.UTF_8.name()); + check("UTF-16BE".equals(StandardCharset.UTF_16BE.name()); + check("UTF-16LE".equals(StandardCharset.UTF_16LE.name()); + check("UTF-16".equals(StandardCharset.UTF_16.name()); + } + + //--------------------- Infrastructure --------------------------- + static volatile int passed = 0, failed = 0; + static void pass() { passed++; } + static void fail() { failed++; Thread.dumpStack(); } + static void fail(String msg) { System.out.println(msg); fail(); } + static void unexpected(Throwable t) { failed++; t.printStackTrace(); } + static void check(boolean cond) { if (cond) pass(); else fail(); } + static void equal(Object x, Object y) { + if (x == null ? y == null : x.equals(y)) pass(); + else {System.out.println(x + " not equal to " + y); fail();}} + static void equal2(Object x, Object y) {equal(x, y); equal(y, x);} + public static void main(String[] args) throws Throwable { + try { realMain(args); } catch (Throwable t) { unexpected(t); } + + System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); + if (failed > 0) throw new Exception("Some tests failed"); + } + private static abstract class Fun {abstract void f() throws Throwable;} + private static void THROWS(Class k, Fun... fs) { + for (Fun f : fs) + try { f.f(); fail("Expected " + k.getName() + " not thrown"); } + catch (Throwable t) { + if (k.isAssignableFrom(t.getClass())) pass(); + else unexpected(t);}} + static byte[] serializedForm(Object obj) { + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + new ObjectOutputStream(baos).writeObject(obj); + return baos.toByteArray(); + } catch (IOException e) { throw new Error(e); }} + static Object readObject(byte[] bytes) + throws IOException, ClassNotFoundException { + InputStream is = new ByteArrayInputStream(bytes); + return new ObjectInputStream(is).readObject();} + @SuppressWarnings("unchecked") + static T serialClone(T obj) { + try { return (T) readObject(serializedForm(obj)); } + catch (Exception e) { throw new Error(e); }} + +}