Class XmlStreamReader
java.lang.Object
java.io.Reader
net.adoptopenjdk.icedteaweb.xmlparser.XmlStreamReader
- All Implemented Interfaces:
Closeable, AutoCloseable, Readable
This Reader will examine the wrapped input stream according to
appendix F of the XML specification in order to guess the encoding
of the XML content in the stream.
If the encoding cannot be guessed the reader falls back to the
XML - Appendix F
defaultEncoding.
The following aspects of the input stream are examined in the order below
- Byte order mark (BOM)
- First 20 byes to see if content starts with "<?xml"
- XML encoding declaration
- Default encoding
XML - Appendix F
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final byte[]private static final byte[](package private) static final byte[]private static final byte[]private static final intprivate final InputStreamReaderprivate static final int(package private) static final byte[]private static final byte[](package private) static final byte[]private static final byte[](package private) static final byte[]private static final byte[](package private) static final String(package private) static final byte[]private static final byte[](package private) static final String(package private) static final byte[]private static final byte[] -
Constructor Summary
ConstructorsConstructorDescriptionConstructor UTF-8 default encoding.XmlStreamReader(InputStream in, Charset defaultEncoding) Constructor with passed in default encoding. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Reads the first 4 bytes of the input stream and detects any unicode byte order mark (BOM).Reads the first 20 bytes of the input stream and detects if the bytes resemble the characters"<?xml"in a unicode encoding.Returns the name of the character encoding being used by this stream.intread()intread(char[] cbuf, int off, int len) readOutOfXmlTag(PushbackInputStream pin, Charset guessedEncoding) Tries to read the encoding from the encoding tag in an XML (invalid input: '<'?xml version="1.0" encoding="UTF-8"?>).booleanready()private booleanstartsWith(byte[] bytes, int len, byte[] prefix) Detects if a given byte array starts with a prefix of bytes.Methods inherited from class Reader
mark, markSupported, nullReader, of, read, read, readAllAsString, readAllLines, reset, skip, transferTo
-
Field Details
-
UTF8_BOM
static final byte[] UTF8_BOM -
UTF16LE_BOM
static final byte[] UTF16LE_BOM -
UTF16BE_BOM
static final byte[] UTF16BE_BOM -
UTF32LE_BOM
static final byte[] UTF32LE_BOM -
UTF32BE_BOM
static final byte[] UTF32BE_BOM -
BROKEN_UTF32LE_BOM
static final byte[] BROKEN_UTF32LE_BOM -
BROKEN_UTF32BE_BOM
static final byte[] BROKEN_UTF32BE_BOM -
UTF8_FIRST_CHARS
private static final byte[] UTF8_FIRST_CHARS -
UTF16LE_FIRST_CHARS
private static final byte[] UTF16LE_FIRST_CHARS -
UTF16BE_FIRST_CHARS
private static final byte[] UTF16BE_FIRST_CHARS -
UTF32LE_FIRST_CHARS
private static final byte[] UTF32LE_FIRST_CHARS -
UTF32BE_FIRST_CHARS
private static final byte[] UTF32BE_FIRST_CHARS -
BROKEN_UTF32LE_FIRST_CHARS
private static final byte[] BROKEN_UTF32LE_FIRST_CHARS -
BROKEN_UTF32BE_FIRST_CHARS
private static final byte[] BROKEN_UTF32BE_FIRST_CHARS -
UTF32LE_NAME
- See Also:
-
UTF32BE_NAME
- See Also:
-
MAX_CHARS
private static final int MAX_CHARS- See Also:
-
BUFFER_SIZE
private static final int BUFFER_SIZE- See Also:
-
delegate
-
-
Constructor Details
-
XmlStreamReader
Constructor UTF-8 default encoding. This constructor is equivalent to calling:
new XmlStreamReader(in, StandardCharsets.UTF_8)- Parameters:
in- an input stream with XML content.- Throws:
IOException
-
XmlStreamReader
Constructor with passed in default encoding.- Parameters:
in- an input stream with XML content.defaultEncoding- the encoding to use if no encoding can be derived from the content of the stream- Throws:
IOException
-
-
Method Details
-
detectFromBom
Reads the first 4 bytes of the input stream and detects any unicode byte order mark (BOM). If a mark is detected the correspondingCharsetis returned. Any detected BOM is removed from the stream leaving only the content without the BOM for further processing.- Parameters:
pin- the input stream- Returns:
- the detected character set or empty if no BOM was found
- Throws:
IOException- if a unsupported BOM was detected.
-
detectFromXml
Reads the first 20 bytes of the input stream and detects if the bytes resemble the characters"<?xml"in a unicode encoding. If a match is found the correspondingCharsetis returned. The content in the stream is left unchanged for further processing.- Parameters:
pin- the input stream- Returns:
- the detected character set or empty if no
"<?xml"was found - Throws:
IOException- if a unsupported BOM was detected.
-
startsWith
private boolean startsWith(byte[] bytes, int len, byte[] prefix) Detects if a given byte array starts with a prefix of bytes.- Parameters:
bytes- the bytes to search for the prefixlen- the number of bytes inbyteswhich are filledprefix- the prefix to search for- Returns:
- true iff
lenis greater or equal toprefix.lengthand theprefixis a prefix ofbytes
-
readOutOfXmlTag
private Optional<Charset> readOutOfXmlTag(PushbackInputStream pin, Charset guessedEncoding) throws IOException Tries to read the encoding from the encoding tag in an XML (invalid input: '<'?xml version="1.0" encoding="UTF-8"?>). Reads the firstBUFFER_SIZEbytes and converts it to a string using theguessedEncoding. The resulting string is searched for an XML encoding tag. If found the corresponding character set is returned. The content in the stream is left unchanged for further processing.- Parameters:
pin- the input streamguessedEncoding- the encoding guessed by analyzing BOM and the first 20 bytes.- Returns:
- the encoding found in the XML tag or empty
- Throws:
IOException- if reading from the stream failed
-
getEncoding
Returns the name of the character encoding being used by this stream.If the encoding has an historical name then that name is returned; otherwise the encoding's canonical name is returned.
If this instance was created with the
XmlStreamReader(InputStream, Charset)constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method will returnnullif the stream has been closed.- Returns:
- The historical name of this encoding, or
nullif the stream has been closed - See Also:
-
ready
- Overrides:
readyin classReader- Throws:
IOException
-
read
- Overrides:
readin classReader- Throws:
IOException
-
read
- Specified by:
readin classReader- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classReader- Throws:
IOException
-