Package net.sf.okapi.common
Class BOMAwareInputStream
- java.lang.Object
-
- java.io.InputStream
-
- net.sf.okapi.common.BOMAwareInputStream
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class BOMAwareInputStream extends InputStream implements AutoCloseable
Helper class to detect and skip BOM in InputStream, including for UTF-8.
-
-
Constructor Summary
Constructors Constructor Description BOMAwareInputStream(InputStream input, String defaultEncoding)Creates a new BOMAwareInputStream object for a given InputStream and a given default encoding.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanautoDtected()Indicates if the guessed encoding was auto-detected.voidclose()WARNING: The inputInputStreamwill also be closed when this method is called.StringdetectEncoding()Tries to detect the presence of a Byte-Order-Mark.intgetBOMSize()Gets the number of bytes used by the Byte-Order-mark in this document.StringgetDetectedEncoding()Gets the encoding that was guessed.booleanhasUTF8BOM()Indicates if the guessed encoding is UTF-8 and this file has a BOM.intread()-
Methods inherited from class java.io.InputStream
available, mark, markSupported, nullInputStream, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
-
-
-
-
Constructor Detail
-
BOMAwareInputStream
public BOMAwareInputStream(InputStream input, String defaultEncoding)
Creates a new BOMAwareInputStream object for a given InputStream and a given default encoding.- Parameters:
input- The input stream to use.defaultEncoding- The default encoding to use if none can be auto-detected.
-
-
Method Detail
-
detectEncoding
public String detectEncoding() throws IOException
Tries to detect the presence of a Byte-Order-Mark.- Returns:
- The encoding guessed after the try. If nothing has been found, the default encoding is returned.
- Throws:
IOException- if an I/O error occurs.
-
getDetectedEncoding
public String getDetectedEncoding()
Gets the encoding that was guessed. It can be the default encoding.- Returns:
- the guessed encoding.
-
autoDtected
public boolean autoDtected()
Indicates if the guessed encoding was auto-detected. If not it is the default encoding that was provided.- Returns:
- True if the guessed encoding was auto-detected, false if not.
-
getBOMSize
public int getBOMSize()
Gets the number of bytes used by the Byte-Order-mark in this document.- Returns:
- The byte size of the BOM in this document.
-
hasUTF8BOM
public boolean hasUTF8BOM()
Indicates if the guessed encoding is UTF-8 and this file has a BOM.- Returns:
- True if the guessed encoding is UTF-8 and this file has a BOM, false otherwise.
-
read
public int read() throws IOException- Specified by:
readin classInputStream- Throws:
IOException
-
close
public void close() throws IOExceptionWARNING: The inputInputStreamwill also be closed when this method is called.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
-