Package org.dellroad.stuff.io
Class BOMReader
java.lang.Object
java.io.Reader
org.dellroad.stuff.io.BOMReader
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Readable
A
Reader
that wraps an InputStream
, detects and strips the byte order mark, and then converts
bytes into characters accordingly.
If no byte order mark is found, the input is implicitly assumed to be in some default character encoding.
The default character encoding and the action to take on malformed input are configurable.
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionBOMReader
(InputStream input) Constructor.BOMReader
(InputStream input, CodingErrorAction errorAction, Charset defaultCharset) Primary constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
getBOM()
Get theBOM
found at the beginning of the input.Get the actual character encoding used.void
mark
(int limit) int
read()
int
read
(char[] cbuf) int
read
(char[] cbuf, int off, int len) int
read
(CharBuffer target) boolean
ready()
void
reset()
long
skip
(long num) Methods inherited from class java.io.Reader
markSupported, nullReader, transferTo
-
Constructor Details
-
BOMReader
Constructor.Equivalent to
BOMReader(input, CodingErrorAction.REPORT, Charset.defaultCharset())
.- Parameters:
input
- data input- Throws:
IllegalArgumentException
- ifinput
is null
-
BOMReader
Primary constructor.The
errorAction
configures the behavior when malformed input is encountered; in the case ofCodingErrorAction.REPORT
, aMalformedInputException
exception is thrown.- Parameters:
input
- data inputerrorAction
- what to do about malformed inputdefaultCharset
- character encoding to use if no BOM is found- Throws:
IllegalArgumentException
- if any parameter is null
-
-
Method Details
-
getBOM
Get theBOM
found at the beginning of the input.If no input has been read yet, this method will trigger the reading of the first few bytes.
- Returns:
- the BOM that was detected, or null if no supported BOM was found
- Throws:
IOException
-
getCharset
Get the actual character encoding used.If no input has been read yet, this method will trigger the reading of the first few bytes.
- Returns:
- the character encoding chosen to decode the data, never null
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classReader
- Throws:
IOException
-
mark
- Overrides:
mark
in classReader
- Throws:
IOException
-
read
- Overrides:
read
in classReader
- Throws:
IOException
-
read
- Overrides:
read
in classReader
- Throws:
IOException
-
read
- Specified by:
read
in classReader
- Throws:
IOException
-
read
- Specified by:
read
in interfaceReadable
- Overrides:
read
in classReader
- Throws:
IOException
-
ready
- Overrides:
ready
in classReader
- Throws:
IOException
-
reset
- Overrides:
reset
in classReader
- Throws:
IOException
-
skip
- Overrides:
skip
in classReader
- Throws:
IOException
-