Package org.apache.velocity.io
Class UnicodeInputStream
java.lang.Object
java.io.InputStream
org.apache.velocity.io.UnicodeInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
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 calling
getEncodingFromStream()
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$
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
Helper class to bundle encoding and BOM marker. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate byte[]
Buffer for BOM readingprivate final String
The stream encoding as read from the BOM or null.private final PushbackInputStream
private static final int
The maximum amount of bytes to read for a BOMprivate int
Buffer pointer.private final boolean
True if the BOM itself should be skipped and not read.static final UnicodeInputStream.UnicodeBOM
BOM Marker for UTF 16, big endian.static final UnicodeInputStream.UnicodeBOM
BOM Marker for UTF 16, little endian.static final UnicodeInputStream.UnicodeBOM
BOM Marker for UTF 32, big endian.static final UnicodeInputStream.UnicodeBOM
BOM Marker for UTF 32, little endian.static final UnicodeInputStream.UnicodeBOM
BOM Marker for UTF 8. -
Constructor Summary
ConstructorsConstructorDescriptionUnicodeInputStream
(InputStream inputStream) Creates a new UnicodeInputStream object.UnicodeInputStream
(InputStream inputStream, boolean skipBOM) Creates a new UnicodeInputStream object. -
Method Summary
Modifier and TypeMethodDescriptionint
void
close()
Read encoding based on BOM.boolean
Returns true if the input stream discards the BOM.void
mark
(int readlimit) boolean
private UnicodeInputStream.UnicodeBOM
match
(UnicodeInputStream.UnicodeBOM matchEncoding, UnicodeInputStream.UnicodeBOM noMatchEncoding) private void
pushback
(UnicodeInputStream.UnicodeBOM matchBOM) int
read()
int
read
(byte[] b) int
read
(byte[] b, int off, int len) private boolean
readByte()
protected String
This method gets the encoding from the stream contents if a BOM exists.void
reset()
static boolean
sameEncoding
(String left, String right) Helper function to compare encodingslong
skip
(long n) Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Field Details
-
UTF8_BOM
BOM Marker for UTF 8. See http://www.unicode.org/unicode/faq/utf_bom.html -
UTF16LE_BOM
BOM Marker for UTF 16, little endian. See http://www.unicode.org/unicode/faq/utf_bom.html -
UTF16BE_BOM
BOM Marker for UTF 16, big endian. See http://www.unicode.org/unicode/faq/utf_bom.html -
UTF32LE_BOM
BOM Marker for UTF 32, little endian. See http://www.unicode.org/unicode/faq/utf_bom.html -
UTF32BE_BOM
BOM Marker for UTF 32, big endian. See http://www.unicode.org/unicode/faq/utf_bom.html -
MAX_BOM_SIZE
private static final int MAX_BOM_SIZEThe maximum amount of bytes to read for a BOM- See Also:
-
buf
private byte[] bufBuffer for BOM reading -
pos
private int posBuffer pointer. -
encoding
The stream encoding as read from the BOM or null. -
skipBOM
private final boolean skipBOMTrue if the BOM itself should be skipped and not read. -
inputStream
-
-
Constructor Details
-
UnicodeInputStream
Creates a new UnicodeInputStream object. Skips a BOM which defines the file encoding.- Parameters:
inputStream
- The input stream to use for reading.- Throws:
IllegalStateException
IOException
-
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:
IllegalStateException
IOException
-
-
Method Details
-
isSkipBOM
public boolean isSkipBOM()Returns true if the input stream discards the BOM.- Returns:
- True if the input stream discards the BOM.
-
getEncodingFromStream
Read encoding based on BOM.- Returns:
- The encoding based on the BOM.
- Throws:
IllegalStateException
- When a problem reading the BOM occured.
-
readEncoding
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
-
match
private UnicodeInputStream.UnicodeBOM match(UnicodeInputStream.UnicodeBOM matchEncoding, UnicodeInputStream.UnicodeBOM noMatchEncoding) throws IOException - Throws:
IOException
-
readByte
- Throws:
IOException
-
pushback
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
- See Also:
-
available
- Overrides:
available
in classInputStream
- Throws:
IOException
- See Also:
-
mark
public void mark(int readlimit) - Overrides:
mark
in classInputStream
- Parameters:
readlimit
-- See Also:
-
markSupported
public boolean markSupported()- Overrides:
markSupported
in classInputStream
- Returns:
- mark supported
- See Also:
-
read
- Specified by:
read
in classInputStream
- Returns:
- read char
- Throws:
IOException
- See Also:
-
read
- Overrides:
read
in classInputStream
- Parameters:
b
- buffer- Returns:
- read chars count
- Throws:
IOException
- See Also:
-
read
- Overrides:
read
in classInputStream
- Parameters:
b
- bufferoff
- offsetlen
- length- Returns:
- reac char
- Throws:
IOException
- See Also:
-
reset
- Overrides:
reset
in classInputStream
- Throws:
IOException
- See Also:
-
skip
- Overrides:
skip
in classInputStream
- Parameters:
n
-- Returns:
- skipped count
- Throws:
IOException
- See Also:
-
sameEncoding
Helper function to compare encodings- Parameters:
left
-right
-- Returns:
- true for same encoding
-