Package org.apache.velocity.io
Class UnicodeInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.velocity.io.UnicodeInputStream
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class UnicodeInputStream extends InputStream
This is an input stream that is unicode BOM aware. This allows you to e.g. read Windows Notepad Unicode files as Velocity templates. It allows you to check the actual encoding of a file by callinggetEncodingFromStream()on the input stream reader. This class is not thread safe! When more than one thread wants to use an instance of UnicodeInputStream, the caller must provide synchronization.- Since:
- 1.5
- Version:
- $Id$
- Author:
- Aki Nieminen, Henning P. Schmiedehausen
-
-
Field Summary
Fields Modifier and Type Field Description static org.apache.velocity.io.UnicodeInputStream.UnicodeBOMUTF16BE_BOMBOM Marker for UTF 16, big endian.static org.apache.velocity.io.UnicodeInputStream.UnicodeBOMUTF16LE_BOMBOM Marker for UTF 16, little endian.static org.apache.velocity.io.UnicodeInputStream.UnicodeBOMUTF32BE_BOMBOM Marker for UTF 32, big endian.static org.apache.velocity.io.UnicodeInputStream.UnicodeBOMUTF32LE_BOMBOM Marker for UTF 32, little endian.static org.apache.velocity.io.UnicodeInputStream.UnicodeBOMUTF8_BOMBOM Marker for UTF 8.
-
Constructor Summary
Constructors Constructor Description UnicodeInputStream(InputStream inputStream)Creates a new UnicodeInputStream object.UnicodeInputStream(InputStream inputStream, boolean skipBOM)Creates a new UnicodeInputStream object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()voidclose()StringgetEncodingFromStream()Read encoding based on BOM.booleanisSkipBOM()Returns true if the input stream discards the BOM.voidmark(int readlimit)booleanmarkSupported()intread()intread(byte[] b)intread(byte[] b, int off, int len)protected StringreadEncoding()This method gets the encoding from the stream contents if a BOM exists.voidreset()static booleansameEncoding(String left, String right)Helper function to compare encodingslongskip(long n)
-
-
-
Field Detail
-
UTF8_BOM
public static final org.apache.velocity.io.UnicodeInputStream.UnicodeBOM UTF8_BOM
BOM Marker for UTF 8. See http://www.unicode.org/unicode/faq/utf_bom.html
-
UTF16LE_BOM
public static final org.apache.velocity.io.UnicodeInputStream.UnicodeBOM UTF16LE_BOM
BOM Marker for UTF 16, little endian. See http://www.unicode.org/unicode/faq/utf_bom.html
-
UTF16BE_BOM
public static final org.apache.velocity.io.UnicodeInputStream.UnicodeBOM UTF16BE_BOM
BOM Marker for UTF 16, big endian. See http://www.unicode.org/unicode/faq/utf_bom.html
-
UTF32LE_BOM
public static final org.apache.velocity.io.UnicodeInputStream.UnicodeBOM UTF32LE_BOM
BOM Marker for UTF 32, little endian. See http://www.unicode.org/unicode/faq/utf_bom.html
-
UTF32BE_BOM
public static final org.apache.velocity.io.UnicodeInputStream.UnicodeBOM UTF32BE_BOM
BOM Marker for UTF 32, big endian. See http://www.unicode.org/unicode/faq/utf_bom.html
-
-
Constructor Detail
-
UnicodeInputStream
public UnicodeInputStream(InputStream inputStream) throws IllegalStateException, IOException
Creates a new UnicodeInputStream object. Skips a BOM which defines the file encoding.- Parameters:
inputStream- The input stream to use for reading.- Throws:
IllegalStateExceptionIOException
-
UnicodeInputStream
public UnicodeInputStream(InputStream inputStream, boolean skipBOM) throws IllegalStateException, IOException
Creates a new UnicodeInputStream object.- Parameters:
inputStream- The input stream to use for reading.skipBOM- If this is set to true, a BOM read from the stream is discarded. This parameter should normally be true.- Throws:
IllegalStateExceptionIOException
-
-
Method Detail
-
isSkipBOM
public boolean isSkipBOM()
Returns true if the input stream discards the BOM.- Returns:
- True if the input stream discards the BOM.
-
getEncodingFromStream
public String getEncodingFromStream()
Read encoding based on BOM.- Returns:
- The encoding based on the BOM.
- Throws:
IllegalStateException- When a problem reading the BOM occured.
-
readEncoding
protected String readEncoding() throws IOException
This method gets the encoding from the stream contents if a BOM exists. If no BOM exists, the encoding is undefined.- Returns:
- The encoding of this streams contents as decided by the BOM or null if no BOM was found.
- Throws:
IOException
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- See Also:
InputStream.close()
-
available
public int available() throws IOException- Overrides:
availablein classInputStream- Throws:
IOException- See Also:
InputStream.available()
-
mark
public void mark(int readlimit)
- Overrides:
markin classInputStream- Parameters:
readlimit-- See Also:
InputStream.mark(int)
-
markSupported
public boolean markSupported()
- Overrides:
markSupportedin classInputStream- Returns:
- mark supported
- See Also:
InputStream.markSupported()
-
read
public int read() throws IOException- Specified by:
readin classInputStream- Returns:
- read char
- Throws:
IOException- See Also:
InputStream.read()
-
read
public int read(byte[] b) throws IOException- Overrides:
readin classInputStream- Parameters:
b- buffer- Returns:
- read chars count
- Throws:
IOException- See Also:
InputStream.read(byte[])
-
read
public int read(byte[] b, int off, int len) throws IOException- Overrides:
readin classInputStream- Parameters:
b- bufferoff- offsetlen- length- Returns:
- reac char
- Throws:
IOException- See Also:
InputStream.read(byte[], int, int)
-
reset
public void reset() throws IOException- Overrides:
resetin classInputStream- Throws:
IOException- See Also:
InputStream.reset()
-
skip
public long skip(long n) throws IOException- Overrides:
skipin classInputStream- Parameters:
n-- Returns:
- skipped count
- Throws:
IOException- See Also:
InputStream.skip(long)
-
-