Class XmlStreamReader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.lang.Readable

    public class XmlStreamReader
    extends java.io.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

    • 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
      void close()  
      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.String getEncoding()
      Returns the name of the character encoding being used by this stream.
      int read()  
      int read​(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 ().
      boolean ready()  
      private boolean startsWith​(byte[] bytes, int len, byte[] prefix)
      Detects if a given byte array starts with a prefix of bytes.
      • Methods inherited from class java.io.Reader

        mark, markSupported, nullReader, read, read, reset, skip, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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
      • delegate

        private final java.io.InputStreamReader delegate
    • Constructor Detail

      • XmlStreamReader

        public XmlStreamReader​(java.io.InputStream in)
                        throws java.io.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:
        java.io.IOException
      • XmlStreamReader

        public XmlStreamReader​(java.io.InputStream in,
                               java.nio.charset.Charset defaultEncoding)
                        throws java.io.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:
        java.io.IOException
    • Method Detail

      • detectFromBom

        private java.util.Optional<java.nio.charset.Charset> detectFromBom​(java.io.PushbackInputStream pin)
                                                                    throws java.io.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:
        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.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:
        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 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 java.util.Optional<java.nio.charset.Charset> readOutOfXmlTag​(java.io.PushbackInputStream pin,
                                                                             java.nio.charset.Charset guessedEncoding)
                                                                      throws java.io.IOException
        Tries to read the encoding from the encoding tag in an XML ().

        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:
        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 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:
        Charset
      • ready

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

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

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

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