Class XmlStreamReader
- java.lang.Object
-
- java.io.Reader
-
- net.adoptopenjdk.icedteaweb.xmlparser.XmlStreamReader
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.lang.Readable
public class XmlStreamReader extends java.io.ReaderThis 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 thedefaultEncoding. 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
Fields Modifier and Type Field Description (package private) static byte[]BROKEN_UTF32BE_BOMprivate static byte[]BROKEN_UTF32BE_FIRST_CHARS(package private) static byte[]BROKEN_UTF32LE_BOMprivate static byte[]BROKEN_UTF32LE_FIRST_CHARSprivate static intBUFFER_SIZEprivate java.io.InputStreamReaderdelegateprivate static intMAX_CHARS(package private) static byte[]UTF16BE_BOMprivate static byte[]UTF16BE_FIRST_CHARS(package private) static byte[]UTF16LE_BOMprivate static byte[]UTF16LE_FIRST_CHARS(package private) static byte[]UTF32BE_BOMprivate static byte[]UTF32BE_FIRST_CHARS(package private) static java.lang.StringUTF32BE_NAME(package private) static byte[]UTF32LE_BOMprivate static byte[]UTF32LE_FIRST_CHARS(package private) static java.lang.StringUTF32LE_NAME(package private) static byte[]UTF8_BOMprivate static byte[]UTF8_FIRST_CHARS
-
Constructor Summary
Constructors Constructor Description XmlStreamReader(java.io.InputStream in)Constructor UTF-8 default encoding.XmlStreamReader(java.io.InputStream in, java.nio.charset.Charset defaultEncoding)Constructor with passed in default encoding.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()private java.util.Optional<java.nio.charset.Charset>detectFromBom(java.io.PushbackInputStream pin)Reads the first 4 bytes of the input stream and detects any unicode byte order mark (BOM).private java.util.Optional<java.nio.charset.Charset>detectFromXml(java.io.PushbackInputStream pin)Reads the first 20 bytes of the input stream and detects if the bytes resemble the characters"<?xml"in a unicode encoding.java.lang.StringgetEncoding()Returns the name of the character encoding being used by this stream.intread()intread(char[] cbuf, int off, int len)private java.util.Optional<java.nio.charset.Charset>readOutOfXmlTag(java.io.PushbackInputStream pin, java.nio.charset.Charset guessedEncoding)Tries to read the encoding from the encoding tag in an XML ().booleanready()private booleanstartsWith(byte[] bytes, int len, byte[] prefix)Detects if a given byte array starts with a prefix of bytes.
-
-
-
Field Detail
-
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
static final java.lang.String UTF32LE_NAME
- See Also:
- Constant Field Values
-
UTF32BE_NAME
static final java.lang.String UTF32BE_NAME
- See Also:
- Constant Field Values
-
MAX_CHARS
private static final int MAX_CHARS
- See Also:
- Constant Field Values
-
BUFFER_SIZE
private static final int BUFFER_SIZE
- See Also:
- Constant Field Values
-
delegate
private final java.io.InputStreamReader delegate
-
-
Constructor Detail
-
XmlStreamReader
public XmlStreamReader(java.io.InputStream in) throws java.io.IOExceptionConstructor 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:
java.io.IOException
-
XmlStreamReader
public XmlStreamReader(java.io.InputStream in, java.nio.charset.Charset defaultEncoding) throws java.io.IOExceptionConstructor 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:
java.io.IOException
-
-
Method Detail
-
detectFromBom
private java.util.Optional<java.nio.charset.Charset> detectFromBom(java.io.PushbackInputStream pin) throws java.io.IOExceptionReads 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:
java.io.IOException- if a unsupported BOM was detected.
-
detectFromXml
private java.util.Optional<java.nio.charset.Charset> detectFromXml(java.io.PushbackInputStream pin) throws java.io.IOExceptionReads 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:
java.io.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 java.util.Optional<java.nio.charset.Charset> readOutOfXmlTag(java.io.PushbackInputStream pin, java.nio.charset.Charset guessedEncoding) throws java.io.IOExceptionTries to read the encoding from the encoding tag in an XML (). 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:
java.io.IOException- if reading from the stream failed
-
getEncoding
public java.lang.String 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:
Charset
-
ready
public boolean ready() throws java.io.IOException- Overrides:
readyin classjava.io.Reader- Throws:
java.io.IOException
-
read
public int read() throws java.io.IOException- Overrides:
readin classjava.io.Reader- Throws:
java.io.IOException
-
read
public int read(char[] cbuf, int off, int len) throws java.io.IOException- Specified by:
readin classjava.io.Reader- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein classjava.io.Reader- Throws:
java.io.IOException
-
-