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

  • 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

    • 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