org.jagatoo.util.io
Enum UnicodeBOM

java.lang.Object
  extended by java.lang.Enum<UnicodeBOM>
      extended by org.jagatoo.util.io.UnicodeBOM
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<UnicodeBOM>

public enum UnicodeBOM
extends java.lang.Enum<UnicodeBOM>

Unicode BOM model.

Author:
Marvin Froehlich (aka Qudus)

Enum Constant Summary
BOCU_1
           
GB_18030
           
SUSU
           
UTF_1
           
UTF_16_BE
           
UTF_16_LE
           
UTF_32_BE
           
UTF_32_LE
           
UTF_7a
           
UTF_7b
           
UTF_7c
           
UTF_7d
           
UTF_8
           
UTF_EBCDIC
           
 
Method Summary
 int getBOM()
          Gets the BOM code.
 java.nio.charset.Charset getCharset()
          Gets the corresponding Charset or null, if not available.
 int getLength()
          The theoretical byte length.
 int getLength(int bom)
          The actual byte length of the given BOM.
static UnicodeBOM readBOM(java.io.File file)
          Only ready up to the first four bytes of the file and tries to recognize the unicode BOM from these data.
static UnicodeBOM recognize(int bom)
           Attempts to recognize the passed unicode BOM.
static UnicodeBOM removeBOM(java.io.File file, java.io.File temp)
          Removes the BOM bytes from the input file, if and only if one was found.
static UnicodeBOM removeBOM(java.io.File file, java.io.File temp, java.nio.charset.Charset targetCharset)
          Removes the BOM bytes from the input file, if and only if one was found.
static UnicodeBOM skipBOM(java.io.InputStream in)
          Skips the bytes, covered by a possible unicode BOM, or, if not recognized, does nothing.
static UnicodeBOM valueOf(int bom)
           Attempts to recognize the passed unicode BOM.
static UnicodeBOM valueOf(java.lang.String name)
          Returns the enum constant of this type with the specified name.
static UnicodeBOM[] 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

UTF_8

public static final UnicodeBOM UTF_8

UTF_16_BE

public static final UnicodeBOM UTF_16_BE

UTF_16_LE

public static final UnicodeBOM UTF_16_LE

UTF_32_BE

public static final UnicodeBOM UTF_32_BE

UTF_32_LE

public static final UnicodeBOM UTF_32_LE

UTF_7a

public static final UnicodeBOM UTF_7a

UTF_7b

public static final UnicodeBOM UTF_7b

UTF_7c

public static final UnicodeBOM UTF_7c

UTF_7d

public static final UnicodeBOM UTF_7d

UTF_1

public static final UnicodeBOM UTF_1

UTF_EBCDIC

public static final UnicodeBOM UTF_EBCDIC

SUSU

public static final UnicodeBOM SUSU

BOCU_1

public static final UnicodeBOM BOCU_1

GB_18030

public static final UnicodeBOM GB_18030
Method Detail

values

public static UnicodeBOM[] 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 (UnicodeBOM c : UnicodeBOM.values())
    System.out.println(c);

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

valueOf

public static UnicodeBOM valueOf(java.lang.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:
java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
java.lang.NullPointerException - if the argument is null

getBOM

public final int getBOM()
Gets the BOM code.

Returns:
the BOM code.

getLength

public final int getLength()
The theoretical byte length. Could be more for .

Returns:
theoretical length. Could be more for .

getLength

public final int getLength(int bom)
The actual byte length of the given BOM.

Parameters:
bom - the 4 byte bom (first 4 bytes of the file)
Returns:
the actual byte length of the given BOM.

getCharset

public final java.nio.charset.Charset getCharset()
Gets the corresponding Charset or null, if not available.

Returns:
the corresponding Charset or null, if not available.

recognize

public static UnicodeBOM recognize(int bom)

Attempts to recognize the passed unicode BOM. If it can't be recognized, null is returned.

The difference to the valueOf(int) method is, that recognize(int) expects the first four bytes of the file, where a BOM of length 3 appears in the first three byte and hence the value is shifted to the left by one byte while valueOf(int) takes the concrete BOM code.

Parameters:
bom - the 4 byte bom (first 4 bytes of the file)
Returns:
the recognized UnicodeBOM or null.
See Also:
valueOf(int)

valueOf

public static UnicodeBOM valueOf(int bom)

Attempts to recognize the passed unicode BOM. If it can't be recognized, null is returned.

The difference to the recognize(int) method is, that valueOf(int) takes the concrete BOM code while recognize(int) expects the first four bytes of the file, where a BOM of length 3 appears in the first three byte and hence the value is shifted to the left by one byte.

Parameters:
bom - the BOM code
Returns:
the recognized UnicodeBOM or null.
See Also:
recognize(int)

skipBOM

public static UnicodeBOM skipBOM(java.io.InputStream in)
                          throws java.io.IOException
Skips the bytes, covered by a possible unicode BOM, or, if not recognized, does nothing. The passed InputStream needs to support mark/reset.

Parameters:
in -
Returns:
the recognized UnicodeBOM or null.
Throws:
java.io.IOException - if anything went wrong.

readBOM

public static UnicodeBOM readBOM(java.io.File file)
                          throws java.io.IOException
Only ready up to the first four bytes of the file and tries to recognize the unicode BOM from these data.

Parameters:
file - the file to probe
Returns:
the recognized UnicodeBOM or null.
Throws:
java.io.IOException - if something went wrong

removeBOM

public static UnicodeBOM removeBOM(java.io.File file,
                                   java.io.File temp,
                                   java.nio.charset.Charset targetCharset)
                            throws java.io.IOException
Removes the BOM bytes from the input file, if and only if one was found. The method uses a temp file to opcy the contents, which replaces the input file afterwards.

Parameters:
file - the input file
temp - the temp file name
targetCharset - the charset to use for the new file or null for default
Returns:
the recognized UnicodeBOM or null.
Throws:
java.io.IOException - if something went wrong

removeBOM

public static UnicodeBOM removeBOM(java.io.File file,
                                   java.io.File temp)
                            throws java.io.IOException
Removes the BOM bytes from the input file, if and only if one was found. The method uses a temp file to opcy the contents, which replaces the input file afterwards.

Parameters:
file - the input file
temp - the temp file name
Returns:
the recognized UnicodeBOM or null.
Throws:
java.io.IOException - if something went wrong