Groovy 2.2.0

groovy.util
[Java] Class CharsetToolkit

java.lang.Object
  groovy.util.CharsetToolkit

public class CharsetToolkit
extends Object

Utility class to guess the encoding of a given text file.

Unicode files encoded in UTF-16 (low or big endian) or UTF-8 files with a Byte Order Marker are correctly discovered. For UTF-8 files with no BOM, if the buffer is wide enough, the charset should also be discovered.

A byte buffer of 4KB is used to be able to guess the encoding.

Usage:

 CharsetToolkit toolkit = new CharsetToolkit(file);

 // guess the encoding
 Charset guessedCharset = toolkit.getCharset();

 // create a reader with the correct charset
 BufferedReader reader = toolkit.getReader();

 // read the file content
 String line;
 while ((line = br.readLine())!= null)
 {
     System.out.println(line);
 }
 
Authors:
Guillaume Laforge


Constructor Summary
CharsetToolkit(File file)

Constructor of the CharsetToolkit utility class.

 
Method Summary
static Charset[] getAvailableCharsets()

Charset getCharset()

Charset getDefaultCharset()

Retrieves the default Charset

static Charset getDefaultSystemCharset()

boolean getEnforce8Bit()

Gets the enforce8Bit flag, in case we do not want to ever get a US-ASCII encoding.

BufferedReader getReader()

boolean hasUTF16BEBom()

boolean hasUTF16LEBom()

Gets a BufferedReader (indeed a LineNumberReader) from the File specified in the constructor of CharsetToolkit using the charset discovered or the default charset if an 8-bit Charset is encountered.

boolean hasUTF8Bom()

void setDefaultCharset(Charset defaultCharset)

Defines the default Charset used in case the buffer represents an 8-bit Charset.

void setEnforce8Bit(boolean enforce)

If US-ASCII is recognized, enforce to return the default encoding, rather than US-ASCII.

 
Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Constructor Detail

CharsetToolkit

public CharsetToolkit(File file)
Constructor of the CharsetToolkit utility class.
Parameters:
file - of which we want to know the encoding.


 
Method Detail

getAvailableCharsets

public static Charset[] getAvailableCharsets()


getCharset

public Charset getCharset()


getDefaultCharset

public Charset getDefaultCharset()
Retrieves the default Charset


getDefaultSystemCharset

public static Charset getDefaultSystemCharset()


getEnforce8Bit

public boolean getEnforce8Bit()
Gets the enforce8Bit flag, in case we do not want to ever get a US-ASCII encoding.
Returns:
a boolean representing the flag of use of US-ASCII.


getReader

public BufferedReader getReader()


hasUTF16BEBom

public boolean hasUTF16BEBom()


hasUTF16LEBom

public boolean hasUTF16LEBom()
Gets a BufferedReader (indeed a LineNumberReader) from the File specified in the constructor of CharsetToolkit using the charset discovered or the default charset if an 8-bit Charset is encountered.
throws:
FileNotFoundException if the file is not found.
Returns:
a BufferedReader


hasUTF8Bom

public boolean hasUTF8Bom()


setDefaultCharset

public void setDefaultCharset(Charset defaultCharset)
Defines the default Charset used in case the buffer represents an 8-bit Charset.
Parameters:
defaultCharset - the default Charset to be returned if an 8-bit Charset is encountered.


setEnforce8Bit

public void setEnforce8Bit(boolean enforce)
If US-ASCII is recognized, enforce to return the default encoding, rather than US-ASCII. It might be a file without any special character in the range 128-255, but that may be or become a file encoded with the default charset rather than US-ASCII.
Parameters:
enforce - a boolean specifying the use or not of US-ASCII.


 

Copyright © 2003-2013 The Codehaus. All rights reserved.