Class UnicodeReader
- java.lang.Object
-
- java.io.Reader
-
- org.fife.io.UnicodeReader
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.lang.Readable
public class UnicodeReader extends java.io.ReaderA reader capable of identifying Unicode streams by their BOMs. This class will recognize the following encodings:- UTF-8
- UTF-16LE
- UTF-16BE
- UTF-32LE
- UTF-32BE
For optimum performance, it is recommended that you wrap all instances of
UnicodeReaderwith ajava.io.BufferedReader.This class is mostly ripped off from the workaround in the description of Java Bug 4508058.
- Version:
- 0.9
-
-
Field Summary
Fields Modifier and Type Field Description private static intBOM_SIZEThe size of a BOM.private java.lang.StringencodingThe encoding being used.private java.io.InputStreamReaderinternalInThe input stream from which we're really reading.
-
Constructor Summary
Constructors Constructor Description UnicodeReader(java.io.File file)This utility constructor is here because you will usually use aUnicodeReaderon files.UnicodeReader(java.io.File file, java.lang.String defaultEncoding)This utility constructor is here because you will usually use aUnicodeReaderon files.UnicodeReader(java.io.File file, java.nio.charset.Charset defaultCharset)This utility constructor is here because you will usually use aUnicodeReaderon files.UnicodeReader(java.io.InputStream in)Creates a reader using the encoding specified by the BOM in the file; if there is no recognized BOM, then a system default encoding is used.UnicodeReader(java.io.InputStream in, java.lang.String defaultEncoding)Creates a reader using the encoding specified by the BOM in the file; if there is no recognized BOM, thendefaultEncodingis used.UnicodeReader(java.io.InputStream in, java.nio.charset.Charset defaultCharset)Creates a reader using the encoding specified by the BOM in the file; if there is no recognized BOM, thendefaultEncodingis used.UnicodeReader(java.lang.String file)This utility constructor is here because you will usually use aUnicodeReaderon files.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this reader.java.lang.StringgetEncoding()Returns the encoding being used to read this input stream (i.e., the encoding of the file).protected voidinit(java.io.InputStream in, java.lang.String defaultEncoding)Read-ahead four bytes and check for BOM marks.intread(char[] cbuf, int off, int len)Read characters into a portion of an array.
-
-
-
Field Detail
-
internalIn
private java.io.InputStreamReader internalIn
The input stream from which we're really reading.
-
encoding
private java.lang.String encoding
The encoding being used. We keep our own instead of using the string returned byjava.io.InputStreamReadersince that class does not return user-friendly names.
-
BOM_SIZE
private static final int BOM_SIZE
The size of a BOM.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
UnicodeReader
public UnicodeReader(java.lang.String file) throws java.io.IOExceptionThis utility constructor is here because you will usually use aUnicodeReaderon files.Creates a reader using the encoding specified by the BOM in the file; if there is no recognized BOM, then a system default encoding is used.
- Parameters:
file- The file from which you want to read.- Throws:
java.io.IOException- If an error occurs when checking for/reading the BOM.java.lang.SecurityException- If a security manager exists and its checkRead method denies read access to the file.
-
UnicodeReader
public UnicodeReader(java.io.File file) throws java.io.IOExceptionThis utility constructor is here because you will usually use aUnicodeReaderon files.Creates a reader using the encoding specified by the BOM in the file; if there is no recognized BOM, then a system default encoding is used.
- Parameters:
file- The file from which you want to read.- Throws:
java.io.IOException- If an error occurs when checking for/reading the BOM.java.lang.SecurityException- If a security manager exists and its checkRead method denies read access to the file.
-
UnicodeReader
public UnicodeReader(java.io.File file, java.lang.String defaultEncoding) throws java.io.IOExceptionThis utility constructor is here because you will usually use aUnicodeReaderon files.Creates a reader using the encoding specified by the BOM in the file; if there is no recognized BOM, then a specified default encoding is used.
- Parameters:
file- The file from which you want to read.defaultEncoding- The encoding to use if no BOM is found. If this value isnull, a system default is used.- Throws:
java.io.IOException- If an error occurs when checking for/reading the BOM.java.lang.SecurityException- If a security manager exists and its checkRead method denies read access to the file.
-
UnicodeReader
public UnicodeReader(java.io.File file, java.nio.charset.Charset defaultCharset) throws java.io.IOExceptionThis utility constructor is here because you will usually use aUnicodeReaderon files.Creates a reader using the encoding specified by the BOM in the file; if there is no recognized BOM, then a specified default encoding is used.
- Parameters:
file- The file from which you want to read.defaultCharset- The encoding to use if no BOM is found. If this value isnull, a system default is used.- Throws:
java.io.IOException- If an error occurs when checking for/reading the BOM.java.lang.SecurityException- If a security manager exists and its checkRead method denies read access to the file.
-
UnicodeReader
public UnicodeReader(java.io.InputStream in) throws java.io.IOExceptionCreates a reader using the encoding specified by the BOM in the file; if there is no recognized BOM, then a system default encoding is used.- Parameters:
in- The input stream from which to read.- Throws:
java.io.IOException- If an error occurs when checking for/reading the BOM.
-
UnicodeReader
public UnicodeReader(java.io.InputStream in, java.lang.String defaultEncoding) throws java.io.IOExceptionCreates a reader using the encoding specified by the BOM in the file; if there is no recognized BOM, thendefaultEncodingis used.- Parameters:
in- The input stream from which to read.defaultEncoding- The encoding to use if no recognized BOM is found. If this value isnull, a system default is used.- Throws:
java.io.IOException- If an error occurs when checking for/reading the BOM.
-
UnicodeReader
public UnicodeReader(java.io.InputStream in, java.nio.charset.Charset defaultCharset) throws java.io.IOExceptionCreates a reader using the encoding specified by the BOM in the file; if there is no recognized BOM, thendefaultEncodingis used.- Parameters:
in- The input stream from which to read.defaultCharset- The encoding to use if no recognized BOM is found. If this value isnull, a system default is used.- Throws:
java.io.IOException- If an error occurs when checking for/reading the BOM.
-
-
Method Detail
-
close
public void close() throws java.io.IOExceptionCloses this reader.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein classjava.io.Reader- Throws:
java.io.IOException
-
getEncoding
public java.lang.String getEncoding()
Returns the encoding being used to read this input stream (i.e., the encoding of the file). If a BOM was recognized, then the specific Unicode type is returned; otherwise, either the default encoding passed into the constructor or the system default is returned.- Returns:
- The encoding of the stream.
-
init
protected void init(java.io.InputStream in, java.lang.String defaultEncoding) throws java.io.IOExceptionRead-ahead four bytes and check for BOM marks. Extra bytes are unread back to the stream, only BOM bytes are skipped.- Parameters:
defaultEncoding- The encoding to use if no BOM was recognized. If this value isnull, then a system default is used.- Throws:
java.io.IOException- If an error occurs when trying to read a BOM.
-
read
public int read(char[] cbuf, int off, int len) throws java.io.IOExceptionRead characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.- Specified by:
readin classjava.io.Reader- Parameters:
cbuf- The buffer into which to read.off- The offset at which to start storing characters.len- The maximum number of characters to read.- Returns:
- The number of characters read, or
-1if the end of the stream has been reached. - Throws:
java.io.IOException
-
-