Sdílet prostřednictvím


Charset Class

Definition

A named mapping between sequences of sixteen-bit Unicode code units and sequences of bytes.

[Android.Runtime.Register("java/nio/charset/Charset", DoNotGenerateAcw=true)]
public abstract class Charset : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.Lang.IComparable
[<Android.Runtime.Register("java/nio/charset/Charset", DoNotGenerateAcw=true)>]
type Charset = class
    inherit Object
    interface IComparable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Inheritance
Charset
Attributes
Implements

Remarks

A named mapping between sequences of sixteen-bit Unicode code units 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.

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 java.nio.charset.spi.CharsetProvider class.

All of the methods defined in this class are safe for use by multiple concurrent threads.

"charenc"><h2>Charset names</h2>

Charsets are named by strings composed of the following characters:

<ul>

<li> The uppercase letters 'A' through 'Z' ('&#92;u0041'&nbsp;through&nbsp;'&#92;u005a'),

<li> The lowercase letters 'a' through 'z' ('&#92;u0061'&nbsp;through&nbsp;'&#92;u007a'),

<li> The digits '0' through '9' ('&#92;u0030'&nbsp;through&nbsp;'&#92;u0039'),

<li> The dash character '-' ('&#92;u002d',&nbsp;<small>HYPHEN-MINUS</small>),

<li> The plus character '+' ('&#92;u002b',&nbsp;<small>PLUS SIGN</small>),

<li> The period character '.' ('&#92;u002e',&nbsp;<small>FULL STOP</small>),

<li> The colon character ':' ('&#92;u003a',&nbsp;<small>COLON</small>), and

<li> The underscore character '_' ('&#92;u005f',&nbsp;<small>LOW&nbsp;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 RFC&nbsp;2278:&nbsp;IANA Charset Registration Procedureshttp://www.ietf.org/rfc/rfc2278.txt.

Every charset has a canonical name and may also have one or more aliases. The canonical name is returned by the #name() name method of this class. Canonical names are, by convention, usually in upper case. The aliases of a charset are returned by the #aliases() aliases method.

"hn">Some charsets have an <i>historical name</i> that is defined for compatibility with previous versions of the Java platform. A charset's historical name is either its canonical name or one of its aliases. The historical name is returned by the getEncoding() methods of the java.io.InputStreamReader#getEncoding InputStreamReader and java.io.OutputStreamWriter#getEncoding OutputStreamWriter classes.

"iana"> If a charset listed in the IANA Charset Registryhttp://www.iana.org/assignments/character-sets 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 MIME-preferred. 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 "X-" or "x-".

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>

"standard">Every implementation of the Java platform is required to support the following standard charsets. Consult the release documentation for your implementation to see if any other charsets are supported. The behavior of such optional charsets may differ between implementations.

<blockquote><table class="striped" style="width:80%"> <caption style="display:none">Description of standard charsets</caption> <thead> <tr><th scope="col" style="text-align:left">Charset</th><th scope="col" style="text-align:left">Description</th></tr> </thead> <tbody> <tr><th scope="row" style="vertical-align:top">US-ASCII</th> <td>Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set</td></tr> <tr><th scope="row" style="vertical-align:top">ISO-8859-1&nbsp;&nbsp;</th> <td>ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1</td></tr> <tr><th scope="row" style="vertical-align:top">UTF-8</th> <td>Eight-bit UCS Transformation Format</td></tr> <tr><th scope="row" style="vertical-align:top">UTF-16BE</th> <td>Sixteen-bit UCS Transformation Format, big-endian byte&nbsp;order</td></tr> <tr><th scope="row" style="vertical-align:top">UTF-16LE</th> <td>Sixteen-bit UCS Transformation Format, little-endian byte&nbsp;order</td></tr> <tr><th scope="row" style="vertical-align:top">UTF-16</th> <td>Sixteen-bit UCS Transformation Format, byte&nbsp;order identified by an optional byte-order mark</td></tr> </tbody> </table></blockquote>

The UTF-8 charset is specified by RFC&nbsp;2279http://www.ietf.org/rfc/rfc2279.txt; the transformation format upon which it is based is specified in Amendment&nbsp;2 of ISO&nbsp;10646-1 and is also described in the Unicode Standardhttp://www.unicode.org/unicode/standard/standard.html.

The UTF-16 charsets are specified by RFC&nbsp;2781http://www.ietf.org/rfc/rfc2781.txt; the transformation formats upon which they are based are specified in Amendment&nbsp;1 of ISO&nbsp;10646-1 and are also described in the Unicode Standardhttp://www.unicode.org/unicode/standard/standard.html.

The UTF-16 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 byte-order mark represented by the Unicode character '&#92;uFEFF'. Byte-order marks are handled as follows:

<ul>

<li>

When decoding, the UTF-16BE and UTF-16LE 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.

</li>

<li>

When decoding, the UTF-16 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.

</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>.

Android note: The Android platform default is always UTF-8.

The StandardCharsets class defines constants for each of the standard charsets.

<h2>Terminology</h2>

The name of this class is taken from the terms used in RFC&nbsp;2278http://www.ietf.org/rfc/rfc2278.txt. In that document a charset 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 charset as a synonym for coded character set.)

A coded character set is a mapping between a set of abstract characters and a set of integers. US-ASCII, ISO&nbsp;8859-1, JIS&nbsp;X&nbsp;0201, and Unicode are examples of coded character sets.

Some standards have defined a character set 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 character set and coded character set is rarely used in practice; the former has become a short form for the latter, including in the Java API specification.

A character-encoding scheme is a mapping between one or more coded character sets and a set of octet (eight-bit byte) sequences. UTF-8, UTF-16, ISO&nbsp;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.

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 US-ASCII is both the name of a coded character set and of the charset that encodes it, while EUC-JP is the name of the charset that encodes the JIS&nbsp;X&nbsp;0201, JIS&nbsp;X&nbsp;0208, and JIS&nbsp;X&nbsp;0212 coded character sets for the Japanese language.

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.

Added in 1.4.

Java documentation for java.nio.charset.Charset.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

Charset(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Charset(String, String[])

Initializes a new charset with the given canonical name and alias set.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
IsRegistered

Tells whether or not this charset is registered in the IANA Charset Registry.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

Aliases()

Returns a set containing this charset's aliases.

AvailableCharsets()

Constructs a sorted map from canonical charset names to charset objects.

CanEncode()

Tells whether or not this charset supports encoding.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
CompareTo(Charset)

Compares this charset to another.

Contains(Charset)

Tells whether or not this charset contains the given charset.

Decode(ByteBuffer)

Convenience method that decodes bytes in this charset into Unicode characters.

DefaultCharset()

Returns the default charset of this Java virtual machine.

DisplayName()

Returns this charset's human-readable name for the default locale.

DisplayName(Locale)

Returns this charset's human-readable name for the given locale.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Encode(CharBuffer)

Convenience method that encodes Unicode characters into bytes in this charset.

Encode(String)

Convenience method that encodes a string into bytes in this charset.

Equals(Object)

Tells whether or not this object is equal to another.

ForName(String)

Returns a charset object for the named charset.

GetHashCode()

Computes a hashcode for this charset.

IsSupported(String)

Tells whether the named charset is supported.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Name()

Returns this charset's canonical name.

NewDecoder()

Constructs a new decoder for this charset.

NewEncoder()

Constructs a new encoder for this charset.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
ToArray<T>() (Inherited from Object)
ToString()

Returns a string describing this charset.

UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IComparable.CompareTo(Object)
IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to