Class XmlStreamReader

java.lang.Object
java.io.Reader
net.adoptopenjdk.icedteaweb.xmlparser.XmlStreamReader
All Implemented Interfaces:
Closeable, AutoCloseable, Readable

public class XmlStreamReader extends Reader
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 defaultEncoding.

The following aspects of the input stream are examined in the order below

  1. Byte order mark (BOM)
  2. First 20 byes to see if content starts with "<?xml"
  3. XML encoding declaration
  4. Default encoding

For details see:
XML - Appendix F

  • 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

      static final String UTF32LE_NAME
      See Also:
    • UTF32BE_NAME

      static final String 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

      private final InputStreamReader delegate
  • Constructor Details

    • XmlStreamReader

      public XmlStreamReader(InputStream in) throws IOException
      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

      public XmlStreamReader(InputStream in, Charset defaultEncoding) throws IOException
      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

      private Optional<Charset> detectFromBom(PushbackInputStream pin) throws IOException
      Reads the first 4 bytes of the input stream and detects any unicode byte order mark (BOM). If a mark is detected the corresponding Charset is 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

      private Optional<Charset> detectFromXml(PushbackInputStream pin) throws IOException
      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 corresponding Charset is 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 prefix
      len - the number of bytes in bytes which are filled
      prefix - the prefix to search for
      Returns:
      true iff len is greater or equal to prefix.length and the prefix is a prefix of bytes
    • 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 first BUFFER_SIZE bytes and converts it to a string using the guessedEncoding. 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 stream
      guessedEncoding - 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

      public 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 return null if the stream has been closed.

      Returns:
      The historical name of this encoding, or null if the stream has been closed
      See Also:
    • ready

      public boolean ready() throws IOException
      Overrides:
      ready in class Reader
      Throws:
      IOException
    • read

      public int read() throws IOException
      Overrides:
      read in class Reader
      Throws:
      IOException
    • read

      public int read(char[] cbuf, int off, int len) throws IOException
      Specified by:
      read in class Reader
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Reader
      Throws:
      IOException