public ParseByteStream(String systemId, InputStream istream, ParseLog log,
      String guessedEncoding, ParseHandler handler) throws ParseException, IOException {
    if (log == null) log = DEFAULT_LOG;

    //We need to be able to restart the stream if the declared encoding
    //is different than our guess, os buffer if necessary.  We also need
    //to be able to peek ahead at the first 4 bytes
    if (!istream.markSupported())
      throw new Error(
          "Precondition violation: the InputStream passed to ParseByteStream must support mark");
    istream.mark(MAXLOOKAHEAD); //mark at begining

    byte[] start = new byte[4];
    int n = istream.read(start);

    if (guessedEncoding == null) guessedEncoding = guessEncoding(systemId, start, n, log);

    try {

      //First try with guessed encoding
      istream.reset();
      InputStreamReader reader = new InputStreamReader(istream, fixEncoding(guessedEncoding));
      try {

        parseSource_ = new ParseCharStream(systemId, reader, log, guessedEncoding, handler);
        //}catch( CharConversionException e ){
      } catch (IOException e) {

        //This exception seems to be caused by reading euc-jp as utf-8
        String secondGuessEncoding = "euc-jp";
        log.note("Problem reading with assumed encoding of " + guessedEncoding
            + " so restarting with " + secondGuessEncoding, systemId, 1);
        istream.reset();
        try {
          reader = new InputStreamReader(istream, fixEncoding(secondGuessEncoding));
        } catch (UnsupportedEncodingException ee) {
          throw new ParseException(log, systemId, 1, '\0', secondGuessEncoding, "\""
              + secondGuessEncoding + "\" is not a supported encoding");
        }

        parseSource_ = new ParseCharStream(systemId, reader, log, null, handler);
      }
    } catch (EncodingMismatchException e) {
      //if that didn't work try declared encoding
      String declaredEncoding = e.getDeclaredEncoding();
      log.note("Encoding declaration of " + declaredEncoding + " is different that assumed "
          + guessedEncoding + " so restarting the parsing with the new encoding", systemId, 1);
      istream.reset();
      InputStreamReader reader;
      try {
        reader = new InputStreamReader(istream, fixEncoding(declaredEncoding));
      } catch (UnsupportedEncodingException ee) {
        throw new ParseException(log, systemId, 1, '\0', declaredEncoding, "\"" + declaredEncoding
            + "\" is not a supported encoding");
      }
      parseSource_ = new ParseCharStream(systemId, reader, log, null, handler);
    }
  }