public int read() throws IOException {
  if (_peek >= 0) {
    int peek=_peek;
    _peek=-1;
    return peek;
  }
  InputStream is=_is;
  int ch1=is.read();
  if (ch1 < 0x80) {
    return ch1;
  }
 else   if ((ch1 & 0xe0) == 0xc0) {
    int ch2=is.read();
    if (ch2 < 0) {
      return error("unexpected end of file in utf8 character");
    }
 else     if ((ch2 & 0xc0) != 0x80) {
      return error("utf-8 character conversion error for '{0}' because second byte is invalid at " + String.format("0x%02x 0x%02x",ch1,ch2));
    }
    return ((ch1 & 0x1f) << 6) + (ch2 & 0x3f);
  }
 else   if ((ch1 & 0xf0) == 0xe0) {
    int ch2=is.read();
    int ch3=is.read();
    if (ch2 < 0)     return error("unexpected end of file in utf8 character");
 else     if ((ch2 & 0xc0) != 0x80) {
      return error("illegal utf8 encoding at " + "\\x" + Integer.toHexString(ch1) + "\\x"+ Integer.toHexString(ch2)+ "\\x"+ Integer.toHexString(ch3));
    }
    if (ch3 < 0)     return error("unexpected end of file in utf8 character");
 else     if ((ch3 & 0xc0) != 0x80)     return error("illegal utf8 encoding at " + "\\x" + Integer.toHexString(ch1) + "\\x"+ Integer.toHexString(ch2)+ "\\x"+ Integer.toHexString(ch3));
    int ch=((ch1 & 0x1f) << 12) + ((ch2 & 0x3f) << 6) + (ch3 & 0x3f);
    if (ch == 0xfeff) {
      return read();
    }
 else     return ch;
  }
 else   if ((ch1 & 0xf0) == 0xf0) {
    int ch2=is.read();
    int ch3=is.read();
    int ch4=is.read();
    if (ch2 < 0)     return error("unexpected end of file in utf8 character");
 else     if ((ch2 & 0xc0) != 0x80)     return error("illegal utf8 encoding at 0x" + Integer.toHexString(ch2));
    if (ch3 < 0)     return error("unexpected end of file in utf8 character");
 else     if ((ch3 & 0xc0) != 0x80)     return error("illegal utf8 encoding at 0x" + Integer.toHexString(ch3));
    if (ch4 < 0)     return error("unexpected end of file in utf8 character");
 else     if ((ch4 & 0xc0) != 0x80)     return error("illegal utf8 encoding at 0x" + Integer.toHexString(ch4));
    int ch=(((ch1 & 0xf) << 18) + ((ch2 & 0x3f) << 12) + ((ch3 & 0x3f) << 6)+ ((ch4 & 0x3f)));
    _peek=0xdc00 + (ch & 0x3ff);
    return 0xd800 + ((ch - 0x10000) / 0x400);
  }
 else {
    return error("illegal utf8 encoding at (0x" + Integer.toHexString(ch1) + ")");
  }
}

public int read ( ) throws IOException { if ( _peek >= 0 ) { int peek = _peek ; _peek = - 1 ; return peek ; } InputStream is = _is ; int ch1 = is . read ( ) ; if ( ch1 < 0x80 ) { return ch1 ; } else if ( ( ch1 & 0xe0 ) == 0xc0 ) { int ch2 = is . read ( ) ; if ( ch2 < 0 ) { return error ( "unexpected end of file in utf8 character" ) ; } else if ( ( ch2 & 0xc0 ) != 0x80 ) { return error ( "utf-8 character conversion error for '{0}' because second byte is invalid at " + String . format ( "0x%02x 0x%02x" , ch1 , ch2 ) ) ; } return ( ( ch1 & 0x1f ) << 6 ) + ( ch2 & 0x3f ) ; } else if ( ( ch1 & 0xf0 ) == 0xe0 ) { int ch2 = is . read ( ) ; int ch3 = is . read ( ) ; if ( ch2 < 0 ) return error ( "unexpected end of file in utf8 character" ) ; else if ( ( ch2 & 0xc0 ) != 0x80 ) { return error ( "illegal utf8 encoding at " + "\\x" + Integer . toHexString ( ch1 ) + "\\x" + Integer . toHexString ( ch2 ) + "\\x" + Integer . toHexString ( ch3 ) ) ; } if ( ch3 < 0 ) return error ( "unexpected end of file in utf8 character" ) ; else if ( ( ch3 & 0xc0 ) != 0x80 ) return error ( "illegal utf8 encoding at " + "\\x" + Integer . toHexString ( ch1 ) + "\\x" + Integer . toHexString ( ch2 ) + "\\x" + Integer . toHexString ( ch3 ) ) ; int ch = ( ( ch1 & 0x1f ) << 12 ) + ( ( ch2 & 0x3f ) << 6 ) + ( ch3 & 0x3f ) ; if ( ch == 0xfeff ) { return read ( ) ; } else return ch ; } else if ( ( ch1 & 0xf0 ) == 0xf0 ) { int ch2 = is . read ( ) ; int ch3 = is . read ( ) ; int ch4 = is . read ( ) ; if ( ch2 < 0 ) return error ( "unexpected end of file in utf8 character" ) ; else if ( ( ch2 & 0xc0 ) != 0x80 ) return error ( "illegal utf8 encoding at 0x" + Integer . toHexString ( ch2 ) ) ; if ( ch3 < 0 ) return error ( "unexpected end of file in utf8 character" ) ; else if ( ( ch3 & 0xc0 ) != 0x80 ) return error ( "illegal utf8 encoding at 0x" + Integer . toHexString ( ch3 ) ) ; if ( ch4 < 0 ) return error ( "unexpected end of file in utf8 character" ) ; else if ( ( ch4 & 0xc0 ) != 0x80 ) return error ( "illegal utf8 encoding at 0x" + Integer . toHexString ( ch4 ) ) ; int ch = ( ( ( ch1 & 0xf ) << 18 ) + ( ( ch2 & 0x3f ) << 12 ) + ( ( ch3 & 0x3f ) << 6 ) + ( ( ch4 & 0x3f ) ) ) ; _peek = 0xdc00 + ( ch & 0x3ff ) ; return 0xd800 + ( ( ch - 0x10000 ) / 0x400 ) ; } else { return error ( "illegal utf8 encoding at (0x" + Integer . toHexString ( ch1 ) + ")" ) ; } }
reads into a character buffer using the correct encoding .
