Class HTMLReader

All Implemented Interfaces:
Closeable, AutoCloseable, Readable

public class HTMLReader extends BufferedFileReader implements AutoCloseable
This class automatically detects encoding of an inner HTML file and constructs a Reader with appropriate encoding. Detecting of encoding is done by reading a possible <META http-equiv="content-type" content="text/html; charset=..."> and a value from XML header (in case there is one) <?xml version="1.0" encoding="..."?>. If encoding isn't specified, or it is not supported by Java platform, the file is opened in encoding passed to constructor or default system encoding (ISO-8859-2 in USA, Windows-1251 on my OS).
  • Constructor Details

    • HTMLReader

      public HTMLReader(String fileName, String defaultEncoding) throws FileNotFoundException
      Creates a new instance of HTMLReader. If encoding cannot be detected, falls back to supplied encoding, or (if supplied null, or supplied encoding is not supported by JVM) falls back to default encoding of Operating System.
      Parameters:
      fileName - The file to read.
      defaultEncoding - The encoding to use if we can't autodetect.
      Throws:
      FileNotFoundException
  • Method Details

    • read

      public int read(char[] cbuf, int off, int len) throws IOException
      Reads characters into a portion of an array. If this is the first time the method is invoked, it ensures that the Byte Order Mark (BOM) is handled correctly by resetting the stream if a BOM is not present. Subsequent reads proceed as normal.
      Overrides:
      read in class BufferedReader
      Parameters:
      cbuf - the destination buffer to hold the characters read from the stream
      off - the start offset in the buffer where characters are written
      len - the maximum number of characters to read
      Returns:
      the number of characters read into the buffer or -1 if the end of the stream has been reached
      Throws:
      IOException - if an I/O error occurs