feat(jdk8): move files to new folder to avoid resources compiled.
This commit is contained in:
51
jdkSrc/jdk8/java/nio/charset/CharacterCodingException.java
Normal file
51
jdkSrc/jdk8/java/nio/charset/CharacterCodingException.java
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2007, 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.
|
||||
*
|
||||
*/
|
||||
|
||||
// -- This file was mechanically generated: Do not edit! -- //
|
||||
|
||||
package java.nio.charset;
|
||||
|
||||
|
||||
/**
|
||||
* Checked exception thrown when a character encoding
|
||||
* or decoding error occurs.
|
||||
*
|
||||
* @since 1.4
|
||||
*/
|
||||
|
||||
public class CharacterCodingException
|
||||
extends java.io.IOException
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = 8421532232154627783L;
|
||||
|
||||
/**
|
||||
* Constructs an instance of this class.
|
||||
*/
|
||||
public CharacterCodingException() { }
|
||||
|
||||
}
|
||||
917
jdkSrc/jdk8/java/nio/charset/Charset.java
Normal file
917
jdkSrc/jdk8/java/nio/charset/Charset.java
Normal file
@@ -0,0 +1,917 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2013, 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;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.charset.spi.CharsetProvider;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.ServiceConfigurationError;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import sun.misc.ASCIICaseInsensitiveComparator;
|
||||
import sun.nio.cs.StandardCharsets;
|
||||
import sun.nio.cs.ThreadLocalCoders;
|
||||
import sun.security.action.GetPropertyAction;
|
||||
|
||||
|
||||
/**
|
||||
* A named mapping between sequences of sixteen-bit Unicode <a
|
||||
* href="../../lang/Character.html#unicode">code units</a> and sequences of
|
||||
* bytes. This class defines methods for creating decoders and encoders and
|
||||
* for retrieving the various names associated with a charset. Instances of
|
||||
* this class are immutable.
|
||||
*
|
||||
* <p> This class also defines static methods for testing whether a particular
|
||||
* charset is supported, for locating charset instances by name, and for
|
||||
* constructing a map that contains every charset for which support is
|
||||
* available in the current Java virtual machine. Support for new charsets can
|
||||
* be added via the service-provider interface defined in the {@link
|
||||
* java.nio.charset.spi.CharsetProvider} class.
|
||||
*
|
||||
* <p> All of the methods defined in this class are safe for use by multiple
|
||||
* concurrent threads.
|
||||
*
|
||||
*
|
||||
* <a name="names"></a><a name="charenc"></a>
|
||||
* <h2>Charset names</h2>
|
||||
*
|
||||
* <p> Charsets are named by strings composed of the following characters:
|
||||
*
|
||||
* <ul>
|
||||
*
|
||||
* <li> The uppercase letters <tt>'A'</tt> through <tt>'Z'</tt>
|
||||
* (<tt>'\u0041'</tt> through <tt>'\u005a'</tt>),
|
||||
*
|
||||
* <li> The lowercase letters <tt>'a'</tt> through <tt>'z'</tt>
|
||||
* (<tt>'\u0061'</tt> through <tt>'\u007a'</tt>),
|
||||
*
|
||||
* <li> The digits <tt>'0'</tt> through <tt>'9'</tt>
|
||||
* (<tt>'\u0030'</tt> through <tt>'\u0039'</tt>),
|
||||
*
|
||||
* <li> The dash character <tt>'-'</tt>
|
||||
* (<tt>'\u002d'</tt>, <small>HYPHEN-MINUS</small>),
|
||||
*
|
||||
* <li> The plus character <tt>'+'</tt>
|
||||
* (<tt>'\u002b'</tt>, <small>PLUS SIGN</small>),
|
||||
*
|
||||
* <li> The period character <tt>'.'</tt>
|
||||
* (<tt>'\u002e'</tt>, <small>FULL STOP</small>),
|
||||
*
|
||||
* <li> The colon character <tt>':'</tt>
|
||||
* (<tt>'\u003a'</tt>, <small>COLON</small>), and
|
||||
*
|
||||
* <li> The underscore character <tt>'_'</tt>
|
||||
* (<tt>'\u005f'</tt>, <small>LOW LINE</small>).
|
||||
*
|
||||
* </ul>
|
||||
*
|
||||
* A charset name must begin with either a letter or a digit. The empty string
|
||||
* is not a legal charset name. Charset names are not case-sensitive; that is,
|
||||
* case is always ignored when comparing charset names. Charset names
|
||||
* generally follow the conventions documented in <a
|
||||
* href="http://www.ietf.org/rfc/rfc2278.txt"><i>RFC 2278: IANA Charset
|
||||
* Registration Procedures</i></a>.
|
||||
*
|
||||
* <p> Every charset has a <i>canonical name</i> and may also have one or more
|
||||
* <i>aliases</i>. The canonical name is returned by the {@link #name() name} method
|
||||
* of this class. Canonical names are, by convention, usually in upper case.
|
||||
* The aliases of a charset are returned by the {@link #aliases() aliases}
|
||||
* method.
|
||||
*
|
||||
* <p><a name="hn">Some charsets have an <i>historical name</i> that is defined for
|
||||
* compatibility with previous versions of the Java platform.</a> A charset's
|
||||
* historical name is either its canonical name or one of its aliases. The
|
||||
* historical name is returned by the <tt>getEncoding()</tt> methods of the
|
||||
* {@link java.io.InputStreamReader#getEncoding InputStreamReader} and {@link
|
||||
* java.io.OutputStreamWriter#getEncoding OutputStreamWriter} classes.
|
||||
*
|
||||
* <p><a name="iana"> </a>If a charset listed in the <a
|
||||
* href="http://www.iana.org/assignments/character-sets"><i>IANA Charset
|
||||
* Registry</i></a> is supported by an implementation of the Java platform then
|
||||
* its canonical name must be the name listed in the registry. Many charsets
|
||||
* are given more than one name in the registry, in which case the registry
|
||||
* identifies one of the names as <i>MIME-preferred</i>. If a charset has more
|
||||
* than one registry name then its canonical name must be the MIME-preferred
|
||||
* name and the other names in the registry must be valid aliases. If a
|
||||
* supported charset is not listed in the IANA registry then its canonical name
|
||||
* must begin with one of the strings <tt>"X-"</tt> or <tt>"x-"</tt>.
|
||||
*
|
||||
* <p> The IANA charset registry does change over time, and so the canonical
|
||||
* name and the aliases of a particular charset may also change over time. To
|
||||
* ensure compatibility it is recommended that no alias ever be removed from a
|
||||
* charset, and that if the canonical name of a charset is changed then its
|
||||
* previous canonical name be made into an alias.
|
||||
*
|
||||
*
|
||||
* <h2>Standard charsets</h2>
|
||||
*
|
||||
*
|
||||
*
|
||||
* <p><a name="standard">Every implementation of the Java platform is required to support the
|
||||
* following standard charsets.</a> Consult the release documentation for your
|
||||
* implementation to see if any other charsets are supported. The behavior
|
||||
* of such optional charsets may differ between implementations.
|
||||
*
|
||||
* <blockquote><table width="80%" summary="Description of standard charsets">
|
||||
* <tr><th align="left">Charset</th><th align="left">Description</th></tr>
|
||||
* <tr><td valign=top><tt>US-ASCII</tt></td>
|
||||
* <td>Seven-bit ASCII, a.k.a. <tt>ISO646-US</tt>,
|
||||
* a.k.a. the Basic Latin block of the Unicode character set</td></tr>
|
||||
* <tr><td valign=top><tt>ISO-8859-1 </tt></td>
|
||||
* <td>ISO Latin Alphabet No. 1, a.k.a. <tt>ISO-LATIN-1</tt></td></tr>
|
||||
* <tr><td valign=top><tt>UTF-8</tt></td>
|
||||
* <td>Eight-bit UCS Transformation Format</td></tr>
|
||||
* <tr><td valign=top><tt>UTF-16BE</tt></td>
|
||||
* <td>Sixteen-bit UCS Transformation Format,
|
||||
* big-endian byte order</td></tr>
|
||||
* <tr><td valign=top><tt>UTF-16LE</tt></td>
|
||||
* <td>Sixteen-bit UCS Transformation Format,
|
||||
* little-endian byte order</td></tr>
|
||||
* <tr><td valign=top><tt>UTF-16</tt></td>
|
||||
* <td>Sixteen-bit UCS Transformation Format,
|
||||
* byte order identified by an optional byte-order mark</td></tr>
|
||||
* </table></blockquote>
|
||||
*
|
||||
* <p> The <tt>UTF-8</tt> charset is specified by <a
|
||||
* href="http://www.ietf.org/rfc/rfc2279.txt"><i>RFC 2279</i></a>; the
|
||||
* transformation format upon which it is based is specified in
|
||||
* Amendment 2 of ISO 10646-1 and is also described in the <a
|
||||
* href="http://www.unicode.org/unicode/standard/standard.html"><i>Unicode
|
||||
* Standard</i></a>.
|
||||
*
|
||||
* <p> The <tt>UTF-16</tt> charsets are specified by <a
|
||||
* href="http://www.ietf.org/rfc/rfc2781.txt"><i>RFC 2781</i></a>; the
|
||||
* transformation formats upon which they are based are specified in
|
||||
* Amendment 1 of ISO 10646-1 and are also described in the <a
|
||||
* href="http://www.unicode.org/unicode/standard/standard.html"><i>Unicode
|
||||
* Standard</i></a>.
|
||||
*
|
||||
* <p> The <tt>UTF-16</tt> charsets use sixteen-bit quantities and are
|
||||
* therefore sensitive to byte order. In these encodings the byte order of a
|
||||
* stream may be indicated by an initial <i>byte-order mark</i> represented by
|
||||
* the Unicode character <tt>'\uFEFF'</tt>. Byte-order marks are handled
|
||||
* as follows:
|
||||
*
|
||||
* <ul>
|
||||
*
|
||||
* <li><p> When decoding, the <tt>UTF-16BE</tt> and <tt>UTF-16LE</tt>
|
||||
* charsets interpret the initial byte-order marks as a <small>ZERO-WIDTH
|
||||
* NON-BREAKING SPACE</small>; when encoding, they do not write
|
||||
* byte-order marks. </p></li>
|
||||
|
||||
*
|
||||
* <li><p> When decoding, the <tt>UTF-16</tt> charset interprets the
|
||||
* byte-order mark at the beginning of the input stream to indicate the
|
||||
* byte-order of the stream but defaults to big-endian if there is no
|
||||
* byte-order mark; when encoding, it uses big-endian byte order and writes
|
||||
* a big-endian byte-order mark. </p></li>
|
||||
*
|
||||
* </ul>
|
||||
*
|
||||
* In any case, byte order marks occurring after the first element of an
|
||||
* input sequence are not omitted since the same code is used to represent
|
||||
* <small>ZERO-WIDTH NON-BREAKING SPACE</small>.
|
||||
*
|
||||
* <p> Every instance of the Java virtual machine has a default charset, which
|
||||
* may or may not be one of the standard charsets. The default charset is
|
||||
* determined during virtual-machine startup and typically depends upon the
|
||||
* locale and charset being used by the underlying operating system. </p>
|
||||
*
|
||||
* <p>The {@link StandardCharsets} class defines constants for each of the
|
||||
* standard charsets.
|
||||
*
|
||||
* <h2>Terminology</h2>
|
||||
*
|
||||
* <p> The name of this class is taken from the terms used in
|
||||
* <a href="http://www.ietf.org/rfc/rfc2278.txt"><i>RFC 2278</i></a>.
|
||||
* In that document a <i>charset</i> is defined as the combination of
|
||||
* one or more coded character sets and a character-encoding scheme.
|
||||
* (This definition is confusing; some other software systems define
|
||||
* <i>charset</i> as a synonym for <i>coded character set</i>.)
|
||||
*
|
||||
* <p> A <i>coded character set</i> is a mapping between a set of abstract
|
||||
* characters and a set of integers. US-ASCII, ISO 8859-1,
|
||||
* JIS X 0201, and Unicode are examples of coded character sets.
|
||||
*
|
||||
* <p> Some standards have defined a <i>character set</i> to be simply a
|
||||
* set of abstract characters without an associated assigned numbering.
|
||||
* An alphabet is an example of such a character set. However, the subtle
|
||||
* distinction between <i>character set</i> and <i>coded character set</i>
|
||||
* is rarely used in practice; the former has become a short form for the
|
||||
* latter, including in the Java API specification.
|
||||
*
|
||||
* <p> A <i>character-encoding scheme</i> is a mapping between one or more
|
||||
* coded character sets and a set of octet (eight-bit byte) sequences.
|
||||
* UTF-8, UTF-16, ISO 2022, and EUC are examples of
|
||||
* character-encoding schemes. Encoding schemes are often associated with
|
||||
* a particular coded character set; UTF-8, for example, is used only to
|
||||
* encode Unicode. Some schemes, however, are associated with multiple
|
||||
* coded character sets; EUC, for example, can be used to encode
|
||||
* characters in a variety of Asian coded character sets.
|
||||
*
|
||||
* <p> When a coded character set is used exclusively with a single
|
||||
* character-encoding scheme then the corresponding charset is usually
|
||||
* named for the coded character set; otherwise a charset is usually named
|
||||
* for the encoding scheme and, possibly, the locale of the coded
|
||||
* character sets that it supports. Hence <tt>US-ASCII</tt> is both the
|
||||
* name of a coded character set and of the charset that encodes it, while
|
||||
* <tt>EUC-JP</tt> is the name of the charset that encodes the
|
||||
* JIS X 0201, JIS X 0208, and JIS X 0212
|
||||
* coded character sets for the Japanese language.
|
||||
*
|
||||
* <p> The native character encoding of the Java programming language is
|
||||
* UTF-16. A charset in the Java platform therefore defines a mapping
|
||||
* between sequences of sixteen-bit UTF-16 code units (that is, sequences
|
||||
* of chars) and sequences of bytes. </p>
|
||||
*
|
||||
*
|
||||
* @author Mark Reinhold
|
||||
* @author JSR-51 Expert Group
|
||||
* @since 1.4
|
||||
*
|
||||
* @see CharsetDecoder
|
||||
* @see CharsetEncoder
|
||||
* @see java.nio.charset.spi.CharsetProvider
|
||||
* @see java.lang.Character
|
||||
*/
|
||||
|
||||
public abstract class Charset
|
||||
implements Comparable<Charset>
|
||||
{
|
||||
|
||||
/* -- Static methods -- */
|
||||
|
||||
private static volatile String bugLevel = null;
|
||||
|
||||
static boolean atBugLevel(String bl) { // package-private
|
||||
String level = bugLevel;
|
||||
if (level == null) {
|
||||
if (!sun.misc.VM.isBooted())
|
||||
return false;
|
||||
bugLevel = level = AccessController.doPrivileged(
|
||||
new GetPropertyAction("sun.nio.cs.bugLevel", ""));
|
||||
}
|
||||
return level.equals(bl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that the given string is a legal charset name. </p>
|
||||
*
|
||||
* @param s
|
||||
* A purported charset name
|
||||
*
|
||||
* @throws IllegalCharsetNameException
|
||||
* If the given name is not a legal charset name
|
||||
*/
|
||||
private static void checkName(String s) {
|
||||
int n = s.length();
|
||||
if (!atBugLevel("1.4")) {
|
||||
if (n == 0)
|
||||
throw new IllegalCharsetNameException(s);
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
char c = s.charAt(i);
|
||||
if (c >= 'A' && c <= 'Z') continue;
|
||||
if (c >= 'a' && c <= 'z') continue;
|
||||
if (c >= '0' && c <= '9') continue;
|
||||
if (c == '-' && i != 0) continue;
|
||||
if (c == '+' && i != 0) continue;
|
||||
if (c == ':' && i != 0) continue;
|
||||
if (c == '_' && i != 0) continue;
|
||||
if (c == '.' && i != 0) continue;
|
||||
throw new IllegalCharsetNameException(s);
|
||||
}
|
||||
}
|
||||
|
||||
/* The standard set of charsets */
|
||||
private static CharsetProvider standardProvider = new StandardCharsets();
|
||||
|
||||
// Cache of the most-recently-returned charsets,
|
||||
// along with the names that were used to find them
|
||||
//
|
||||
private static volatile Object[] cache1 = null; // "Level 1" cache
|
||||
private static volatile Object[] cache2 = null; // "Level 2" cache
|
||||
|
||||
private static void cache(String charsetName, Charset cs) {
|
||||
cache2 = cache1;
|
||||
cache1 = new Object[] { charsetName, cs };
|
||||
}
|
||||
|
||||
// Creates an iterator that walks over the available providers, ignoring
|
||||
// those whose lookup or instantiation causes a security exception to be
|
||||
// thrown. Should be invoked with full privileges.
|
||||
//
|
||||
private static Iterator<CharsetProvider> providers() {
|
||||
return new Iterator<CharsetProvider>() {
|
||||
|
||||
ClassLoader cl = ClassLoader.getSystemClassLoader();
|
||||
ServiceLoader<CharsetProvider> sl =
|
||||
ServiceLoader.load(CharsetProvider.class, cl);
|
||||
Iterator<CharsetProvider> i = sl.iterator();
|
||||
|
||||
CharsetProvider next = null;
|
||||
|
||||
private boolean getNext() {
|
||||
while (next == null) {
|
||||
try {
|
||||
if (!i.hasNext())
|
||||
return false;
|
||||
next = i.next();
|
||||
} catch (ServiceConfigurationError sce) {
|
||||
if (sce.getCause() instanceof SecurityException) {
|
||||
// Ignore security exceptions
|
||||
continue;
|
||||
}
|
||||
throw sce;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return getNext();
|
||||
}
|
||||
|
||||
public CharsetProvider next() {
|
||||
if (!getNext())
|
||||
throw new NoSuchElementException();
|
||||
CharsetProvider n = next;
|
||||
next = null;
|
||||
return n;
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
// Thread-local gate to prevent recursive provider lookups
|
||||
private static ThreadLocal<ThreadLocal<?>> gate =
|
||||
new ThreadLocal<ThreadLocal<?>>();
|
||||
|
||||
private static Charset lookupViaProviders(final String charsetName) {
|
||||
|
||||
// The runtime startup sequence looks up standard charsets as a
|
||||
// consequence of the VM's invocation of System.initializeSystemClass
|
||||
// in order to, e.g., set system properties and encode filenames. At
|
||||
// that point the application class loader has not been initialized,
|
||||
// however, so we can't look for providers because doing so will cause
|
||||
// that loader to be prematurely initialized with incomplete
|
||||
// information.
|
||||
//
|
||||
if (!sun.misc.VM.isBooted())
|
||||
return null;
|
||||
|
||||
if (gate.get() != null)
|
||||
// Avoid recursive provider lookups
|
||||
return null;
|
||||
try {
|
||||
gate.set(gate);
|
||||
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<Charset>() {
|
||||
public Charset run() {
|
||||
for (Iterator<CharsetProvider> i = providers();
|
||||
i.hasNext();) {
|
||||
CharsetProvider cp = i.next();
|
||||
Charset cs = cp.charsetForName(charsetName);
|
||||
if (cs != null)
|
||||
return cs;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
} finally {
|
||||
gate.set(null);
|
||||
}
|
||||
}
|
||||
|
||||
/* The extended set of charsets */
|
||||
private static class ExtendedProviderHolder {
|
||||
static final CharsetProvider extendedProvider = extendedProvider();
|
||||
// returns ExtendedProvider, if installed
|
||||
private static CharsetProvider extendedProvider() {
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<CharsetProvider>() {
|
||||
public CharsetProvider run() {
|
||||
try {
|
||||
Class<?> epc
|
||||
= Class.forName("sun.nio.cs.ext.ExtendedCharsets");
|
||||
return (CharsetProvider)epc.newInstance();
|
||||
} catch (ClassNotFoundException x) {
|
||||
// Extended charsets not available
|
||||
// (charsets.jar not present)
|
||||
} catch (InstantiationException |
|
||||
IllegalAccessException x) {
|
||||
throw new Error(x);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private static Charset lookupExtendedCharset(String charsetName) {
|
||||
CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
|
||||
return (ecp != null) ? ecp.charsetForName(charsetName) : null;
|
||||
}
|
||||
|
||||
private static Charset lookup(String charsetName) {
|
||||
if (charsetName == null)
|
||||
throw new IllegalArgumentException("Null charset name");
|
||||
Object[] a;
|
||||
if ((a = cache1) != null && charsetName.equals(a[0]))
|
||||
return (Charset)a[1];
|
||||
// We expect most programs to use one Charset repeatedly.
|
||||
// We convey a hint to this effect to the VM by putting the
|
||||
// level 1 cache miss code in a separate method.
|
||||
return lookup2(charsetName);
|
||||
}
|
||||
|
||||
private static Charset lookup2(String charsetName) {
|
||||
Object[] a;
|
||||
if ((a = cache2) != null && charsetName.equals(a[0])) {
|
||||
cache2 = cache1;
|
||||
cache1 = a;
|
||||
return (Charset)a[1];
|
||||
}
|
||||
Charset cs;
|
||||
if ((cs = standardProvider.charsetForName(charsetName)) != null ||
|
||||
(cs = lookupExtendedCharset(charsetName)) != null ||
|
||||
(cs = lookupViaProviders(charsetName)) != null)
|
||||
{
|
||||
cache(charsetName, cs);
|
||||
return cs;
|
||||
}
|
||||
|
||||
/* Only need to check the name if we didn't find a charset for it */
|
||||
checkName(charsetName);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether the named charset is supported.
|
||||
*
|
||||
* @param charsetName
|
||||
* The name of the requested charset; may be either
|
||||
* a canonical name or an alias
|
||||
*
|
||||
* @return <tt>true</tt> if, and only if, support for the named charset
|
||||
* is available in the current Java virtual machine
|
||||
*
|
||||
* @throws IllegalCharsetNameException
|
||||
* If the given charset name is illegal
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If the given <tt>charsetName</tt> is null
|
||||
*/
|
||||
public static boolean isSupported(String charsetName) {
|
||||
return (lookup(charsetName) != null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a charset object for the named charset.
|
||||
*
|
||||
* @param charsetName
|
||||
* The name of the requested charset; may be either
|
||||
* a canonical name or an alias
|
||||
*
|
||||
* @return A charset object for the named charset
|
||||
*
|
||||
* @throws IllegalCharsetNameException
|
||||
* If the given charset name is illegal
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If the given <tt>charsetName</tt> is null
|
||||
*
|
||||
* @throws UnsupportedCharsetException
|
||||
* If no support for the named charset is available
|
||||
* in this instance of the Java virtual machine
|
||||
*/
|
||||
public static Charset forName(String charsetName) {
|
||||
Charset cs = lookup(charsetName);
|
||||
if (cs != null)
|
||||
return cs;
|
||||
throw new UnsupportedCharsetException(charsetName);
|
||||
}
|
||||
|
||||
// Fold charsets from the given iterator into the given map, ignoring
|
||||
// charsets whose names already have entries in the map.
|
||||
//
|
||||
private static void put(Iterator<Charset> i, Map<String,Charset> m) {
|
||||
while (i.hasNext()) {
|
||||
Charset cs = i.next();
|
||||
if (!m.containsKey(cs.name()))
|
||||
m.put(cs.name(), cs);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a sorted map from canonical charset names to charset objects.
|
||||
*
|
||||
* <p> The map returned by this method will have one entry for each charset
|
||||
* for which support is available in the current Java virtual machine. If
|
||||
* two or more supported charsets have the same canonical name then the
|
||||
* resulting map will contain just one of them; which one it will contain
|
||||
* is not specified. </p>
|
||||
*
|
||||
* <p> The invocation of this method, and the subsequent use of the
|
||||
* resulting map, may cause time-consuming disk or network I/O operations
|
||||
* to occur. This method is provided for applications that need to
|
||||
* enumerate all of the available charsets, for example to allow user
|
||||
* charset selection. This method is not used by the {@link #forName
|
||||
* forName} method, which instead employs an efficient incremental lookup
|
||||
* algorithm.
|
||||
*
|
||||
* <p> This method may return different results at different times if new
|
||||
* charset providers are dynamically made available to the current Java
|
||||
* virtual machine. In the absence of such changes, the charsets returned
|
||||
* by this method are exactly those that can be retrieved via the {@link
|
||||
* #forName forName} method. </p>
|
||||
*
|
||||
* @return An immutable, case-insensitive map from canonical charset names
|
||||
* to charset objects
|
||||
*/
|
||||
public static SortedMap<String,Charset> availableCharsets() {
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<SortedMap<String,Charset>>() {
|
||||
public SortedMap<String,Charset> run() {
|
||||
TreeMap<String,Charset> m =
|
||||
new TreeMap<String,Charset>(
|
||||
ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
|
||||
put(standardProvider.charsets(), m);
|
||||
CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
|
||||
if (ecp != null)
|
||||
put(ecp.charsets(), m);
|
||||
for (Iterator<CharsetProvider> i = providers(); i.hasNext();) {
|
||||
CharsetProvider cp = i.next();
|
||||
put(cp.charsets(), m);
|
||||
}
|
||||
return Collections.unmodifiableSortedMap(m);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static volatile Charset defaultCharset;
|
||||
|
||||
/**
|
||||
* Returns the default charset of this Java virtual machine.
|
||||
*
|
||||
* <p> The default charset is determined during virtual-machine startup and
|
||||
* typically depends upon the locale and charset of the underlying
|
||||
* operating system.
|
||||
*
|
||||
* @return A charset object for the default charset
|
||||
*
|
||||
* @since 1.5
|
||||
*/
|
||||
public static Charset defaultCharset() {
|
||||
if (defaultCharset == null) {
|
||||
synchronized (Charset.class) {
|
||||
String csn = AccessController.doPrivileged(
|
||||
new GetPropertyAction("file.encoding"));
|
||||
Charset cs = lookup(csn);
|
||||
if (cs != null)
|
||||
defaultCharset = cs;
|
||||
else
|
||||
defaultCharset = forName("UTF-8");
|
||||
}
|
||||
}
|
||||
return defaultCharset;
|
||||
}
|
||||
|
||||
|
||||
/* -- Instance fields and methods -- */
|
||||
|
||||
private final String name; // tickles a bug in oldjavac
|
||||
private final String[] aliases; // tickles a bug in oldjavac
|
||||
private Set<String> aliasSet = null;
|
||||
|
||||
/**
|
||||
* Initializes a new charset with the given canonical name and alias
|
||||
* set.
|
||||
*
|
||||
* @param canonicalName
|
||||
* The canonical name of this charset
|
||||
*
|
||||
* @param aliases
|
||||
* An array of this charset's aliases, or null if it has no aliases
|
||||
*
|
||||
* @throws IllegalCharsetNameException
|
||||
* If the canonical name or any of the aliases are illegal
|
||||
*/
|
||||
protected Charset(String canonicalName, String[] aliases) {
|
||||
checkName(canonicalName);
|
||||
String[] as = (aliases == null) ? new String[0] : aliases;
|
||||
for (int i = 0; i < as.length; i++)
|
||||
checkName(as[i]);
|
||||
this.name = canonicalName;
|
||||
this.aliases = as;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this charset's canonical name.
|
||||
*
|
||||
* @return The canonical name of this charset
|
||||
*/
|
||||
public final String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a set containing this charset's aliases.
|
||||
*
|
||||
* @return An immutable set of this charset's aliases
|
||||
*/
|
||||
public final Set<String> aliases() {
|
||||
if (aliasSet != null)
|
||||
return aliasSet;
|
||||
int n = aliases.length;
|
||||
HashSet<String> hs = new HashSet<String>(n);
|
||||
for (int i = 0; i < n; i++)
|
||||
hs.add(aliases[i]);
|
||||
aliasSet = Collections.unmodifiableSet(hs);
|
||||
return aliasSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this charset's human-readable name for the default locale.
|
||||
*
|
||||
* <p> The default implementation of this method simply returns this
|
||||
* charset's canonical name. Concrete subclasses of this class may
|
||||
* override this method in order to provide a localized display name. </p>
|
||||
*
|
||||
* @return The display name of this charset in the default locale
|
||||
*/
|
||||
public String displayName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether or not this charset is registered in the <a
|
||||
* href="http://www.iana.org/assignments/character-sets">IANA Charset
|
||||
* Registry</a>.
|
||||
*
|
||||
* @return <tt>true</tt> if, and only if, this charset is known by its
|
||||
* implementor to be registered with the IANA
|
||||
*/
|
||||
public final boolean isRegistered() {
|
||||
return !name.startsWith("X-") && !name.startsWith("x-");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this charset's human-readable name for the given locale.
|
||||
*
|
||||
* <p> The default implementation of this method simply returns this
|
||||
* charset's canonical name. Concrete subclasses of this class may
|
||||
* override this method in order to provide a localized display name. </p>
|
||||
*
|
||||
* @param locale
|
||||
* The locale for which the display name is to be retrieved
|
||||
*
|
||||
* @return The display name of this charset in the given locale
|
||||
*/
|
||||
public String displayName(Locale locale) {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether or not this charset contains the given charset.
|
||||
*
|
||||
* <p> A charset <i>C</i> is said to <i>contain</i> a charset <i>D</i> if,
|
||||
* and only if, every character representable in <i>D</i> is also
|
||||
* representable in <i>C</i>. If this relationship holds then it is
|
||||
* guaranteed that every string that can be encoded in <i>D</i> can also be
|
||||
* encoded in <i>C</i> without performing any replacements.
|
||||
*
|
||||
* <p> That <i>C</i> contains <i>D</i> does not imply that each character
|
||||
* representable in <i>C</i> by a particular byte sequence is represented
|
||||
* in <i>D</i> by the same byte sequence, although sometimes this is the
|
||||
* case.
|
||||
*
|
||||
* <p> Every charset contains itself.
|
||||
*
|
||||
* <p> This method computes an approximation of the containment relation:
|
||||
* If it returns <tt>true</tt> then the given charset is known to be
|
||||
* contained by this charset; if it returns <tt>false</tt>, however, then
|
||||
* it is not necessarily the case that the given charset is not contained
|
||||
* in this charset.
|
||||
*
|
||||
* @param cs
|
||||
* The given charset
|
||||
*
|
||||
* @return <tt>true</tt> if the given charset is contained in this charset
|
||||
*/
|
||||
public abstract boolean contains(Charset cs);
|
||||
|
||||
/**
|
||||
* Constructs a new decoder for this charset.
|
||||
*
|
||||
* @return A new decoder for this charset
|
||||
*/
|
||||
public abstract CharsetDecoder newDecoder();
|
||||
|
||||
/**
|
||||
* Constructs a new encoder for this charset.
|
||||
*
|
||||
* @return A new encoder for this charset
|
||||
*
|
||||
* @throws UnsupportedOperationException
|
||||
* If this charset does not support encoding
|
||||
*/
|
||||
public abstract CharsetEncoder newEncoder();
|
||||
|
||||
/**
|
||||
* Tells whether or not this charset supports encoding.
|
||||
*
|
||||
* <p> Nearly all charsets support encoding. The primary exceptions are
|
||||
* special-purpose <i>auto-detect</i> charsets whose decoders can determine
|
||||
* which of several possible encoding schemes is in use by examining the
|
||||
* input byte sequence. Such charsets do not support encoding because
|
||||
* there is no way to determine which encoding should be used on output.
|
||||
* Implementations of such charsets should override this method to return
|
||||
* <tt>false</tt>. </p>
|
||||
*
|
||||
* @return <tt>true</tt> if, and only if, this charset supports encoding
|
||||
*/
|
||||
public boolean canEncode() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method that decodes bytes in this charset into Unicode
|
||||
* characters.
|
||||
*
|
||||
* <p> An invocation of this method upon a charset <tt>cs</tt> returns the
|
||||
* same result as the expression
|
||||
*
|
||||
* <pre>
|
||||
* cs.newDecoder()
|
||||
* .onMalformedInput(CodingErrorAction.REPLACE)
|
||||
* .onUnmappableCharacter(CodingErrorAction.REPLACE)
|
||||
* .decode(bb); </pre>
|
||||
*
|
||||
* except that it is potentially more efficient because it can cache
|
||||
* decoders between successive invocations.
|
||||
*
|
||||
* <p> This method always replaces malformed-input and unmappable-character
|
||||
* sequences with this charset's default replacement byte array. In order
|
||||
* to detect such sequences, use the {@link
|
||||
* CharsetDecoder#decode(java.nio.ByteBuffer)} method directly. </p>
|
||||
*
|
||||
* @param bb The byte buffer to be decoded
|
||||
*
|
||||
* @return A char buffer containing the decoded characters
|
||||
*/
|
||||
public final CharBuffer decode(ByteBuffer bb) {
|
||||
try {
|
||||
return ThreadLocalCoders.decoderFor(this)
|
||||
.onMalformedInput(CodingErrorAction.REPLACE)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPLACE)
|
||||
.decode(bb);
|
||||
} catch (CharacterCodingException x) {
|
||||
throw new Error(x); // Can't happen
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method that encodes Unicode characters into bytes in this
|
||||
* charset.
|
||||
*
|
||||
* <p> An invocation of this method upon a charset <tt>cs</tt> returns the
|
||||
* same result as the expression
|
||||
*
|
||||
* <pre>
|
||||
* cs.newEncoder()
|
||||
* .onMalformedInput(CodingErrorAction.REPLACE)
|
||||
* .onUnmappableCharacter(CodingErrorAction.REPLACE)
|
||||
* .encode(bb); </pre>
|
||||
*
|
||||
* except that it is potentially more efficient because it can cache
|
||||
* encoders between successive invocations.
|
||||
*
|
||||
* <p> This method always replaces malformed-input and unmappable-character
|
||||
* sequences with this charset's default replacement string. In order to
|
||||
* detect such sequences, use the {@link
|
||||
* CharsetEncoder#encode(java.nio.CharBuffer)} method directly. </p>
|
||||
*
|
||||
* @param cb The char buffer to be encoded
|
||||
*
|
||||
* @return A byte buffer containing the encoded characters
|
||||
*/
|
||||
public final ByteBuffer encode(CharBuffer cb) {
|
||||
try {
|
||||
return ThreadLocalCoders.encoderFor(this)
|
||||
.onMalformedInput(CodingErrorAction.REPLACE)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPLACE)
|
||||
.encode(cb);
|
||||
} catch (CharacterCodingException x) {
|
||||
throw new Error(x); // Can't happen
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method that encodes a string into bytes in this charset.
|
||||
*
|
||||
* <p> An invocation of this method upon a charset <tt>cs</tt> returns the
|
||||
* same result as the expression
|
||||
*
|
||||
* <pre>
|
||||
* cs.encode(CharBuffer.wrap(s)); </pre>
|
||||
*
|
||||
* @param str The string to be encoded
|
||||
*
|
||||
* @return A byte buffer containing the encoded characters
|
||||
*/
|
||||
public final ByteBuffer encode(String str) {
|
||||
return encode(CharBuffer.wrap(str));
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares this charset to another.
|
||||
*
|
||||
* <p> Charsets are ordered by their canonical names, without regard to
|
||||
* case. </p>
|
||||
*
|
||||
* @param that
|
||||
* The charset to which this charset is to be compared
|
||||
*
|
||||
* @return A negative integer, zero, or a positive integer as this charset
|
||||
* is less than, equal to, or greater than the specified charset
|
||||
*/
|
||||
public final int compareTo(Charset that) {
|
||||
return (name().compareToIgnoreCase(that.name()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes a hashcode for this charset.
|
||||
*
|
||||
* @return An integer hashcode
|
||||
*/
|
||||
public final int hashCode() {
|
||||
return name().hashCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether or not this object is equal to another.
|
||||
*
|
||||
* <p> Two charsets are equal if, and only if, they have the same canonical
|
||||
* names. A charset is never equal to any other type of object. </p>
|
||||
*
|
||||
* @return <tt>true</tt> if, and only if, this charset is equal to the
|
||||
* given object
|
||||
*/
|
||||
public final boolean equals(Object ob) {
|
||||
if (!(ob instanceof Charset))
|
||||
return false;
|
||||
if (this == ob)
|
||||
return true;
|
||||
return name.equals(((Charset)ob).name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string describing this charset.
|
||||
*
|
||||
* @return A string describing this charset
|
||||
*/
|
||||
public final String toString() {
|
||||
return name();
|
||||
}
|
||||
|
||||
}
|
||||
996
jdkSrc/jdk8/java/nio/charset/CharsetDecoder.java
Normal file
996
jdkSrc/jdk8/java/nio/charset/CharsetDecoder.java
Normal file
@@ -0,0 +1,996 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2013, 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.
|
||||
*/
|
||||
|
||||
// -- This file was mechanically generated: Do not edit! -- //
|
||||
|
||||
package java.nio.charset;
|
||||
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.BufferOverflowException;
|
||||
import java.nio.BufferUnderflowException;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.nio.charset.CoderMalfunctionError; // javadoc
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
/**
|
||||
* An engine that can transform a sequence of bytes in a specific charset into a sequence of
|
||||
* sixteen-bit Unicode characters.
|
||||
*
|
||||
* <a name="steps"></a>
|
||||
*
|
||||
* <p> The input byte sequence is provided in a byte buffer or a series
|
||||
* of such buffers. The output character sequence is written to a character buffer
|
||||
* or a series of such buffers. A decoder should always be used by making
|
||||
* the following sequence of method invocations, hereinafter referred to as a
|
||||
* <i>decoding operation</i>:
|
||||
*
|
||||
* <ol>
|
||||
*
|
||||
* <li><p> Reset the decoder via the {@link #reset reset} method, unless it
|
||||
* has not been used before; </p></li>
|
||||
*
|
||||
* <li><p> Invoke the {@link #decode decode} method zero or more times, as
|
||||
* long as additional input may be available, passing <tt>false</tt> for the
|
||||
* <tt>endOfInput</tt> argument and filling the input buffer and flushing the
|
||||
* output buffer between invocations; </p></li>
|
||||
*
|
||||
* <li><p> Invoke the {@link #decode decode} method one final time, passing
|
||||
* <tt>true</tt> for the <tt>endOfInput</tt> argument; and then </p></li>
|
||||
*
|
||||
* <li><p> Invoke the {@link #flush flush} method so that the decoder can
|
||||
* flush any internal state to the output buffer. </p></li>
|
||||
*
|
||||
* </ol>
|
||||
*
|
||||
* Each invocation of the {@link #decode decode} method will decode as many
|
||||
* bytes as possible from the input buffer, writing the resulting characters
|
||||
* to the output buffer. The {@link #decode decode} method returns when more
|
||||
* input is required, when there is not enough room in the output buffer, or
|
||||
* when a decoding error has occurred. In each case a {@link CoderResult}
|
||||
* object is returned to describe the reason for termination. An invoker can
|
||||
* examine this object and fill the input buffer, flush the output buffer, or
|
||||
* attempt to recover from a decoding error, as appropriate, and try again.
|
||||
*
|
||||
* <a name="ce"></a>
|
||||
*
|
||||
* <p> There are two general types of decoding errors. If the input byte
|
||||
* sequence is not legal for this charset then the input is considered <i>malformed</i>. If
|
||||
* the input byte sequence is legal but cannot be mapped to a valid
|
||||
* Unicode character then an <i>unmappable character</i> has been encountered.
|
||||
*
|
||||
* <a name="cae"></a>
|
||||
*
|
||||
* <p> How a decoding error is handled depends upon the action requested for
|
||||
* that type of error, which is described by an instance of the {@link
|
||||
* CodingErrorAction} class. The possible error actions are to {@linkplain
|
||||
* CodingErrorAction#IGNORE ignore} the erroneous input, {@linkplain
|
||||
* CodingErrorAction#REPORT report} the error to the invoker via
|
||||
* the returned {@link CoderResult} object, or {@linkplain CodingErrorAction#REPLACE
|
||||
* replace} the erroneous input with the current value of the
|
||||
* replacement string. The replacement
|
||||
*
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
* has the initial value <tt>"\uFFFD"</tt>;
|
||||
|
||||
*
|
||||
* its value may be changed via the {@link #replaceWith(java.lang.String)
|
||||
* replaceWith} method.
|
||||
*
|
||||
* <p> The default action for malformed-input and unmappable-character errors
|
||||
* is to {@linkplain CodingErrorAction#REPORT report} them. The
|
||||
* malformed-input error action may be changed via the {@link
|
||||
* #onMalformedInput(CodingErrorAction) onMalformedInput} method; the
|
||||
* unmappable-character action may be changed via the {@link
|
||||
* #onUnmappableCharacter(CodingErrorAction) onUnmappableCharacter} method.
|
||||
*
|
||||
* <p> This class is designed to handle many of the details of the decoding
|
||||
* process, including the implementation of error actions. A decoder for a
|
||||
* specific charset, which is a concrete subclass of this class, need only
|
||||
* implement the abstract {@link #decodeLoop decodeLoop} method, which
|
||||
* encapsulates the basic decoding loop. A subclass that maintains internal
|
||||
* state should, additionally, override the {@link #implFlush implFlush} and
|
||||
* {@link #implReset implReset} methods.
|
||||
*
|
||||
* <p> Instances of this class are not safe for use by multiple concurrent
|
||||
* threads. </p>
|
||||
*
|
||||
*
|
||||
* @author Mark Reinhold
|
||||
* @author JSR-51 Expert Group
|
||||
* @since 1.4
|
||||
*
|
||||
* @see ByteBuffer
|
||||
* @see CharBuffer
|
||||
* @see Charset
|
||||
* @see CharsetEncoder
|
||||
*/
|
||||
|
||||
public abstract class CharsetDecoder {
|
||||
|
||||
private final Charset charset;
|
||||
private final float averageCharsPerByte;
|
||||
private final float maxCharsPerByte;
|
||||
|
||||
private String replacement;
|
||||
private CodingErrorAction malformedInputAction
|
||||
= CodingErrorAction.REPORT;
|
||||
private CodingErrorAction unmappableCharacterAction
|
||||
= CodingErrorAction.REPORT;
|
||||
|
||||
// Internal states
|
||||
//
|
||||
private static final int ST_RESET = 0;
|
||||
private static final int ST_CODING = 1;
|
||||
private static final int ST_END = 2;
|
||||
private static final int ST_FLUSHED = 3;
|
||||
|
||||
private int state = ST_RESET;
|
||||
|
||||
private static String stateNames[]
|
||||
= { "RESET", "CODING", "CODING_END", "FLUSHED" };
|
||||
|
||||
|
||||
/**
|
||||
* Initializes a new decoder. The new decoder will have the given
|
||||
* chars-per-byte and replacement values.
|
||||
*
|
||||
* @param cs
|
||||
* The charset that created this decoder
|
||||
*
|
||||
* @param averageCharsPerByte
|
||||
* A positive float value indicating the expected number of
|
||||
* characters that will be produced for each input byte
|
||||
*
|
||||
* @param maxCharsPerByte
|
||||
* A positive float value indicating the maximum number of
|
||||
* characters that will be produced for each input byte
|
||||
*
|
||||
* @param replacement
|
||||
* The initial replacement; must not be <tt>null</tt>, must have
|
||||
* non-zero length, must not be longer than maxCharsPerByte,
|
||||
* and must be {@linkplain #isLegalReplacement legal}
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If the preconditions on the parameters do not hold
|
||||
*/
|
||||
private
|
||||
CharsetDecoder(Charset cs,
|
||||
float averageCharsPerByte,
|
||||
float maxCharsPerByte,
|
||||
String replacement)
|
||||
{
|
||||
this.charset = cs;
|
||||
if (averageCharsPerByte <= 0.0f)
|
||||
throw new IllegalArgumentException("Non-positive "
|
||||
+ "averageCharsPerByte");
|
||||
if (maxCharsPerByte <= 0.0f)
|
||||
throw new IllegalArgumentException("Non-positive "
|
||||
+ "maxCharsPerByte");
|
||||
if (!Charset.atBugLevel("1.4")) {
|
||||
if (averageCharsPerByte > maxCharsPerByte)
|
||||
throw new IllegalArgumentException("averageCharsPerByte"
|
||||
+ " exceeds "
|
||||
+ "maxCharsPerByte");
|
||||
}
|
||||
this.replacement = replacement;
|
||||
this.averageCharsPerByte = averageCharsPerByte;
|
||||
this.maxCharsPerByte = maxCharsPerByte;
|
||||
replaceWith(replacement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a new decoder. The new decoder will have the given
|
||||
* chars-per-byte values and its replacement will be the
|
||||
* string <tt>"\uFFFD"</tt>.
|
||||
*
|
||||
* @param cs
|
||||
* The charset that created this decoder
|
||||
*
|
||||
* @param averageCharsPerByte
|
||||
* A positive float value indicating the expected number of
|
||||
* characters that will be produced for each input byte
|
||||
*
|
||||
* @param maxCharsPerByte
|
||||
* A positive float value indicating the maximum number of
|
||||
* characters that will be produced for each input byte
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If the preconditions on the parameters do not hold
|
||||
*/
|
||||
protected CharsetDecoder(Charset cs,
|
||||
float averageCharsPerByte,
|
||||
float maxCharsPerByte)
|
||||
{
|
||||
this(cs,
|
||||
averageCharsPerByte, maxCharsPerByte,
|
||||
"\uFFFD");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the charset that created this decoder.
|
||||
*
|
||||
* @return This decoder's charset
|
||||
*/
|
||||
public final Charset charset() {
|
||||
return charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this decoder's replacement value.
|
||||
*
|
||||
* @return This decoder's current replacement,
|
||||
* which is never <tt>null</tt> and is never empty
|
||||
*/
|
||||
public final String replacement() {
|
||||
|
||||
return replacement;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes this decoder's replacement value.
|
||||
*
|
||||
* <p> This method invokes the {@link #implReplaceWith implReplaceWith}
|
||||
* method, passing the new replacement, after checking that the new
|
||||
* replacement is acceptable. </p>
|
||||
*
|
||||
* @param newReplacement The replacement value
|
||||
*
|
||||
|
||||
* The new replacement; must not be <tt>null</tt>
|
||||
* and must have non-zero length
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*
|
||||
* @return This decoder
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If the preconditions on the parameter do not hold
|
||||
*/
|
||||
public final CharsetDecoder replaceWith(String newReplacement) {
|
||||
if (newReplacement == null)
|
||||
throw new IllegalArgumentException("Null replacement");
|
||||
int len = newReplacement.length();
|
||||
if (len == 0)
|
||||
throw new IllegalArgumentException("Empty replacement");
|
||||
if (len > maxCharsPerByte)
|
||||
throw new IllegalArgumentException("Replacement too long");
|
||||
|
||||
this.replacement = newReplacement;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
implReplaceWith(this.replacement);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports a change to this decoder's replacement value.
|
||||
*
|
||||
* <p> The default implementation of this method does nothing. This method
|
||||
* should be overridden by decoders that require notification of changes to
|
||||
* the replacement. </p>
|
||||
*
|
||||
* @param newReplacement The replacement value
|
||||
*/
|
||||
protected void implReplaceWith(String newReplacement) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns this decoder's current action for malformed-input errors.
|
||||
*
|
||||
* @return The current malformed-input action, which is never <tt>null</tt>
|
||||
*/
|
||||
public CodingErrorAction malformedInputAction() {
|
||||
return malformedInputAction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes this decoder's action for malformed-input errors.
|
||||
*
|
||||
* <p> This method invokes the {@link #implOnMalformedInput
|
||||
* implOnMalformedInput} method, passing the new action. </p>
|
||||
*
|
||||
* @param newAction The new action; must not be <tt>null</tt>
|
||||
*
|
||||
* @return This decoder
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If the precondition on the parameter does not hold
|
||||
*/
|
||||
public final CharsetDecoder onMalformedInput(CodingErrorAction newAction) {
|
||||
if (newAction == null)
|
||||
throw new IllegalArgumentException("Null action");
|
||||
malformedInputAction = newAction;
|
||||
implOnMalformedInput(newAction);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports a change to this decoder's malformed-input action.
|
||||
*
|
||||
* <p> The default implementation of this method does nothing. This method
|
||||
* should be overridden by decoders that require notification of changes to
|
||||
* the malformed-input action. </p>
|
||||
*
|
||||
* @param newAction The new action
|
||||
*/
|
||||
protected void implOnMalformedInput(CodingErrorAction newAction) { }
|
||||
|
||||
/**
|
||||
* Returns this decoder's current action for unmappable-character errors.
|
||||
*
|
||||
* @return The current unmappable-character action, which is never
|
||||
* <tt>null</tt>
|
||||
*/
|
||||
public CodingErrorAction unmappableCharacterAction() {
|
||||
return unmappableCharacterAction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes this decoder's action for unmappable-character errors.
|
||||
*
|
||||
* <p> This method invokes the {@link #implOnUnmappableCharacter
|
||||
* implOnUnmappableCharacter} method, passing the new action. </p>
|
||||
*
|
||||
* @param newAction The new action; must not be <tt>null</tt>
|
||||
*
|
||||
* @return This decoder
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If the precondition on the parameter does not hold
|
||||
*/
|
||||
public final CharsetDecoder onUnmappableCharacter(CodingErrorAction
|
||||
newAction)
|
||||
{
|
||||
if (newAction == null)
|
||||
throw new IllegalArgumentException("Null action");
|
||||
unmappableCharacterAction = newAction;
|
||||
implOnUnmappableCharacter(newAction);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports a change to this decoder's unmappable-character action.
|
||||
*
|
||||
* <p> The default implementation of this method does nothing. This method
|
||||
* should be overridden by decoders that require notification of changes to
|
||||
* the unmappable-character action. </p>
|
||||
*
|
||||
* @param newAction The new action
|
||||
*/
|
||||
protected void implOnUnmappableCharacter(CodingErrorAction newAction) { }
|
||||
|
||||
/**
|
||||
* Returns the average number of characters that will be produced for each
|
||||
* byte of input. This heuristic value may be used to estimate the size
|
||||
* of the output buffer required for a given input sequence.
|
||||
*
|
||||
* @return The average number of characters produced
|
||||
* per byte of input
|
||||
*/
|
||||
public final float averageCharsPerByte() {
|
||||
return averageCharsPerByte;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum number of characters that will be produced for each
|
||||
* byte of input. This value may be used to compute the worst-case size
|
||||
* of the output buffer required for a given input sequence.
|
||||
*
|
||||
* @return The maximum number of characters that will be produced per
|
||||
* byte of input
|
||||
*/
|
||||
public final float maxCharsPerByte() {
|
||||
return maxCharsPerByte;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes as many bytes as possible from the given input buffer,
|
||||
* writing the results to the given output buffer.
|
||||
*
|
||||
* <p> The buffers are read from, and written to, starting at their current
|
||||
* positions. At most {@link Buffer#remaining in.remaining()} bytes
|
||||
* will be read and at most {@link Buffer#remaining out.remaining()}
|
||||
* characters will be written. The buffers' positions will be advanced to
|
||||
* reflect the bytes read and the characters written, but their marks and
|
||||
* limits will not be modified.
|
||||
*
|
||||
* <p> In addition to reading bytes from the input buffer and writing
|
||||
* characters to the output buffer, this method returns a {@link CoderResult}
|
||||
* object to describe its reason for termination:
|
||||
*
|
||||
* <ul>
|
||||
*
|
||||
* <li><p> {@link CoderResult#UNDERFLOW} indicates that as much of the
|
||||
* input buffer as possible has been decoded. If there is no further
|
||||
* input then the invoker can proceed to the next step of the
|
||||
* <a href="#steps">decoding operation</a>. Otherwise this method
|
||||
* should be invoked again with further input. </p></li>
|
||||
*
|
||||
* <li><p> {@link CoderResult#OVERFLOW} indicates that there is
|
||||
* insufficient space in the output buffer to decode any more bytes.
|
||||
* This method should be invoked again with an output buffer that has
|
||||
* more {@linkplain Buffer#remaining remaining} characters. This is
|
||||
* typically done by draining any decoded characters from the output
|
||||
* buffer. </p></li>
|
||||
*
|
||||
* <li><p> A {@linkplain CoderResult#malformedForLength
|
||||
* malformed-input} result indicates that a malformed-input
|
||||
* error has been detected. The malformed bytes begin at the input
|
||||
* buffer's (possibly incremented) position; the number of malformed
|
||||
* bytes may be determined by invoking the result object's {@link
|
||||
* CoderResult#length() length} method. This case applies only if the
|
||||
* {@linkplain #onMalformedInput malformed action} of this decoder
|
||||
* is {@link CodingErrorAction#REPORT}; otherwise the malformed input
|
||||
* will be ignored or replaced, as requested. </p></li>
|
||||
*
|
||||
* <li><p> An {@linkplain CoderResult#unmappableForLength
|
||||
* unmappable-character} result indicates that an
|
||||
* unmappable-character error has been detected. The bytes that
|
||||
* decode the unmappable character begin at the input buffer's (possibly
|
||||
* incremented) position; the number of such bytes may be determined
|
||||
* by invoking the result object's {@link CoderResult#length() length}
|
||||
* method. This case applies only if the {@linkplain #onUnmappableCharacter
|
||||
* unmappable action} of this decoder is {@link
|
||||
* CodingErrorAction#REPORT}; otherwise the unmappable character will be
|
||||
* ignored or replaced, as requested. </p></li>
|
||||
*
|
||||
* </ul>
|
||||
*
|
||||
* In any case, if this method is to be reinvoked in the same decoding
|
||||
* operation then care should be taken to preserve any bytes remaining
|
||||
* in the input buffer so that they are available to the next invocation.
|
||||
*
|
||||
* <p> The <tt>endOfInput</tt> parameter advises this method as to whether
|
||||
* the invoker can provide further input beyond that contained in the given
|
||||
* input buffer. If there is a possibility of providing additional input
|
||||
* then the invoker should pass <tt>false</tt> for this parameter; if there
|
||||
* is no possibility of providing further input then the invoker should
|
||||
* pass <tt>true</tt>. It is not erroneous, and in fact it is quite
|
||||
* common, to pass <tt>false</tt> in one invocation and later discover that
|
||||
* no further input was actually available. It is critical, however, that
|
||||
* the final invocation of this method in a sequence of invocations always
|
||||
* pass <tt>true</tt> so that any remaining undecoded input will be treated
|
||||
* as being malformed.
|
||||
*
|
||||
* <p> This method works by invoking the {@link #decodeLoop decodeLoop}
|
||||
* method, interpreting its results, handling error conditions, and
|
||||
* reinvoking it as necessary. </p>
|
||||
*
|
||||
*
|
||||
* @param in
|
||||
* The input byte buffer
|
||||
*
|
||||
* @param out
|
||||
* The output character buffer
|
||||
*
|
||||
* @param endOfInput
|
||||
* <tt>true</tt> if, and only if, the invoker can provide no
|
||||
* additional input bytes beyond those in the given buffer
|
||||
*
|
||||
* @return A coder-result object describing the reason for termination
|
||||
*
|
||||
* @throws IllegalStateException
|
||||
* If a decoding operation is already in progress and the previous
|
||||
* step was an invocation neither of the {@link #reset reset}
|
||||
* method, nor of this method with a value of <tt>false</tt> for
|
||||
* the <tt>endOfInput</tt> parameter, nor of this method with a
|
||||
* value of <tt>true</tt> for the <tt>endOfInput</tt> parameter
|
||||
* but a return value indicating an incomplete decoding operation
|
||||
*
|
||||
* @throws CoderMalfunctionError
|
||||
* If an invocation of the decodeLoop method threw
|
||||
* an unexpected exception
|
||||
*/
|
||||
public final CoderResult decode(ByteBuffer in, CharBuffer out,
|
||||
boolean endOfInput)
|
||||
{
|
||||
int newState = endOfInput ? ST_END : ST_CODING;
|
||||
if ((state != ST_RESET) && (state != ST_CODING)
|
||||
&& !(endOfInput && (state == ST_END)))
|
||||
throwIllegalStateException(state, newState);
|
||||
state = newState;
|
||||
|
||||
for (;;) {
|
||||
|
||||
CoderResult cr;
|
||||
try {
|
||||
cr = decodeLoop(in, out);
|
||||
} catch (BufferUnderflowException x) {
|
||||
throw new CoderMalfunctionError(x);
|
||||
} catch (BufferOverflowException x) {
|
||||
throw new CoderMalfunctionError(x);
|
||||
}
|
||||
|
||||
if (cr.isOverflow())
|
||||
return cr;
|
||||
|
||||
if (cr.isUnderflow()) {
|
||||
if (endOfInput && in.hasRemaining()) {
|
||||
cr = CoderResult.malformedForLength(in.remaining());
|
||||
// Fall through to malformed-input case
|
||||
} else {
|
||||
return cr;
|
||||
}
|
||||
}
|
||||
|
||||
CodingErrorAction action = null;
|
||||
if (cr.isMalformed())
|
||||
action = malformedInputAction;
|
||||
else if (cr.isUnmappable())
|
||||
action = unmappableCharacterAction;
|
||||
else
|
||||
assert false : cr.toString();
|
||||
|
||||
if (action == CodingErrorAction.REPORT)
|
||||
return cr;
|
||||
|
||||
if (action == CodingErrorAction.REPLACE) {
|
||||
if (out.remaining() < replacement.length())
|
||||
return CoderResult.OVERFLOW;
|
||||
out.put(replacement);
|
||||
}
|
||||
|
||||
if ((action == CodingErrorAction.IGNORE)
|
||||
|| (action == CodingErrorAction.REPLACE)) {
|
||||
// Skip erroneous input either way
|
||||
in.position(in.position() + cr.length());
|
||||
continue;
|
||||
}
|
||||
|
||||
assert false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Flushes this decoder.
|
||||
*
|
||||
* <p> Some decoders maintain internal state and may need to write some
|
||||
* final characters to the output buffer once the overall input sequence has
|
||||
* been read.
|
||||
*
|
||||
* <p> Any additional output is written to the output buffer beginning at
|
||||
* its current position. At most {@link Buffer#remaining out.remaining()}
|
||||
* characters will be written. The buffer's position will be advanced
|
||||
* appropriately, but its mark and limit will not be modified.
|
||||
*
|
||||
* <p> If this method completes successfully then it returns {@link
|
||||
* CoderResult#UNDERFLOW}. If there is insufficient room in the output
|
||||
* buffer then it returns {@link CoderResult#OVERFLOW}. If this happens
|
||||
* then this method must be invoked again, with an output buffer that has
|
||||
* more room, in order to complete the current <a href="#steps">decoding
|
||||
* operation</a>.
|
||||
*
|
||||
* <p> If this decoder has already been flushed then invoking this method
|
||||
* has no effect.
|
||||
*
|
||||
* <p> This method invokes the {@link #implFlush implFlush} method to
|
||||
* perform the actual flushing operation. </p>
|
||||
*
|
||||
* @param out
|
||||
* The output character buffer
|
||||
*
|
||||
* @return A coder-result object, either {@link CoderResult#UNDERFLOW} or
|
||||
* {@link CoderResult#OVERFLOW}
|
||||
*
|
||||
* @throws IllegalStateException
|
||||
* If the previous step of the current decoding operation was an
|
||||
* invocation neither of the {@link #flush flush} method nor of
|
||||
* the three-argument {@link
|
||||
* #decode(ByteBuffer,CharBuffer,boolean) decode} method
|
||||
* with a value of <tt>true</tt> for the <tt>endOfInput</tt>
|
||||
* parameter
|
||||
*/
|
||||
public final CoderResult flush(CharBuffer out) {
|
||||
if (state == ST_END) {
|
||||
CoderResult cr = implFlush(out);
|
||||
if (cr.isUnderflow())
|
||||
state = ST_FLUSHED;
|
||||
return cr;
|
||||
}
|
||||
|
||||
if (state != ST_FLUSHED)
|
||||
throwIllegalStateException(state, ST_FLUSHED);
|
||||
|
||||
return CoderResult.UNDERFLOW; // Already flushed
|
||||
}
|
||||
|
||||
/**
|
||||
* Flushes this decoder.
|
||||
*
|
||||
* <p> The default implementation of this method does nothing, and always
|
||||
* returns {@link CoderResult#UNDERFLOW}. This method should be overridden
|
||||
* by decoders that may need to write final characters to the output buffer
|
||||
* once the entire input sequence has been read. </p>
|
||||
*
|
||||
* @param out
|
||||
* The output character buffer
|
||||
*
|
||||
* @return A coder-result object, either {@link CoderResult#UNDERFLOW} or
|
||||
* {@link CoderResult#OVERFLOW}
|
||||
*/
|
||||
protected CoderResult implFlush(CharBuffer out) {
|
||||
return CoderResult.UNDERFLOW;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets this decoder, clearing any internal state.
|
||||
*
|
||||
* <p> This method resets charset-independent state and also invokes the
|
||||
* {@link #implReset() implReset} method in order to perform any
|
||||
* charset-specific reset actions. </p>
|
||||
*
|
||||
* @return This decoder
|
||||
*
|
||||
*/
|
||||
public final CharsetDecoder reset() {
|
||||
implReset();
|
||||
state = ST_RESET;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets this decoder, clearing any charset-specific internal state.
|
||||
*
|
||||
* <p> The default implementation of this method does nothing. This method
|
||||
* should be overridden by decoders that maintain internal state. </p>
|
||||
*/
|
||||
protected void implReset() { }
|
||||
|
||||
/**
|
||||
* Decodes one or more bytes into one or more characters.
|
||||
*
|
||||
* <p> This method encapsulates the basic decoding loop, decoding as many
|
||||
* bytes as possible until it either runs out of input, runs out of room
|
||||
* in the output buffer, or encounters a decoding error. This method is
|
||||
* invoked by the {@link #decode decode} method, which handles result
|
||||
* interpretation and error recovery.
|
||||
*
|
||||
* <p> The buffers are read from, and written to, starting at their current
|
||||
* positions. At most {@link Buffer#remaining in.remaining()} bytes
|
||||
* will be read, and at most {@link Buffer#remaining out.remaining()}
|
||||
* characters will be written. The buffers' positions will be advanced to
|
||||
* reflect the bytes read and the characters written, but their marks and
|
||||
* limits will not be modified.
|
||||
*
|
||||
* <p> This method returns a {@link CoderResult} object to describe its
|
||||
* reason for termination, in the same manner as the {@link #decode decode}
|
||||
* method. Most implementations of this method will handle decoding errors
|
||||
* by returning an appropriate result object for interpretation by the
|
||||
* {@link #decode decode} method. An optimized implementation may instead
|
||||
* examine the relevant error action and implement that action itself.
|
||||
*
|
||||
* <p> An implementation of this method may perform arbitrary lookahead by
|
||||
* returning {@link CoderResult#UNDERFLOW} until it receives sufficient
|
||||
* input. </p>
|
||||
*
|
||||
* @param in
|
||||
* The input byte buffer
|
||||
*
|
||||
* @param out
|
||||
* The output character buffer
|
||||
*
|
||||
* @return A coder-result object describing the reason for termination
|
||||
*/
|
||||
protected abstract CoderResult decodeLoop(ByteBuffer in,
|
||||
CharBuffer out);
|
||||
|
||||
/**
|
||||
* Convenience method that decodes the remaining content of a single input
|
||||
* byte buffer into a newly-allocated character buffer.
|
||||
*
|
||||
* <p> This method implements an entire <a href="#steps">decoding
|
||||
* operation</a>; that is, it resets this decoder, then it decodes the
|
||||
* bytes in the given byte buffer, and finally it flushes this
|
||||
* decoder. This method should therefore not be invoked if a decoding
|
||||
* operation is already in progress. </p>
|
||||
*
|
||||
* @param in
|
||||
* The input byte buffer
|
||||
*
|
||||
* @return A newly-allocated character buffer containing the result of the
|
||||
* decoding operation. The buffer's position will be zero and its
|
||||
* limit will follow the last character written.
|
||||
*
|
||||
* @throws IllegalStateException
|
||||
* If a decoding operation is already in progress
|
||||
*
|
||||
* @throws MalformedInputException
|
||||
* If the byte sequence starting at the input buffer's current
|
||||
* position is not legal for this charset and the current malformed-input action
|
||||
* is {@link CodingErrorAction#REPORT}
|
||||
*
|
||||
* @throws UnmappableCharacterException
|
||||
* If the byte sequence starting at the input buffer's current
|
||||
* position cannot be mapped to an equivalent character sequence and
|
||||
* the current unmappable-character action is {@link
|
||||
* CodingErrorAction#REPORT}
|
||||
*/
|
||||
public final CharBuffer decode(ByteBuffer in)
|
||||
throws CharacterCodingException
|
||||
{
|
||||
int n = (int)(in.remaining() * averageCharsPerByte());
|
||||
CharBuffer out = CharBuffer.allocate(n);
|
||||
|
||||
if ((n == 0) && (in.remaining() == 0))
|
||||
return out;
|
||||
reset();
|
||||
for (;;) {
|
||||
CoderResult cr = in.hasRemaining() ?
|
||||
decode(in, out, true) : CoderResult.UNDERFLOW;
|
||||
if (cr.isUnderflow())
|
||||
cr = flush(out);
|
||||
|
||||
if (cr.isUnderflow())
|
||||
break;
|
||||
if (cr.isOverflow()) {
|
||||
n = 2*n + 1; // Ensure progress; n might be 0!
|
||||
CharBuffer o = CharBuffer.allocate(n);
|
||||
out.flip();
|
||||
o.put(out);
|
||||
out = o;
|
||||
continue;
|
||||
}
|
||||
cr.throwException();
|
||||
}
|
||||
out.flip();
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Tells whether or not this decoder implements an auto-detecting charset.
|
||||
*
|
||||
* <p> The default implementation of this method always returns
|
||||
* <tt>false</tt>; it should be overridden by auto-detecting decoders to
|
||||
* return <tt>true</tt>. </p>
|
||||
*
|
||||
* @return <tt>true</tt> if, and only if, this decoder implements an
|
||||
* auto-detecting charset
|
||||
*/
|
||||
public boolean isAutoDetecting() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether or not this decoder has yet detected a
|
||||
* charset <i>(optional operation)</i>.
|
||||
*
|
||||
* <p> If this decoder implements an auto-detecting charset then at a
|
||||
* single point during a decoding operation this method may start returning
|
||||
* <tt>true</tt> to indicate that a specific charset has been detected in
|
||||
* the input byte sequence. Once this occurs, the {@link #detectedCharset
|
||||
* detectedCharset} method may be invoked to retrieve the detected charset.
|
||||
*
|
||||
* <p> That this method returns <tt>false</tt> does not imply that no bytes
|
||||
* have yet been decoded. Some auto-detecting decoders are capable of
|
||||
* decoding some, or even all, of an input byte sequence without fixing on
|
||||
* a particular charset.
|
||||
*
|
||||
* <p> The default implementation of this method always throws an {@link
|
||||
* UnsupportedOperationException}; it should be overridden by
|
||||
* auto-detecting decoders to return <tt>true</tt> once the input charset
|
||||
* has been determined. </p>
|
||||
*
|
||||
* @return <tt>true</tt> if, and only if, this decoder has detected a
|
||||
* specific charset
|
||||
*
|
||||
* @throws UnsupportedOperationException
|
||||
* If this decoder does not implement an auto-detecting charset
|
||||
*/
|
||||
public boolean isCharsetDetected() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the charset that was detected by this
|
||||
* decoder <i>(optional operation)</i>.
|
||||
*
|
||||
* <p> If this decoder implements an auto-detecting charset then this
|
||||
* method returns the actual charset once it has been detected. After that
|
||||
* point, this method returns the same value for the duration of the
|
||||
* current decoding operation. If not enough input bytes have yet been
|
||||
* read to determine the actual charset then this method throws an {@link
|
||||
* IllegalStateException}.
|
||||
*
|
||||
* <p> The default implementation of this method always throws an {@link
|
||||
* UnsupportedOperationException}; it should be overridden by
|
||||
* auto-detecting decoders to return the appropriate value. </p>
|
||||
*
|
||||
* @return The charset detected by this auto-detecting decoder,
|
||||
* or <tt>null</tt> if the charset has not yet been determined
|
||||
*
|
||||
* @throws IllegalStateException
|
||||
* If insufficient bytes have been read to determine a charset
|
||||
*
|
||||
* @throws UnsupportedOperationException
|
||||
* If this decoder does not implement an auto-detecting charset
|
||||
*/
|
||||
public Charset detectedCharset() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void throwIllegalStateException(int from, int to) {
|
||||
throw new IllegalStateException("Current state = " + stateNames[from]
|
||||
+ ", new state = " + stateNames[to]);
|
||||
}
|
||||
|
||||
}
|
||||
996
jdkSrc/jdk8/java/nio/charset/CharsetEncoder.java
Normal file
996
jdkSrc/jdk8/java/nio/charset/CharsetEncoder.java
Normal file
@@ -0,0 +1,996 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2013, 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.
|
||||
*/
|
||||
|
||||
// -- This file was mechanically generated: Do not edit! -- //
|
||||
|
||||
package java.nio.charset;
|
||||
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.BufferOverflowException;
|
||||
import java.nio.BufferUnderflowException;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.nio.charset.CoderMalfunctionError; // javadoc
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
/**
|
||||
* An engine that can transform a sequence of sixteen-bit Unicode characters into a sequence of
|
||||
* bytes in a specific charset.
|
||||
*
|
||||
* <a name="steps"></a>
|
||||
*
|
||||
* <p> The input character sequence is provided in a character buffer or a series
|
||||
* of such buffers. The output byte sequence is written to a byte buffer
|
||||
* or a series of such buffers. An encoder should always be used by making
|
||||
* the following sequence of method invocations, hereinafter referred to as an
|
||||
* <i>encoding operation</i>:
|
||||
*
|
||||
* <ol>
|
||||
*
|
||||
* <li><p> Reset the encoder via the {@link #reset reset} method, unless it
|
||||
* has not been used before; </p></li>
|
||||
*
|
||||
* <li><p> Invoke the {@link #encode encode} method zero or more times, as
|
||||
* long as additional input may be available, passing <tt>false</tt> for the
|
||||
* <tt>endOfInput</tt> argument and filling the input buffer and flushing the
|
||||
* output buffer between invocations; </p></li>
|
||||
*
|
||||
* <li><p> Invoke the {@link #encode encode} method one final time, passing
|
||||
* <tt>true</tt> for the <tt>endOfInput</tt> argument; and then </p></li>
|
||||
*
|
||||
* <li><p> Invoke the {@link #flush flush} method so that the encoder can
|
||||
* flush any internal state to the output buffer. </p></li>
|
||||
*
|
||||
* </ol>
|
||||
*
|
||||
* Each invocation of the {@link #encode encode} method will encode as many
|
||||
* characters as possible from the input buffer, writing the resulting bytes
|
||||
* to the output buffer. The {@link #encode encode} method returns when more
|
||||
* input is required, when there is not enough room in the output buffer, or
|
||||
* when an encoding error has occurred. In each case a {@link CoderResult}
|
||||
* object is returned to describe the reason for termination. An invoker can
|
||||
* examine this object and fill the input buffer, flush the output buffer, or
|
||||
* attempt to recover from an encoding error, as appropriate, and try again.
|
||||
*
|
||||
* <a name="ce"></a>
|
||||
*
|
||||
* <p> There are two general types of encoding errors. If the input character
|
||||
* sequence is not a legal sixteen-bit Unicode sequence then the input is considered <i>malformed</i>. If
|
||||
* the input character sequence is legal but cannot be mapped to a valid
|
||||
* byte sequence in the given charset then an <i>unmappable character</i> has been encountered.
|
||||
*
|
||||
* <a name="cae"></a>
|
||||
*
|
||||
* <p> How an encoding error is handled depends upon the action requested for
|
||||
* that type of error, which is described by an instance of the {@link
|
||||
* CodingErrorAction} class. The possible error actions are to {@linkplain
|
||||
* CodingErrorAction#IGNORE ignore} the erroneous input, {@linkplain
|
||||
* CodingErrorAction#REPORT report} the error to the invoker via
|
||||
* the returned {@link CoderResult} object, or {@linkplain CodingErrorAction#REPLACE
|
||||
* replace} the erroneous input with the current value of the
|
||||
* replacement byte array. The replacement
|
||||
*
|
||||
|
||||
* is initially set to the encoder's default replacement, which often
|
||||
* (but not always) has the initial value <tt>{</tt> <tt>(byte)'?'</tt> <tt>}</tt>;
|
||||
|
||||
|
||||
|
||||
|
||||
*
|
||||
* its value may be changed via the {@link #replaceWith(byte[])
|
||||
* replaceWith} method.
|
||||
*
|
||||
* <p> The default action for malformed-input and unmappable-character errors
|
||||
* is to {@linkplain CodingErrorAction#REPORT report} them. The
|
||||
* malformed-input error action may be changed via the {@link
|
||||
* #onMalformedInput(CodingErrorAction) onMalformedInput} method; the
|
||||
* unmappable-character action may be changed via the {@link
|
||||
* #onUnmappableCharacter(CodingErrorAction) onUnmappableCharacter} method.
|
||||
*
|
||||
* <p> This class is designed to handle many of the details of the encoding
|
||||
* process, including the implementation of error actions. An encoder for a
|
||||
* specific charset, which is a concrete subclass of this class, need only
|
||||
* implement the abstract {@link #encodeLoop encodeLoop} method, which
|
||||
* encapsulates the basic encoding loop. A subclass that maintains internal
|
||||
* state should, additionally, override the {@link #implFlush implFlush} and
|
||||
* {@link #implReset implReset} methods.
|
||||
*
|
||||
* <p> Instances of this class are not safe for use by multiple concurrent
|
||||
* threads. </p>
|
||||
*
|
||||
*
|
||||
* @author Mark Reinhold
|
||||
* @author JSR-51 Expert Group
|
||||
* @since 1.4
|
||||
*
|
||||
* @see ByteBuffer
|
||||
* @see CharBuffer
|
||||
* @see Charset
|
||||
* @see CharsetDecoder
|
||||
*/
|
||||
|
||||
public abstract class CharsetEncoder {
|
||||
|
||||
private final Charset charset;
|
||||
private final float averageBytesPerChar;
|
||||
private final float maxBytesPerChar;
|
||||
|
||||
private byte[] replacement;
|
||||
private CodingErrorAction malformedInputAction
|
||||
= CodingErrorAction.REPORT;
|
||||
private CodingErrorAction unmappableCharacterAction
|
||||
= CodingErrorAction.REPORT;
|
||||
|
||||
// Internal states
|
||||
//
|
||||
private static final int ST_RESET = 0;
|
||||
private static final int ST_CODING = 1;
|
||||
private static final int ST_END = 2;
|
||||
private static final int ST_FLUSHED = 3;
|
||||
|
||||
private int state = ST_RESET;
|
||||
|
||||
private static String stateNames[]
|
||||
= { "RESET", "CODING", "CODING_END", "FLUSHED" };
|
||||
|
||||
|
||||
/**
|
||||
* Initializes a new encoder. The new encoder will have the given
|
||||
* bytes-per-char and replacement values.
|
||||
*
|
||||
* @param cs
|
||||
* The charset that created this encoder
|
||||
*
|
||||
* @param averageBytesPerChar
|
||||
* A positive float value indicating the expected number of
|
||||
* bytes that will be produced for each input character
|
||||
*
|
||||
* @param maxBytesPerChar
|
||||
* A positive float value indicating the maximum number of
|
||||
* bytes that will be produced for each input character
|
||||
*
|
||||
* @param replacement
|
||||
* The initial replacement; must not be <tt>null</tt>, must have
|
||||
* non-zero length, must not be longer than maxBytesPerChar,
|
||||
* and must be {@linkplain #isLegalReplacement legal}
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If the preconditions on the parameters do not hold
|
||||
*/
|
||||
protected
|
||||
CharsetEncoder(Charset cs,
|
||||
float averageBytesPerChar,
|
||||
float maxBytesPerChar,
|
||||
byte[] replacement)
|
||||
{
|
||||
this.charset = cs;
|
||||
if (averageBytesPerChar <= 0.0f)
|
||||
throw new IllegalArgumentException("Non-positive "
|
||||
+ "averageBytesPerChar");
|
||||
if (maxBytesPerChar <= 0.0f)
|
||||
throw new IllegalArgumentException("Non-positive "
|
||||
+ "maxBytesPerChar");
|
||||
if (!Charset.atBugLevel("1.4")) {
|
||||
if (averageBytesPerChar > maxBytesPerChar)
|
||||
throw new IllegalArgumentException("averageBytesPerChar"
|
||||
+ " exceeds "
|
||||
+ "maxBytesPerChar");
|
||||
}
|
||||
this.replacement = replacement;
|
||||
this.averageBytesPerChar = averageBytesPerChar;
|
||||
this.maxBytesPerChar = maxBytesPerChar;
|
||||
replaceWith(replacement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a new encoder. The new encoder will have the given
|
||||
* bytes-per-char values and its replacement will be the
|
||||
* byte array <tt>{</tt> <tt>(byte)'?'</tt> <tt>}</tt>.
|
||||
*
|
||||
* @param cs
|
||||
* The charset that created this encoder
|
||||
*
|
||||
* @param averageBytesPerChar
|
||||
* A positive float value indicating the expected number of
|
||||
* bytes that will be produced for each input character
|
||||
*
|
||||
* @param maxBytesPerChar
|
||||
* A positive float value indicating the maximum number of
|
||||
* bytes that will be produced for each input character
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If the preconditions on the parameters do not hold
|
||||
*/
|
||||
protected CharsetEncoder(Charset cs,
|
||||
float averageBytesPerChar,
|
||||
float maxBytesPerChar)
|
||||
{
|
||||
this(cs,
|
||||
averageBytesPerChar, maxBytesPerChar,
|
||||
new byte[] { (byte)'?' });
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the charset that created this encoder.
|
||||
*
|
||||
* @return This encoder's charset
|
||||
*/
|
||||
public final Charset charset() {
|
||||
return charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this encoder's replacement value.
|
||||
*
|
||||
* @return This encoder's current replacement,
|
||||
* which is never <tt>null</tt> and is never empty
|
||||
*/
|
||||
public final byte[] replacement() {
|
||||
|
||||
|
||||
|
||||
|
||||
return Arrays.copyOf(replacement, replacement.length);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes this encoder's replacement value.
|
||||
*
|
||||
* <p> This method invokes the {@link #implReplaceWith implReplaceWith}
|
||||
* method, passing the new replacement, after checking that the new
|
||||
* replacement is acceptable. </p>
|
||||
*
|
||||
* @param newReplacement The replacement value
|
||||
*
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
* The new replacement; must not be <tt>null</tt>, must have
|
||||
* non-zero length, must not be longer than the value returned by
|
||||
* the {@link #maxBytesPerChar() maxBytesPerChar} method, and
|
||||
* must be {@link #isLegalReplacement legal}
|
||||
|
||||
*
|
||||
* @return This encoder
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If the preconditions on the parameter do not hold
|
||||
*/
|
||||
public final CharsetEncoder replaceWith(byte[] newReplacement) {
|
||||
if (newReplacement == null)
|
||||
throw new IllegalArgumentException("Null replacement");
|
||||
int len = newReplacement.length;
|
||||
if (len == 0)
|
||||
throw new IllegalArgumentException("Empty replacement");
|
||||
if (len > maxBytesPerChar)
|
||||
throw new IllegalArgumentException("Replacement too long");
|
||||
|
||||
|
||||
|
||||
|
||||
if (!isLegalReplacement(newReplacement))
|
||||
throw new IllegalArgumentException("Illegal replacement");
|
||||
this.replacement = Arrays.copyOf(newReplacement, newReplacement.length);
|
||||
|
||||
implReplaceWith(this.replacement);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports a change to this encoder's replacement value.
|
||||
*
|
||||
* <p> The default implementation of this method does nothing. This method
|
||||
* should be overridden by encoders that require notification of changes to
|
||||
* the replacement. </p>
|
||||
*
|
||||
* @param newReplacement The replacement value
|
||||
*/
|
||||
protected void implReplaceWith(byte[] newReplacement) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
private WeakReference<CharsetDecoder> cachedDecoder = null;
|
||||
|
||||
/**
|
||||
* Tells whether or not the given byte array is a legal replacement value
|
||||
* for this encoder.
|
||||
*
|
||||
* <p> A replacement is legal if, and only if, it is a legal sequence of
|
||||
* bytes in this encoder's charset; that is, it must be possible to decode
|
||||
* the replacement into one or more sixteen-bit Unicode characters.
|
||||
*
|
||||
* <p> The default implementation of this method is not very efficient; it
|
||||
* should generally be overridden to improve performance. </p>
|
||||
*
|
||||
* @param repl The byte array to be tested
|
||||
*
|
||||
* @return <tt>true</tt> if, and only if, the given byte array
|
||||
* is a legal replacement value for this encoder
|
||||
*/
|
||||
public boolean isLegalReplacement(byte[] repl) {
|
||||
WeakReference<CharsetDecoder> wr = cachedDecoder;
|
||||
CharsetDecoder dec = null;
|
||||
if ((wr == null) || ((dec = wr.get()) == null)) {
|
||||
dec = charset().newDecoder();
|
||||
dec.onMalformedInput(CodingErrorAction.REPORT);
|
||||
dec.onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
cachedDecoder = new WeakReference<CharsetDecoder>(dec);
|
||||
} else {
|
||||
dec.reset();
|
||||
}
|
||||
ByteBuffer bb = ByteBuffer.wrap(repl);
|
||||
CharBuffer cb = CharBuffer.allocate((int)(bb.remaining()
|
||||
* dec.maxCharsPerByte()));
|
||||
CoderResult cr = dec.decode(bb, cb, true);
|
||||
return !cr.isError();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns this encoder's current action for malformed-input errors.
|
||||
*
|
||||
* @return The current malformed-input action, which is never <tt>null</tt>
|
||||
*/
|
||||
public CodingErrorAction malformedInputAction() {
|
||||
return malformedInputAction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes this encoder's action for malformed-input errors.
|
||||
*
|
||||
* <p> This method invokes the {@link #implOnMalformedInput
|
||||
* implOnMalformedInput} method, passing the new action. </p>
|
||||
*
|
||||
* @param newAction The new action; must not be <tt>null</tt>
|
||||
*
|
||||
* @return This encoder
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If the precondition on the parameter does not hold
|
||||
*/
|
||||
public final CharsetEncoder onMalformedInput(CodingErrorAction newAction) {
|
||||
if (newAction == null)
|
||||
throw new IllegalArgumentException("Null action");
|
||||
malformedInputAction = newAction;
|
||||
implOnMalformedInput(newAction);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports a change to this encoder's malformed-input action.
|
||||
*
|
||||
* <p> The default implementation of this method does nothing. This method
|
||||
* should be overridden by encoders that require notification of changes to
|
||||
* the malformed-input action. </p>
|
||||
*
|
||||
* @param newAction The new action
|
||||
*/
|
||||
protected void implOnMalformedInput(CodingErrorAction newAction) { }
|
||||
|
||||
/**
|
||||
* Returns this encoder's current action for unmappable-character errors.
|
||||
*
|
||||
* @return The current unmappable-character action, which is never
|
||||
* <tt>null</tt>
|
||||
*/
|
||||
public CodingErrorAction unmappableCharacterAction() {
|
||||
return unmappableCharacterAction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes this encoder's action for unmappable-character errors.
|
||||
*
|
||||
* <p> This method invokes the {@link #implOnUnmappableCharacter
|
||||
* implOnUnmappableCharacter} method, passing the new action. </p>
|
||||
*
|
||||
* @param newAction The new action; must not be <tt>null</tt>
|
||||
*
|
||||
* @return This encoder
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If the precondition on the parameter does not hold
|
||||
*/
|
||||
public final CharsetEncoder onUnmappableCharacter(CodingErrorAction
|
||||
newAction)
|
||||
{
|
||||
if (newAction == null)
|
||||
throw new IllegalArgumentException("Null action");
|
||||
unmappableCharacterAction = newAction;
|
||||
implOnUnmappableCharacter(newAction);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports a change to this encoder's unmappable-character action.
|
||||
*
|
||||
* <p> The default implementation of this method does nothing. This method
|
||||
* should be overridden by encoders that require notification of changes to
|
||||
* the unmappable-character action. </p>
|
||||
*
|
||||
* @param newAction The new action
|
||||
*/
|
||||
protected void implOnUnmappableCharacter(CodingErrorAction newAction) { }
|
||||
|
||||
/**
|
||||
* Returns the average number of bytes that will be produced for each
|
||||
* character of input. This heuristic value may be used to estimate the size
|
||||
* of the output buffer required for a given input sequence.
|
||||
*
|
||||
* @return The average number of bytes produced
|
||||
* per character of input
|
||||
*/
|
||||
public final float averageBytesPerChar() {
|
||||
return averageBytesPerChar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum number of bytes that will be produced for each
|
||||
* character of input. This value may be used to compute the worst-case size
|
||||
* of the output buffer required for a given input sequence.
|
||||
*
|
||||
* @return The maximum number of bytes that will be produced per
|
||||
* character of input
|
||||
*/
|
||||
public final float maxBytesPerChar() {
|
||||
return maxBytesPerChar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes as many characters as possible from the given input buffer,
|
||||
* writing the results to the given output buffer.
|
||||
*
|
||||
* <p> The buffers are read from, and written to, starting at their current
|
||||
* positions. At most {@link Buffer#remaining in.remaining()} characters
|
||||
* will be read and at most {@link Buffer#remaining out.remaining()}
|
||||
* bytes will be written. The buffers' positions will be advanced to
|
||||
* reflect the characters read and the bytes written, but their marks and
|
||||
* limits will not be modified.
|
||||
*
|
||||
* <p> In addition to reading characters from the input buffer and writing
|
||||
* bytes to the output buffer, this method returns a {@link CoderResult}
|
||||
* object to describe its reason for termination:
|
||||
*
|
||||
* <ul>
|
||||
*
|
||||
* <li><p> {@link CoderResult#UNDERFLOW} indicates that as much of the
|
||||
* input buffer as possible has been encoded. If there is no further
|
||||
* input then the invoker can proceed to the next step of the
|
||||
* <a href="#steps">encoding operation</a>. Otherwise this method
|
||||
* should be invoked again with further input. </p></li>
|
||||
*
|
||||
* <li><p> {@link CoderResult#OVERFLOW} indicates that there is
|
||||
* insufficient space in the output buffer to encode any more characters.
|
||||
* This method should be invoked again with an output buffer that has
|
||||
* more {@linkplain Buffer#remaining remaining} bytes. This is
|
||||
* typically done by draining any encoded bytes from the output
|
||||
* buffer. </p></li>
|
||||
*
|
||||
* <li><p> A {@linkplain CoderResult#malformedForLength
|
||||
* malformed-input} result indicates that a malformed-input
|
||||
* error has been detected. The malformed characters begin at the input
|
||||
* buffer's (possibly incremented) position; the number of malformed
|
||||
* characters may be determined by invoking the result object's {@link
|
||||
* CoderResult#length() length} method. This case applies only if the
|
||||
* {@linkplain #onMalformedInput malformed action} of this encoder
|
||||
* is {@link CodingErrorAction#REPORT}; otherwise the malformed input
|
||||
* will be ignored or replaced, as requested. </p></li>
|
||||
*
|
||||
* <li><p> An {@linkplain CoderResult#unmappableForLength
|
||||
* unmappable-character} result indicates that an
|
||||
* unmappable-character error has been detected. The characters that
|
||||
* encode the unmappable character begin at the input buffer's (possibly
|
||||
* incremented) position; the number of such characters may be determined
|
||||
* by invoking the result object's {@link CoderResult#length() length}
|
||||
* method. This case applies only if the {@linkplain #onUnmappableCharacter
|
||||
* unmappable action} of this encoder is {@link
|
||||
* CodingErrorAction#REPORT}; otherwise the unmappable character will be
|
||||
* ignored or replaced, as requested. </p></li>
|
||||
*
|
||||
* </ul>
|
||||
*
|
||||
* In any case, if this method is to be reinvoked in the same encoding
|
||||
* operation then care should be taken to preserve any characters remaining
|
||||
* in the input buffer so that they are available to the next invocation.
|
||||
*
|
||||
* <p> The <tt>endOfInput</tt> parameter advises this method as to whether
|
||||
* the invoker can provide further input beyond that contained in the given
|
||||
* input buffer. If there is a possibility of providing additional input
|
||||
* then the invoker should pass <tt>false</tt> for this parameter; if there
|
||||
* is no possibility of providing further input then the invoker should
|
||||
* pass <tt>true</tt>. It is not erroneous, and in fact it is quite
|
||||
* common, to pass <tt>false</tt> in one invocation and later discover that
|
||||
* no further input was actually available. It is critical, however, that
|
||||
* the final invocation of this method in a sequence of invocations always
|
||||
* pass <tt>true</tt> so that any remaining unencoded input will be treated
|
||||
* as being malformed.
|
||||
*
|
||||
* <p> This method works by invoking the {@link #encodeLoop encodeLoop}
|
||||
* method, interpreting its results, handling error conditions, and
|
||||
* reinvoking it as necessary. </p>
|
||||
*
|
||||
*
|
||||
* @param in
|
||||
* The input character buffer
|
||||
*
|
||||
* @param out
|
||||
* The output byte buffer
|
||||
*
|
||||
* @param endOfInput
|
||||
* <tt>true</tt> if, and only if, the invoker can provide no
|
||||
* additional input characters beyond those in the given buffer
|
||||
*
|
||||
* @return A coder-result object describing the reason for termination
|
||||
*
|
||||
* @throws IllegalStateException
|
||||
* If an encoding operation is already in progress and the previous
|
||||
* step was an invocation neither of the {@link #reset reset}
|
||||
* method, nor of this method with a value of <tt>false</tt> for
|
||||
* the <tt>endOfInput</tt> parameter, nor of this method with a
|
||||
* value of <tt>true</tt> for the <tt>endOfInput</tt> parameter
|
||||
* but a return value indicating an incomplete encoding operation
|
||||
*
|
||||
* @throws CoderMalfunctionError
|
||||
* If an invocation of the encodeLoop method threw
|
||||
* an unexpected exception
|
||||
*/
|
||||
public final CoderResult encode(CharBuffer in, ByteBuffer out,
|
||||
boolean endOfInput)
|
||||
{
|
||||
int newState = endOfInput ? ST_END : ST_CODING;
|
||||
if ((state != ST_RESET) && (state != ST_CODING)
|
||||
&& !(endOfInput && (state == ST_END)))
|
||||
throwIllegalStateException(state, newState);
|
||||
state = newState;
|
||||
|
||||
for (;;) {
|
||||
|
||||
CoderResult cr;
|
||||
try {
|
||||
cr = encodeLoop(in, out);
|
||||
} catch (BufferUnderflowException x) {
|
||||
throw new CoderMalfunctionError(x);
|
||||
} catch (BufferOverflowException x) {
|
||||
throw new CoderMalfunctionError(x);
|
||||
}
|
||||
|
||||
if (cr.isOverflow())
|
||||
return cr;
|
||||
|
||||
if (cr.isUnderflow()) {
|
||||
if (endOfInput && in.hasRemaining()) {
|
||||
cr = CoderResult.malformedForLength(in.remaining());
|
||||
// Fall through to malformed-input case
|
||||
} else {
|
||||
return cr;
|
||||
}
|
||||
}
|
||||
|
||||
CodingErrorAction action = null;
|
||||
if (cr.isMalformed())
|
||||
action = malformedInputAction;
|
||||
else if (cr.isUnmappable())
|
||||
action = unmappableCharacterAction;
|
||||
else
|
||||
assert false : cr.toString();
|
||||
|
||||
if (action == CodingErrorAction.REPORT)
|
||||
return cr;
|
||||
|
||||
if (action == CodingErrorAction.REPLACE) {
|
||||
if (out.remaining() < replacement.length)
|
||||
return CoderResult.OVERFLOW;
|
||||
out.put(replacement);
|
||||
}
|
||||
|
||||
if ((action == CodingErrorAction.IGNORE)
|
||||
|| (action == CodingErrorAction.REPLACE)) {
|
||||
// Skip erroneous input either way
|
||||
in.position(in.position() + cr.length());
|
||||
continue;
|
||||
}
|
||||
|
||||
assert false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Flushes this encoder.
|
||||
*
|
||||
* <p> Some encoders maintain internal state and may need to write some
|
||||
* final bytes to the output buffer once the overall input sequence has
|
||||
* been read.
|
||||
*
|
||||
* <p> Any additional output is written to the output buffer beginning at
|
||||
* its current position. At most {@link Buffer#remaining out.remaining()}
|
||||
* bytes will be written. The buffer's position will be advanced
|
||||
* appropriately, but its mark and limit will not be modified.
|
||||
*
|
||||
* <p> If this method completes successfully then it returns {@link
|
||||
* CoderResult#UNDERFLOW}. If there is insufficient room in the output
|
||||
* buffer then it returns {@link CoderResult#OVERFLOW}. If this happens
|
||||
* then this method must be invoked again, with an output buffer that has
|
||||
* more room, in order to complete the current <a href="#steps">encoding
|
||||
* operation</a>.
|
||||
*
|
||||
* <p> If this encoder has already been flushed then invoking this method
|
||||
* has no effect.
|
||||
*
|
||||
* <p> This method invokes the {@link #implFlush implFlush} method to
|
||||
* perform the actual flushing operation. </p>
|
||||
*
|
||||
* @param out
|
||||
* The output byte buffer
|
||||
*
|
||||
* @return A coder-result object, either {@link CoderResult#UNDERFLOW} or
|
||||
* {@link CoderResult#OVERFLOW}
|
||||
*
|
||||
* @throws IllegalStateException
|
||||
* If the previous step of the current encoding operation was an
|
||||
* invocation neither of the {@link #flush flush} method nor of
|
||||
* the three-argument {@link
|
||||
* #encode(CharBuffer,ByteBuffer,boolean) encode} method
|
||||
* with a value of <tt>true</tt> for the <tt>endOfInput</tt>
|
||||
* parameter
|
||||
*/
|
||||
public final CoderResult flush(ByteBuffer out) {
|
||||
if (state == ST_END) {
|
||||
CoderResult cr = implFlush(out);
|
||||
if (cr.isUnderflow())
|
||||
state = ST_FLUSHED;
|
||||
return cr;
|
||||
}
|
||||
|
||||
if (state != ST_FLUSHED)
|
||||
throwIllegalStateException(state, ST_FLUSHED);
|
||||
|
||||
return CoderResult.UNDERFLOW; // Already flushed
|
||||
}
|
||||
|
||||
/**
|
||||
* Flushes this encoder.
|
||||
*
|
||||
* <p> The default implementation of this method does nothing, and always
|
||||
* returns {@link CoderResult#UNDERFLOW}. This method should be overridden
|
||||
* by encoders that may need to write final bytes to the output buffer
|
||||
* once the entire input sequence has been read. </p>
|
||||
*
|
||||
* @param out
|
||||
* The output byte buffer
|
||||
*
|
||||
* @return A coder-result object, either {@link CoderResult#UNDERFLOW} or
|
||||
* {@link CoderResult#OVERFLOW}
|
||||
*/
|
||||
protected CoderResult implFlush(ByteBuffer out) {
|
||||
return CoderResult.UNDERFLOW;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets this encoder, clearing any internal state.
|
||||
*
|
||||
* <p> This method resets charset-independent state and also invokes the
|
||||
* {@link #implReset() implReset} method in order to perform any
|
||||
* charset-specific reset actions. </p>
|
||||
*
|
||||
* @return This encoder
|
||||
*
|
||||
*/
|
||||
public final CharsetEncoder reset() {
|
||||
implReset();
|
||||
state = ST_RESET;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets this encoder, clearing any charset-specific internal state.
|
||||
*
|
||||
* <p> The default implementation of this method does nothing. This method
|
||||
* should be overridden by encoders that maintain internal state. </p>
|
||||
*/
|
||||
protected void implReset() { }
|
||||
|
||||
/**
|
||||
* Encodes one or more characters into one or more bytes.
|
||||
*
|
||||
* <p> This method encapsulates the basic encoding loop, encoding as many
|
||||
* characters as possible until it either runs out of input, runs out of room
|
||||
* in the output buffer, or encounters an encoding error. This method is
|
||||
* invoked by the {@link #encode encode} method, which handles result
|
||||
* interpretation and error recovery.
|
||||
*
|
||||
* <p> The buffers are read from, and written to, starting at their current
|
||||
* positions. At most {@link Buffer#remaining in.remaining()} characters
|
||||
* will be read, and at most {@link Buffer#remaining out.remaining()}
|
||||
* bytes will be written. The buffers' positions will be advanced to
|
||||
* reflect the characters read and the bytes written, but their marks and
|
||||
* limits will not be modified.
|
||||
*
|
||||
* <p> This method returns a {@link CoderResult} object to describe its
|
||||
* reason for termination, in the same manner as the {@link #encode encode}
|
||||
* method. Most implementations of this method will handle encoding errors
|
||||
* by returning an appropriate result object for interpretation by the
|
||||
* {@link #encode encode} method. An optimized implementation may instead
|
||||
* examine the relevant error action and implement that action itself.
|
||||
*
|
||||
* <p> An implementation of this method may perform arbitrary lookahead by
|
||||
* returning {@link CoderResult#UNDERFLOW} until it receives sufficient
|
||||
* input. </p>
|
||||
*
|
||||
* @param in
|
||||
* The input character buffer
|
||||
*
|
||||
* @param out
|
||||
* The output byte buffer
|
||||
*
|
||||
* @return A coder-result object describing the reason for termination
|
||||
*/
|
||||
protected abstract CoderResult encodeLoop(CharBuffer in,
|
||||
ByteBuffer out);
|
||||
|
||||
/**
|
||||
* Convenience method that encodes the remaining content of a single input
|
||||
* character buffer into a newly-allocated byte buffer.
|
||||
*
|
||||
* <p> This method implements an entire <a href="#steps">encoding
|
||||
* operation</a>; that is, it resets this encoder, then it encodes the
|
||||
* characters in the given character buffer, and finally it flushes this
|
||||
* encoder. This method should therefore not be invoked if an encoding
|
||||
* operation is already in progress. </p>
|
||||
*
|
||||
* @param in
|
||||
* The input character buffer
|
||||
*
|
||||
* @return A newly-allocated byte buffer containing the result of the
|
||||
* encoding operation. The buffer's position will be zero and its
|
||||
* limit will follow the last byte written.
|
||||
*
|
||||
* @throws IllegalStateException
|
||||
* If an encoding operation is already in progress
|
||||
*
|
||||
* @throws MalformedInputException
|
||||
* If the character sequence starting at the input buffer's current
|
||||
* position is not a legal sixteen-bit Unicode sequence and the current malformed-input action
|
||||
* is {@link CodingErrorAction#REPORT}
|
||||
*
|
||||
* @throws UnmappableCharacterException
|
||||
* If the character sequence starting at the input buffer's current
|
||||
* position cannot be mapped to an equivalent byte sequence and
|
||||
* the current unmappable-character action is {@link
|
||||
* CodingErrorAction#REPORT}
|
||||
*/
|
||||
public final ByteBuffer encode(CharBuffer in)
|
||||
throws CharacterCodingException
|
||||
{
|
||||
int n = (int)(in.remaining() * averageBytesPerChar());
|
||||
ByteBuffer out = ByteBuffer.allocate(n);
|
||||
|
||||
if ((n == 0) && (in.remaining() == 0))
|
||||
return out;
|
||||
reset();
|
||||
for (;;) {
|
||||
CoderResult cr = in.hasRemaining() ?
|
||||
encode(in, out, true) : CoderResult.UNDERFLOW;
|
||||
if (cr.isUnderflow())
|
||||
cr = flush(out);
|
||||
|
||||
if (cr.isUnderflow())
|
||||
break;
|
||||
if (cr.isOverflow()) {
|
||||
n = 2*n + 1; // Ensure progress; n might be 0!
|
||||
ByteBuffer o = ByteBuffer.allocate(n);
|
||||
out.flip();
|
||||
o.put(out);
|
||||
out = o;
|
||||
continue;
|
||||
}
|
||||
cr.throwException();
|
||||
}
|
||||
out.flip();
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private boolean canEncode(CharBuffer cb) {
|
||||
if (state == ST_FLUSHED)
|
||||
reset();
|
||||
else if (state != ST_RESET)
|
||||
throwIllegalStateException(state, ST_CODING);
|
||||
CodingErrorAction ma = malformedInputAction();
|
||||
CodingErrorAction ua = unmappableCharacterAction();
|
||||
try {
|
||||
onMalformedInput(CodingErrorAction.REPORT);
|
||||
onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
encode(cb);
|
||||
} catch (CharacterCodingException x) {
|
||||
return false;
|
||||
} finally {
|
||||
onMalformedInput(ma);
|
||||
onUnmappableCharacter(ua);
|
||||
reset();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether or not this encoder can encode the given character.
|
||||
*
|
||||
* <p> This method returns <tt>false</tt> if the given character is a
|
||||
* surrogate character; such characters can be interpreted only when they
|
||||
* are members of a pair consisting of a high surrogate followed by a low
|
||||
* surrogate. The {@link #canEncode(java.lang.CharSequence)
|
||||
* canEncode(CharSequence)} method may be used to test whether or not a
|
||||
* character sequence can be encoded.
|
||||
*
|
||||
* <p> This method may modify this encoder's state; it should therefore not
|
||||
* be invoked if an <a href="#steps">encoding operation</a> is already in
|
||||
* progress.
|
||||
*
|
||||
* <p> The default implementation of this method is not very efficient; it
|
||||
* should generally be overridden to improve performance. </p>
|
||||
*
|
||||
* @param c
|
||||
* The given character
|
||||
*
|
||||
* @return <tt>true</tt> if, and only if, this encoder can encode
|
||||
* the given character
|
||||
*
|
||||
* @throws IllegalStateException
|
||||
* If an encoding operation is already in progress
|
||||
*/
|
||||
public boolean canEncode(char c) {
|
||||
CharBuffer cb = CharBuffer.allocate(1);
|
||||
cb.put(c);
|
||||
cb.flip();
|
||||
return canEncode(cb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether or not this encoder can encode the given character
|
||||
* sequence.
|
||||
*
|
||||
* <p> If this method returns <tt>false</tt> for a particular character
|
||||
* sequence then more information about why the sequence cannot be encoded
|
||||
* may be obtained by performing a full <a href="#steps">encoding
|
||||
* operation</a>.
|
||||
*
|
||||
* <p> This method may modify this encoder's state; it should therefore not
|
||||
* be invoked if an encoding operation is already in progress.
|
||||
*
|
||||
* <p> The default implementation of this method is not very efficient; it
|
||||
* should generally be overridden to improve performance. </p>
|
||||
*
|
||||
* @param cs
|
||||
* The given character sequence
|
||||
*
|
||||
* @return <tt>true</tt> if, and only if, this encoder can encode
|
||||
* the given character without throwing any exceptions and without
|
||||
* performing any replacements
|
||||
*
|
||||
* @throws IllegalStateException
|
||||
* If an encoding operation is already in progress
|
||||
*/
|
||||
public boolean canEncode(CharSequence cs) {
|
||||
CharBuffer cb;
|
||||
if (cs instanceof CharBuffer)
|
||||
cb = ((CharBuffer)cs).duplicate();
|
||||
else
|
||||
cb = CharBuffer.wrap(cs.toString());
|
||||
return canEncode(cb);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void throwIllegalStateException(int from, int to) {
|
||||
throw new IllegalStateException("Current state = " + stateNames[from]
|
||||
+ ", new state = " + stateNames[to]);
|
||||
}
|
||||
|
||||
}
|
||||
54
jdkSrc/jdk8/java/nio/charset/CoderMalfunctionError.java
Normal file
54
jdkSrc/jdk8/java/nio/charset/CoderMalfunctionError.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2001, 2007, 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;
|
||||
|
||||
|
||||
/**
|
||||
* Error thrown when the {@link CharsetDecoder#decodeLoop decodeLoop} method of
|
||||
* a {@link CharsetDecoder}, or the {@link CharsetEncoder#encodeLoop
|
||||
* encodeLoop} method of a {@link CharsetEncoder}, throws an unexpected
|
||||
* exception.
|
||||
*
|
||||
* @since 1.4
|
||||
*/
|
||||
|
||||
public class CoderMalfunctionError
|
||||
extends Error
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = -1151412348057794301L;
|
||||
|
||||
/**
|
||||
* Initializes an instance of this class.
|
||||
*
|
||||
* @param cause
|
||||
* The unexpected exception that was thrown
|
||||
*/
|
||||
public CoderMalfunctionError(Exception cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
||||
288
jdkSrc/jdk8/java/nio/charset/CoderResult.java
Normal file
288
jdkSrc/jdk8/java/nio/charset/CoderResult.java
Normal file
@@ -0,0 +1,288 @@
|
||||
/*
|
||||
* Copyright (c) 2001, 2013, 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;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.nio.*;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
|
||||
/**
|
||||
* A description of the result state of a coder.
|
||||
*
|
||||
* <p> A charset coder, that is, either a decoder or an encoder, consumes bytes
|
||||
* (or characters) from an input buffer, translates them, and writes the
|
||||
* resulting characters (or bytes) to an output buffer. A coding process
|
||||
* terminates for one of four categories of reasons, which are described by
|
||||
* instances of this class:
|
||||
*
|
||||
* <ul>
|
||||
*
|
||||
* <li><p> <i>Underflow</i> is reported when there is no more input to be
|
||||
* processed, or there is insufficient input and additional input is
|
||||
* required. This condition is represented by the unique result object
|
||||
* {@link #UNDERFLOW}, whose {@link #isUnderflow() isUnderflow} method
|
||||
* returns <tt>true</tt>. </p></li>
|
||||
*
|
||||
* <li><p> <i>Overflow</i> is reported when there is insufficient room
|
||||
* remaining in the output buffer. This condition is represented by the
|
||||
* unique result object {@link #OVERFLOW}, whose {@link #isOverflow()
|
||||
* isOverflow} method returns <tt>true</tt>. </p></li>
|
||||
*
|
||||
* <li><p> A <i>malformed-input error</i> is reported when a sequence of
|
||||
* input units is not well-formed. Such errors are described by instances of
|
||||
* this class whose {@link #isMalformed() isMalformed} method returns
|
||||
* <tt>true</tt> and whose {@link #length() length} method returns the length
|
||||
* of the malformed sequence. There is one unique instance of this class for
|
||||
* all malformed-input errors of a given length. </p></li>
|
||||
*
|
||||
* <li><p> An <i>unmappable-character error</i> is reported when a sequence
|
||||
* of input units denotes a character that cannot be represented in the
|
||||
* output charset. Such errors are described by instances of this class
|
||||
* whose {@link #isUnmappable() isUnmappable} method returns <tt>true</tt> and
|
||||
* whose {@link #length() length} method returns the length of the input
|
||||
* sequence denoting the unmappable character. There is one unique instance
|
||||
* of this class for all unmappable-character errors of a given length.
|
||||
* </p></li>
|
||||
*
|
||||
* </ul>
|
||||
*
|
||||
* <p> For convenience, the {@link #isError() isError} method returns <tt>true</tt>
|
||||
* for result objects that describe malformed-input and unmappable-character
|
||||
* errors but <tt>false</tt> for those that describe underflow or overflow
|
||||
* conditions. </p>
|
||||
*
|
||||
*
|
||||
* @author Mark Reinhold
|
||||
* @author JSR-51 Expert Group
|
||||
* @since 1.4
|
||||
*/
|
||||
|
||||
public class CoderResult {
|
||||
|
||||
private static final int CR_UNDERFLOW = 0;
|
||||
private static final int CR_OVERFLOW = 1;
|
||||
private static final int CR_ERROR_MIN = 2;
|
||||
private static final int CR_MALFORMED = 2;
|
||||
private static final int CR_UNMAPPABLE = 3;
|
||||
|
||||
private static final String[] names
|
||||
= { "UNDERFLOW", "OVERFLOW", "MALFORMED", "UNMAPPABLE" };
|
||||
|
||||
private final int type;
|
||||
private final int length;
|
||||
|
||||
private CoderResult(int type, int length) {
|
||||
this.type = type;
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string describing this coder result.
|
||||
*
|
||||
* @return A descriptive string
|
||||
*/
|
||||
public String toString() {
|
||||
String nm = names[type];
|
||||
return isError() ? nm + "[" + length + "]" : nm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether or not this object describes an underflow condition.
|
||||
*
|
||||
* @return <tt>true</tt> if, and only if, this object denotes underflow
|
||||
*/
|
||||
public boolean isUnderflow() {
|
||||
return (type == CR_UNDERFLOW);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether or not this object describes an overflow condition.
|
||||
*
|
||||
* @return <tt>true</tt> if, and only if, this object denotes overflow
|
||||
*/
|
||||
public boolean isOverflow() {
|
||||
return (type == CR_OVERFLOW);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether or not this object describes an error condition.
|
||||
*
|
||||
* @return <tt>true</tt> if, and only if, this object denotes either a
|
||||
* malformed-input error or an unmappable-character error
|
||||
*/
|
||||
public boolean isError() {
|
||||
return (type >= CR_ERROR_MIN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether or not this object describes a malformed-input error.
|
||||
*
|
||||
* @return <tt>true</tt> if, and only if, this object denotes a
|
||||
* malformed-input error
|
||||
*/
|
||||
public boolean isMalformed() {
|
||||
return (type == CR_MALFORMED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether or not this object describes an unmappable-character
|
||||
* error.
|
||||
*
|
||||
* @return <tt>true</tt> if, and only if, this object denotes an
|
||||
* unmappable-character error
|
||||
*/
|
||||
public boolean isUnmappable() {
|
||||
return (type == CR_UNMAPPABLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of the erroneous input described by this
|
||||
* object <i>(optional operation)</i>.
|
||||
*
|
||||
* @return The length of the erroneous input, a positive integer
|
||||
*
|
||||
* @throws UnsupportedOperationException
|
||||
* If this object does not describe an error condition, that is,
|
||||
* if the {@link #isError() isError} does not return <tt>true</tt>
|
||||
*/
|
||||
public int length() {
|
||||
if (!isError())
|
||||
throw new UnsupportedOperationException();
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Result object indicating underflow, meaning that either the input buffer
|
||||
* has been completely consumed or, if the input buffer is not yet empty,
|
||||
* that additional input is required.
|
||||
*/
|
||||
public static final CoderResult UNDERFLOW
|
||||
= new CoderResult(CR_UNDERFLOW, 0);
|
||||
|
||||
/**
|
||||
* Result object indicating overflow, meaning that there is insufficient
|
||||
* room in the output buffer.
|
||||
*/
|
||||
public static final CoderResult OVERFLOW
|
||||
= new CoderResult(CR_OVERFLOW, 0);
|
||||
|
||||
private static abstract class Cache {
|
||||
|
||||
private Map<Integer,WeakReference<CoderResult>> cache = null;
|
||||
|
||||
protected abstract CoderResult create(int len);
|
||||
|
||||
private synchronized CoderResult get(int len) {
|
||||
if (len <= 0)
|
||||
throw new IllegalArgumentException("Non-positive length");
|
||||
Integer k = new Integer(len);
|
||||
WeakReference<CoderResult> w;
|
||||
CoderResult e = null;
|
||||
if (cache == null) {
|
||||
cache = new HashMap<Integer,WeakReference<CoderResult>>();
|
||||
} else if ((w = cache.get(k)) != null) {
|
||||
e = w.get();
|
||||
}
|
||||
if (e == null) {
|
||||
e = create(len);
|
||||
cache.put(k, new WeakReference<CoderResult>(e));
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static Cache malformedCache
|
||||
= new Cache() {
|
||||
public CoderResult create(int len) {
|
||||
return new CoderResult(CR_MALFORMED, len);
|
||||
}};
|
||||
|
||||
/**
|
||||
* Static factory method that returns the unique object describing a
|
||||
* malformed-input error of the given length.
|
||||
*
|
||||
* @param length
|
||||
* The given length
|
||||
*
|
||||
* @return The requested coder-result object
|
||||
*/
|
||||
public static CoderResult malformedForLength(int length) {
|
||||
return malformedCache.get(length);
|
||||
}
|
||||
|
||||
private static Cache unmappableCache
|
||||
= new Cache() {
|
||||
public CoderResult create(int len) {
|
||||
return new CoderResult(CR_UNMAPPABLE, len);
|
||||
}};
|
||||
|
||||
/**
|
||||
* Static factory method that returns the unique result object describing
|
||||
* an unmappable-character error of the given length.
|
||||
*
|
||||
* @param length
|
||||
* The given length
|
||||
*
|
||||
* @return The requested coder-result object
|
||||
*/
|
||||
public static CoderResult unmappableForLength(int length) {
|
||||
return unmappableCache.get(length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception appropriate to the result described by this object.
|
||||
*
|
||||
* @throws BufferUnderflowException
|
||||
* If this object is {@link #UNDERFLOW}
|
||||
*
|
||||
* @throws BufferOverflowException
|
||||
* If this object is {@link #OVERFLOW}
|
||||
*
|
||||
* @throws MalformedInputException
|
||||
* If this object represents a malformed-input error; the
|
||||
* exception's length value will be that of this object
|
||||
*
|
||||
* @throws UnmappableCharacterException
|
||||
* If this object represents an unmappable-character error; the
|
||||
* exceptions length value will be that of this object
|
||||
*/
|
||||
public void throwException()
|
||||
throws CharacterCodingException
|
||||
{
|
||||
switch (type) {
|
||||
case CR_UNDERFLOW: throw new BufferUnderflowException();
|
||||
case CR_OVERFLOW: throw new BufferOverflowException();
|
||||
case CR_MALFORMED: throw new MalformedInputException(length);
|
||||
case CR_UNMAPPABLE: throw new UnmappableCharacterException(length);
|
||||
default:
|
||||
assert false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
84
jdkSrc/jdk8/java/nio/charset/CodingErrorAction.java
Normal file
84
jdkSrc/jdk8/java/nio/charset/CodingErrorAction.java
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (c) 2001, 2013, 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;
|
||||
|
||||
|
||||
/**
|
||||
* A typesafe enumeration for coding-error actions.
|
||||
*
|
||||
* <p> Instances of this class are used to specify how malformed-input and
|
||||
* unmappable-character errors are to be handled by charset <a
|
||||
* href="CharsetDecoder.html#cae">decoders</a> and <a
|
||||
* href="CharsetEncoder.html#cae">encoders</a>. </p>
|
||||
*
|
||||
*
|
||||
* @author Mark Reinhold
|
||||
* @author JSR-51 Expert Group
|
||||
* @since 1.4
|
||||
*/
|
||||
|
||||
public class CodingErrorAction {
|
||||
|
||||
private String name;
|
||||
|
||||
private CodingErrorAction(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Action indicating that a coding error is to be handled by dropping the
|
||||
* erroneous input and resuming the coding operation.
|
||||
*/
|
||||
public static final CodingErrorAction IGNORE
|
||||
= new CodingErrorAction("IGNORE");
|
||||
|
||||
/**
|
||||
* Action indicating that a coding error is to be handled by dropping the
|
||||
* erroneous input, appending the coder's replacement value to the output
|
||||
* buffer, and resuming the coding operation.
|
||||
*/
|
||||
public static final CodingErrorAction REPLACE
|
||||
= new CodingErrorAction("REPLACE");
|
||||
|
||||
/**
|
||||
* Action indicating that a coding error is to be reported, either by
|
||||
* returning a {@link CoderResult} object or by throwing a {@link
|
||||
* CharacterCodingException}, whichever is appropriate for the method
|
||||
* implementing the coding process.
|
||||
*/
|
||||
public static final CodingErrorAction REPORT
|
||||
= new CodingErrorAction("REPORT");
|
||||
|
||||
/**
|
||||
* Returns a string describing this action.
|
||||
*
|
||||
* @return A descriptive string
|
||||
*/
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2007, 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.
|
||||
*
|
||||
*/
|
||||
|
||||
// -- This file was mechanically generated: Do not edit! -- //
|
||||
|
||||
package java.nio.charset;
|
||||
|
||||
|
||||
/**
|
||||
* Unchecked exception thrown when a string that is not a
|
||||
* <a href=Charset.html#names>legal charset name</a> is used as such.
|
||||
*
|
||||
* @since 1.4
|
||||
*/
|
||||
|
||||
public class IllegalCharsetNameException
|
||||
extends IllegalArgumentException
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = 1457525358470002989L;
|
||||
|
||||
private String charsetName;
|
||||
|
||||
/**
|
||||
* Constructs an instance of this class.
|
||||
*
|
||||
* @param charsetName
|
||||
* The illegal charset name
|
||||
*/
|
||||
public IllegalCharsetNameException(String charsetName) {
|
||||
super(String.valueOf(charsetName));
|
||||
this.charsetName = charsetName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the illegal charset name.
|
||||
*
|
||||
* @return The illegal charset name
|
||||
*/
|
||||
public String getCharsetName() {
|
||||
return charsetName;
|
||||
}
|
||||
|
||||
}
|
||||
70
jdkSrc/jdk8/java/nio/charset/MalformedInputException.java
Normal file
70
jdkSrc/jdk8/java/nio/charset/MalformedInputException.java
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2013, 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;
|
||||
|
||||
|
||||
/**
|
||||
* Checked exception thrown when an input byte sequence is not legal for given
|
||||
* charset, or an input character sequence is not a legal sixteen-bit Unicode
|
||||
* sequence.
|
||||
*
|
||||
* @since 1.4
|
||||
*/
|
||||
|
||||
public class MalformedInputException
|
||||
extends CharacterCodingException
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = -3438823399834806194L;
|
||||
|
||||
private int inputLength;
|
||||
|
||||
/**
|
||||
* Constructs an {@code MalformedInputException} with the given
|
||||
* length.
|
||||
* @param inputLength the length of the input
|
||||
*/
|
||||
public MalformedInputException(int inputLength) {
|
||||
this.inputLength = inputLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of the input.
|
||||
* @return the length of the input
|
||||
*/
|
||||
public int getInputLength() {
|
||||
return inputLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message.
|
||||
* @return the message
|
||||
*/
|
||||
public String getMessage() {
|
||||
return "Input length = " + inputLength;
|
||||
}
|
||||
|
||||
}
|
||||
66
jdkSrc/jdk8/java/nio/charset/StandardCharsets.java
Normal file
66
jdkSrc/jdk8/java/nio/charset/StandardCharsets.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 StandardCharsets {
|
||||
|
||||
private StandardCharsets() {
|
||||
throw new AssertionError("No java.nio.charset.StandardCharsets 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");
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2001, 2013, 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;
|
||||
|
||||
|
||||
/**
|
||||
* Checked exception thrown when an input character (or byte) sequence
|
||||
* is valid but cannot be mapped to an output byte (or character)
|
||||
* sequence.
|
||||
*
|
||||
* @since 1.4
|
||||
*/
|
||||
|
||||
public class UnmappableCharacterException
|
||||
extends CharacterCodingException
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = -7026962371537706123L;
|
||||
|
||||
private int inputLength;
|
||||
|
||||
/**
|
||||
* Constructs an {@code UnmappableCharacterException} with the
|
||||
* given length.
|
||||
* @param inputLength the length of the input
|
||||
*/
|
||||
public UnmappableCharacterException(int inputLength) {
|
||||
this.inputLength = inputLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of the input.
|
||||
* @return the length of the input
|
||||
*/
|
||||
public int getInputLength() {
|
||||
return inputLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message.
|
||||
* @return the message
|
||||
*/
|
||||
public String getMessage() {
|
||||
return "Input length = " + inputLength;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2007, 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.
|
||||
*
|
||||
*/
|
||||
|
||||
// -- This file was mechanically generated: Do not edit! -- //
|
||||
|
||||
package java.nio.charset;
|
||||
|
||||
|
||||
/**
|
||||
* Unchecked exception thrown when no support is available
|
||||
* for a requested charset.
|
||||
*
|
||||
* @since 1.4
|
||||
*/
|
||||
|
||||
public class UnsupportedCharsetException
|
||||
extends IllegalArgumentException
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = 1490765524727386367L;
|
||||
|
||||
private String charsetName;
|
||||
|
||||
/**
|
||||
* Constructs an instance of this class.
|
||||
*
|
||||
* @param charsetName
|
||||
* The name of the unsupported charset
|
||||
*/
|
||||
public UnsupportedCharsetException(String charsetName) {
|
||||
super(String.valueOf(charsetName));
|
||||
this.charsetName = charsetName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the name of the unsupported charset.
|
||||
*
|
||||
* @return The name of the unsupported charset
|
||||
*/
|
||||
public String getCharsetName() {
|
||||
return charsetName;
|
||||
}
|
||||
|
||||
}
|
||||
110
jdkSrc/jdk8/java/nio/charset/spi/CharsetProvider.java
Normal file
110
jdkSrc/jdk8/java/nio/charset/spi/CharsetProvider.java
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2013, 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.spi;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Iterator;
|
||||
|
||||
|
||||
/**
|
||||
* Charset service-provider class.
|
||||
*
|
||||
* <p> A charset provider is a concrete subclass of this class that has a
|
||||
* zero-argument constructor and some number of associated charset
|
||||
* implementation classes. Charset providers may be installed in an instance
|
||||
* of the Java platform as extensions, that is, jar files placed into any of
|
||||
* the usual extension directories. Providers may also be made available by
|
||||
* adding them to the applet or application class path or by some other
|
||||
* platform-specific means. Charset providers are looked up via the current
|
||||
* thread's {@link java.lang.Thread#getContextClassLoader() context class
|
||||
* loader}.
|
||||
*
|
||||
* <p> A charset provider identifies itself with a provider-configuration file
|
||||
* named <tt>java.nio.charset.spi.CharsetProvider</tt> in the resource
|
||||
* directory <tt>META-INF/services</tt>. The file should contain a list of
|
||||
* fully-qualified concrete charset-provider class names, one per line. A line
|
||||
* is terminated by any one of a line feed (<tt>'\n'</tt>), a carriage return
|
||||
* (<tt>'\r'</tt>), or a carriage return followed immediately by a line feed.
|
||||
* Space and tab characters surrounding each name, as well as blank lines, are
|
||||
* ignored. The comment character is <tt>'#'</tt> (<tt>'\u0023'</tt>); on
|
||||
* each line all characters following the first comment character are ignored.
|
||||
* The file must be encoded in UTF-8.
|
||||
*
|
||||
* <p> If a particular concrete charset provider class is named in more than
|
||||
* one configuration file, or is named in the same configuration file more than
|
||||
* once, then the duplicates will be ignored. The configuration file naming a
|
||||
* particular provider need not be in the same jar file or other distribution
|
||||
* unit as the provider itself. The provider must be accessible from the same
|
||||
* class loader that was initially queried to locate the configuration file;
|
||||
* this is not necessarily the class loader that loaded the file. </p>
|
||||
*
|
||||
*
|
||||
* @author Mark Reinhold
|
||||
* @author JSR-51 Expert Group
|
||||
* @since 1.4
|
||||
*
|
||||
* @see java.nio.charset.Charset
|
||||
*/
|
||||
|
||||
public abstract class CharsetProvider {
|
||||
|
||||
/**
|
||||
* Initializes a new charset provider.
|
||||
*
|
||||
* @throws SecurityException
|
||||
* If a security manager has been installed and it denies
|
||||
* {@link RuntimePermission}<tt>("charsetProvider")</tt>
|
||||
*/
|
||||
protected CharsetProvider() {
|
||||
SecurityManager sm = System.getSecurityManager();
|
||||
if (sm != null)
|
||||
sm.checkPermission(new RuntimePermission("charsetProvider"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an iterator that iterates over the charsets supported by this
|
||||
* provider. This method is used in the implementation of the {@link
|
||||
* java.nio.charset.Charset#availableCharsets Charset.availableCharsets}
|
||||
* method.
|
||||
*
|
||||
* @return The new iterator
|
||||
*/
|
||||
public abstract Iterator<Charset> charsets();
|
||||
|
||||
/**
|
||||
* Retrieves a charset for the given charset name.
|
||||
*
|
||||
* @param charsetName
|
||||
* The name of the requested charset; may be either
|
||||
* a canonical name or an alias
|
||||
*
|
||||
* @return A charset object for the named charset,
|
||||
* or <tt>null</tt> if the named charset
|
||||
* is not supported by this provider
|
||||
*/
|
||||
public abstract Charset charsetForName(String charsetName);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user