org.marketcetera.util.unicode
Enum UnicodeCharset

java.lang.Object
  extended by java.lang.Enum<UnicodeCharset>
      extended by org.marketcetera.util.unicode.UnicodeCharset
All Implemented Interfaces:
Serializable, Comparable<UnicodeCharset>

public enum UnicodeCharset
extends Enum<UnicodeCharset>

A thin wrapper around Charset for the Unicode charsets.

Notes:

Java inserts a BOM when a string is encoded into a byte array with the generic (neither LE or BE) UTF-16 charset. Java does not do that for the generic UTF-32, or UTF-8. As a result, the generic UTF-16 and UTF-32 are avoided by other classes in this package, opting instead for the more consistent LE/BE variants that never insert a BOM (and, upon decoding, ignore one if present).

Since Java 5, strings are internally stored in UTF-16, not UCS-2, which means that they can represent code points above 0xFFFF.

UTF-32 encodings may not be supported on all platforms.

Since:
0.6.0
Version:
$Id: UnicodeCharset.java 16154 2012-07-14 16:34:05Z colin $
Author:
tlerios@marketcetera.com

Enum Constant Summary
UTF16
           
UTF16BE
           
UTF16LE
           
UTF32
           
UTF32BE
           
UTF32LE
           
UTF8
           
 
Method Summary
 void assertSupported()
          Asserts that the JVM supports the receiver's charset.
 String decode(byte[] data)
          Decodes the given byte array using the receiver's charset, and returns the result.
 String decode(byte[] data, int offset, int length)
          Decodes the given portion of the given byte array using the receiver's charset, and returns the result.
 byte[] encode(String data)
          Encodes the given string using the receiver's charset, and returns the result.
 Charset getCharset()
          Returns the receiver's wrapped Java charset.
 String getName()
          Returns the receiver's name.
 boolean isSupported()
          Checks whether the JVM supports the receiver's charset.
static UnicodeCharset valueOf(String name)
          Returns the enum constant of this type with the specified name.
static UnicodeCharset[] values()
          Returns an array containing the constants of this enum type, in the order they are declared.
 
Methods inherited from class java.lang.Enum
clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Enum Constant Detail

UTF8

public static final UnicodeCharset UTF8

UTF16BE

public static final UnicodeCharset UTF16BE

UTF16LE

public static final UnicodeCharset UTF16LE

UTF16

public static final UnicodeCharset UTF16

UTF32BE

public static final UnicodeCharset UTF32BE

UTF32LE

public static final UnicodeCharset UTF32LE

UTF32

public static final UnicodeCharset UTF32
Method Detail

values

public static UnicodeCharset[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
for (UnicodeCharset c : UnicodeCharset.values())
    System.out.println(c);

Returns:
an array containing the constants of this enum type, in the order they are declared

valueOf

public static UnicodeCharset valueOf(String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

Parameters:
name - the name of the enum constant to be returned.
Returns:
the enum constant with the specified name
Throws:
IllegalArgumentException - if this enum type has no constant with the specified name
NullPointerException - if the argument is null

getName

public String getName()
Returns the receiver's name.

Returns:
The name.

getCharset

public Charset getCharset()
Returns the receiver's wrapped Java charset.

Returns:
The charset. It is null if the JVM does not support this charset.

isSupported

public boolean isSupported()
Checks whether the JVM supports the receiver's charset.

Returns:
True if so.

assertSupported

public void assertSupported()
                     throws I18NException
Asserts that the JVM supports the receiver's charset.

Throws:
I18NException - Thrown if it does not.

decode

public String decode(byte[] data,
                     int offset,
                     int length)
              throws I18NException
Decodes the given portion of the given byte array using the receiver's charset, and returns the result.

Parameters:
data - The byte array, which may be null.
offset - The starting point for decoding.
length - The number of bytes to decode.
Returns:
The decoded string; it is null if the given byte array is null.
Throws:
I18NException - Thrown if the receiver is not a supported JVM charset.

decode

public String decode(byte[] data)
              throws I18NException
Decodes the given byte array using the receiver's charset, and returns the result.

Parameters:
data - The byte array, which may be null.
Returns:
The decoded string; it is null if the given byte array is null.
Throws:
I18NException - Thrown if the receiver is not a supported JVM charset.

encode

public byte[] encode(String data)
              throws I18NException
Encodes the given string using the receiver's charset, and returns the result.

Parameters:
data - The string, which may be null.
Returns:
The encoded byte array; it is null if the given string is null.
Throws:
I18NException - Thrown if the receiver is not a supported JVM charset.


Copyright © 2012. All Rights Reserved.