Package org.apache.velocity.io
Class UnicodeInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.velocity.io.UnicodeInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class UnicodeInputStream extends java.io.InputStreamThis 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: UnicodeInputStream.java 685685 2008-08-13 21:43:27Z nbubna $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classUnicodeInputStream.UnicodeBOMHelper class to bundle encoding and BOM marker.
-
Field Summary
Fields Modifier and Type Field Description private byte[]bufBuffer for BOM readingprivate java.lang.StringencodingThe stream encoding as read from the BOM or null.private java.io.PushbackInputStreaminputStreamprivate static intMAX_BOM_SIZEThe maximum amount of bytes to read for a BOMprivate intposBuffer pointer.private booleanskipBOMTrue if the BOM itself should be skipped and not read.static UnicodeInputStream.UnicodeBOMUTF16BE_BOMBOM Marker for UTF 16, big endian.static UnicodeInputStream.UnicodeBOMUTF16LE_BOMBOM Marker for UTF 16, little endian.static UnicodeInputStream.UnicodeBOMUTF32BE_BOMBOM Marker for UTF 32, big endian.static UnicodeInputStream.UnicodeBOMUTF32LE_BOMBOM Marker for UTF 32, little endian.static UnicodeInputStream.UnicodeBOMUTF8_BOMBOM Marker for UTF 8.
-
Constructor Summary
Constructors Constructor Description UnicodeInputStream(java.io.InputStream inputStream)Creates a new UnicodeInputStream object.UnicodeInputStream(java.io.InputStream inputStream, boolean skipBOM)Creates a new UnicodeInputStream object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()voidclose()java.lang.StringgetEncodingFromStream()Read encoding based on BOM.booleanisSkipBOM()Returns true if the input stream discards the BOM.voidmark(int readlimit)booleanmarkSupported()private UnicodeInputStream.UnicodeBOMmatch(UnicodeInputStream.UnicodeBOM matchEncoding, UnicodeInputStream.UnicodeBOM noMatchEncoding)private voidpushback(UnicodeInputStream.UnicodeBOM matchBOM)intread()intread(byte[] b)intread(byte[] b, int off, int len)private booleanreadByte()protected java.lang.StringreadEncoding()This method gets the encoding from the stream contents if a BOM exists.voidreset()longskip(long n)
-
-
-
Field Detail
-
UTF8_BOM
public static final UnicodeInputStream.UnicodeBOM UTF8_BOM
BOM Marker for UTF 8. See http://www.unicode.org/unicode/faq/utf_bom.html
-
UTF16LE_BOM
public static final 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 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 UnicodeInputStream.UnicodeBOM UTF32LE_BOM
BOM Marker for UTF 32, little endian. See http://www.unicode.org/unicode/faq/utf_bom.html TODO: Does Java actually support this?
-
UTF32BE_BOM
public static final UnicodeInputStream.UnicodeBOM UTF32BE_BOM
BOM Marker for UTF 32, big endian. See http://www.unicode.org/unicode/faq/utf_bom.html TODO: Does Java actually support this?
-
MAX_BOM_SIZE
private static final int MAX_BOM_SIZE
The maximum amount of bytes to read for a BOM- See Also:
- Constant Field Values
-
buf
private byte[] buf
Buffer for BOM reading
-
pos
private int pos
Buffer pointer.
-
encoding
private final java.lang.String encoding
The stream encoding as read from the BOM or null.
-
skipBOM
private final boolean skipBOM
True if the BOM itself should be skipped and not read.
-
inputStream
private final java.io.PushbackInputStream inputStream
-
-
Constructor Detail
-
UnicodeInputStream
public UnicodeInputStream(java.io.InputStream inputStream) throws java.lang.IllegalStateException, java.io.IOExceptionCreates a new UnicodeInputStream object. Skips a BOM which defines the file encoding.- Parameters:
inputStream- The input stream to use for reading.- Throws:
java.lang.IllegalStateExceptionjava.io.IOException
-
UnicodeInputStream
public UnicodeInputStream(java.io.InputStream inputStream, boolean skipBOM) throws java.lang.IllegalStateException, java.io.IOExceptionCreates 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:
java.lang.IllegalStateExceptionjava.io.IOException
-
-
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 java.lang.String getEncodingFromStream()
Read encoding based on BOM.- Returns:
- The encoding based on the BOM.
- Throws:
java.lang.IllegalStateException- When a problem reading the BOM occured.
-
readEncoding
protected java.lang.String readEncoding() throws java.io.IOExceptionThis 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:
java.io.IOException
-
match
private final UnicodeInputStream.UnicodeBOM match(UnicodeInputStream.UnicodeBOM matchEncoding, UnicodeInputStream.UnicodeBOM noMatchEncoding) throws java.io.IOException
- Throws:
java.io.IOException
-
readByte
private final boolean readByte() throws java.io.IOException- Throws:
java.io.IOException
-
pushback
private final void pushback(UnicodeInputStream.UnicodeBOM matchBOM) throws java.io.IOException
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException- See Also:
InputStream.close()
-
available
public int available() throws java.io.IOException- Overrides:
availablein classjava.io.InputStream- Throws:
java.io.IOException- See Also:
InputStream.available()
-
mark
public void mark(int readlimit)
- Overrides:
markin classjava.io.InputStream- See Also:
InputStream.mark(int)
-
markSupported
public boolean markSupported()
- Overrides:
markSupportedin classjava.io.InputStream- See Also:
InputStream.markSupported()
-
read
public int read() throws java.io.IOException- Specified by:
readin classjava.io.InputStream- Throws:
java.io.IOException- See Also:
InputStream.read()
-
read
public int read(byte[] b) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException- See Also:
InputStream.read(byte[])
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException- See Also:
InputStream.read(byte[], int, int)
-
reset
public void reset() throws java.io.IOException- Overrides:
resetin classjava.io.InputStream- Throws:
java.io.IOException- See Also:
InputStream.reset()
-
skip
public long skip(long n) throws java.io.IOException- Overrides:
skipin classjava.io.InputStream- Throws:
java.io.IOException- See Also:
InputStream.skip(long)
-
-