mirror of
https://github.com/openjdk/jdk.git
synced 2026-02-15 21:05:11 +00:00
4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets
Reviewed-by: alanb, sherman, darcy
This commit is contained in:
parent
30bdc40c81
commit
a5e37c9377
@ -143,6 +143,8 @@ import sun.security.action.GetPropertyAction;
|
||||
*
|
||||
* <h4>Standard charsets</h4>
|
||||
*
|
||||
* <a name="standard">
|
||||
*
|
||||
* <p> 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. </p>
|
||||
*
|
||||
* <p>The {@link StandardCharset} class defines constants for each of the
|
||||
* standard charsets.
|
||||
*
|
||||
* <h4>Terminology</h4>
|
||||
*
|
||||
|
||||
66
jdk/src/share/classes/java/nio/charset/StandardCharset.java
Normal file
66
jdk/src/share/classes/java/nio/charset/StandardCharset.java
Normal file
@ -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 <a href="Charset#standard">Standard Charsets</a>
|
||||
* @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");
|
||||
}
|
||||
@ -72,7 +72,7 @@ import java.util.Iterator;
|
||||
* directory and is UTF-8 encoded.
|
||||
* <pre>
|
||||
* 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);
|
||||
* </pre>
|
||||
*
|
||||
* <a name="interop"><h4>Interoperability</h4></a>
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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;
|
||||
|
||||
91
jdk/test/java/nio/charset/StandardCharset/Standard.java
Normal file
91
jdk/test/java/nio/charset/StandardCharset/Standard.java
Normal file
@ -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<? extends Throwable> 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> T serialClone(T obj) {
|
||||
try { return (T) readObject(serializedForm(obj)); }
|
||||
catch (Exception e) { throw new Error(e); }}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user