org.pfsw.text
Class CheckedCharsetName

java.lang.Object
  extended by org.pfsw.text.CheckedCharsetName
All Implemented Interfaces:
java.io.Serializable, org.pfsw.bif.text.ICheckedCharset

public class CheckedCharsetName
extends java.lang.Object
implements org.pfsw.bif.text.ICheckedCharset, java.io.Serializable

This is a helper class that represents a character encoding name that has already been checked if it is supported by the platform.

It helps to avoid the annoying try-catch code-pollution every time you use a valid encoding, but the API requires the handling of UnsupportedEncodingException even if it cannot occur at all.

See Also:
Serialized Form

Field Summary
static CheckedCharsetName DEFAULT
          The platform default file encoding.
static CheckedCharsetName ISO_8859_1
          The pre-defined constant for Java standard charset ISO-8859-1.
static CheckedCharsetName US_ASCII
          The pre-defined constant for Java standard charset US-ASCII.
static CheckedCharsetName UTF_16
          The pre-defined constant for Java standard charset UTF-16.
static CheckedCharsetName UTF_16BE
          The pre-defined constant for Java standard charset UTF-16BE.
static CheckedCharsetName UTF_16LE
          The pre-defined constant for Java standard charset UTF-16LE.
static CheckedCharsetName UTF_8
          The pre-defined constant for Java standard charset UTF-8.
 
Constructor Summary
CheckedCharsetName(java.lang.String encodingName)
          Initialize the new instance with an encoding name (e.g.
 
Method Summary
static void checkEncoding(java.lang.String charsetName)
          Checks whether or not the given encoding is supported.
static CheckedCharsetName create(java.lang.String encodingName)
          Creates a new instance based on the given encoding name.
 boolean equals(java.lang.Object obj)
           
 byte[] getBytes(java.lang.String string)
          Returns the bytes of the given string as array encoded to the charset this instance represents, avoiding the unnecessary exception handling (UnsupportedEncodingException).
 java.nio.charset.Charset getCharset()
          Returns the corresponding charset object.
 java.lang.String getName()
          Returns the pure upper case encoding name (e.g.
 int hashCode()
           
 java.lang.String newString(byte[] bytes)
          Returns a new String initialized with the given bytes which are expected to be encoded according to the character set this instance represents, avoiding the unnecessary exception handling (UnsupportedEncodingException).
 java.lang.String toString()
          Returns the pure upper case encoding name (e.g.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

US_ASCII

public static final CheckedCharsetName US_ASCII
The pre-defined constant for Java standard charset US-ASCII.


ISO_8859_1

public static final CheckedCharsetName ISO_8859_1
The pre-defined constant for Java standard charset ISO-8859-1.


UTF_8

public static final CheckedCharsetName UTF_8
The pre-defined constant for Java standard charset UTF-8.


UTF_16

public static final CheckedCharsetName UTF_16
The pre-defined constant for Java standard charset UTF-16.


UTF_16BE

public static final CheckedCharsetName UTF_16BE
The pre-defined constant for Java standard charset UTF-16BE.


UTF_16LE

public static final CheckedCharsetName UTF_16LE
The pre-defined constant for Java standard charset UTF-16LE.


DEFAULT

public static final CheckedCharsetName DEFAULT
The platform default file encoding.

Constructor Detail

CheckedCharsetName

public CheckedCharsetName(java.lang.String encodingName)
                   throws java.io.UnsupportedEncodingException
Initialize the new instance with an encoding name (e.g. "UTF-8").

Throws:
java.io.UnsupportedEncodingException
Method Detail

checkEncoding

public static void checkEncoding(java.lang.String charsetName)
                          throws java.io.UnsupportedEncodingException
Checks whether or not the given encoding is supported.

Parameters:
charsetName - The name of the encoding to check (e.g. "UTF-16")
Throws:
java.io.UnsupportedEncodingException - Will be thrown if the encoding is not supported.

create

public static CheckedCharsetName create(java.lang.String encodingName)
                                 throws java.io.UnsupportedEncodingException
Creates a new instance based on the given encoding name.

Parameters:
encodingName - The name of the encoding to check (e.g. "UTF-16")
Returns:
Returns a new instance of CheckedCharsetName after checking the given charset name
Throws:
java.io.UnsupportedEncodingException - Will be thrown if the encoding is not supported.

getName

public java.lang.String getName()
Returns the pure upper case encoding name (e.g. "UTF-8").

Specified by:
getName in interface org.pfsw.bif.text.ICheckedCharset

getCharset

public java.nio.charset.Charset getCharset()
Returns the corresponding charset object.

Specified by:
getCharset in interface org.pfsw.bif.text.ICheckedCharset

toString

public java.lang.String toString()
Returns the pure upper case encoding name (e.g. "ISO-8859-1").

Overrides:
toString in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

newString

public java.lang.String newString(byte[] bytes)
Returns a new String initialized with the given bytes which are expected to be encoded according to the character set this instance represents, avoiding the unnecessary exception handling (UnsupportedEncodingException).

It is the same like: new String(bytes, checkedCharsetName.toString());

Specified by:
newString in interface org.pfsw.bif.text.ICheckedCharset
Parameters:
bytes - The byte to create a string from.
Returns:
A new string built from the given bytes that were encoded with the charset of this instance.

getBytes

public byte[] getBytes(java.lang.String string)
Returns the bytes of the given string as array encoded to the charset this instance represents, avoiding the unnecessary exception handling (UnsupportedEncodingException).

Specified by:
getBytes in interface org.pfsw.bif.text.ICheckedCharset
Parameters:
string - The string to be converted to a byte array using this instance's charset.
Returns:
The encoded byte array.