package sun.awt.windows; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.io.BufferedInputStream; import java.io.InputStream; class HTMLCodec extends InputStream { public static final String ENCODING = "UTF-8"; public static final String VERSION = "Version:"; public static final String START_HTML = "StartHTML:"; public static final String END_HTML = "EndHTML:"; public static final String START_FRAGMENT = "StartFragment:"; public static final String END_FRAGMENT = "EndFragment:"; public static final String START_SELECTION = "StartSelection:"; public static final String END_SELECTION = "EndSelection:"; public static final String START_FRAGMENT_CMT = ""; public static final String END_FRAGMENT_CMT = ""; public static final String SOURCE_URL = "SourceURL:"; public static final String DEF_SOURCE_URL = "about:blank"; public static final String EOLN = "\r\n"; private static final String VERSION_NUM = "1.0"; private static final int PADDED_WIDTH = 10; private final BufferedInputStream bufferedStream; private boolean descriptionParsed; private boolean closed; public static final int BYTE_BUFFER_LEN = 8192; public static final int CHAR_BUFFER_LEN = 2730; private static final String FAILURE_MSG = "Unable to parse HTML description: "; private static final String INVALID_MSG = " invalid"; private long iHTMLStart; private long iHTMLEnd; private long iFragStart; private long iFragEnd; private long iSelStart; private long iSelEnd; private String stBaseURL; private String stVersion; private long iStartOffset; private long iEndOffset; private long iReadCount; private EHTMLReadMode readMode; private static String toPaddedString(int i, int capacity) { String str = "" + i; int length = str.length(); if (i >= 0 && length < capacity) { char[] array = new char[capacity - length]; Arrays.fill(array, '0'); StringBuffer sb = new StringBuffer(capacity); sb.append(array); sb.append(str); str = sb.toString(); return str; } return str; } public static byte[] convertToHTMLFormat(byte[] bytes) { String string = ""; String string2 = ""; String upperCase = new String(bytes).toUpperCase(); if (-1 == upperCase.indexOf("".length()); o.append("Version:"); o.append("1.0"); o.append("\r\n"); o.append("StartHTML:"); o.append(toPaddedString(n, 10)); o.append("\r\n"); o.append("EndHTML:"); o.append(toPaddedString(n4, 10)); o.append("\r\n"); o.append("StartFragment:"); o.append(toPaddedString(n2, 10)); o.append("\r\n"); o.append("EndFragment:"); o.append(toPaddedString(n3, 10)); o.append("\r\n"); o.append("SourceURL:"); o.append(str); o.append("\r\n"); o.append(string); byte[] bytes2 = null; byte[] bytes3 = null; try { bytes2 = o.toString().getBytes("UTF-8"); bytes3 = string2.getBytes("UTF-8"); } catch (UnsupportedEncodingException ex) {} byte[] array = new byte[bytes2.length + bytes.length + bytes3.length]; System.arraycopy(bytes2, 0, array, 0, bytes2.length); System.arraycopy(bytes, 0, array, bytes2.length, bytes.length - 1); System.arraycopy(bytes3, 0, array, bytes2.length + bytes.length - 1, bytes3.length); array[array.length - 1] = 0; return array; } public HTMLCodec(InputStream in, EHTMLReadMode readMode) throws IOException { this.descriptionParsed = false; this.closed = false; this.bufferedStream = new BufferedInputStream(in, 8192); this.readMode = readMode; } public synchronized String getBaseURL() throws IOException { if (!this.descriptionParsed) { this.parseDescription(); } return this.stBaseURL; } public synchronized String getVersion() throws IOException { if (!this.descriptionParsed) { this.parseDescription(); } return this.stVersion; } private void parseDescription() throws IOException { this.stBaseURL = null; this.stVersion = null; long n = -1L; this.iSelStart = n; this.iSelEnd = n; this.iFragStart = n; this.iFragEnd = n; this.iHTMLStart = n; this.iHTMLEnd = n; this.bufferedStream.mark(8192); String[] array = { "Version:", "StartHTML:", "EndHTML:", "StartFragment:", "EndFragment:", "StartSelection:", "EndSelection:", "SourceURL:" }; BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(this.bufferedStream, "UTF-8"), 2730); long ihtmlStart = 0L; long n2 = (long)"\r\n".length(); int length = array.length; int i = 0; while (i < length) { String line = bufferedReader.readLine(); if (null == line) { break; } while (i < length) { if (!line.startsWith(array[i])) { ++i; } else { ihtmlStart += (long)line.length() + n2; String trim = line.substring(array[i].length()).trim(); if (null != trim) { try { switch (i) { case 0: { this.stVersion = trim; break; } case 1: { this.iHTMLStart = (long)Integer.parseInt(trim); break; } case 2: { this.iHTMLEnd = (long)Integer.parseInt(trim); break; } case 3: { this.iFragStart = (long)Integer.parseInt(trim); break; } case 4: { this.iFragEnd = (long)Integer.parseInt(trim); break; } case 5: { this.iSelStart = (long)Integer.parseInt(trim); break; } case 6: { this.iSelEnd = (long)Integer.parseInt(trim); break; } case 7: { this.stBaseURL = trim; break; } } break; } catch (NumberFormatException obj) { throw new IOException("Unable to parse HTML description: " + array[i] + " value " + obj + " invalid"); } } else { break; } } } ++i; } if (-1L == this.iHTMLStart) { this.iHTMLStart = ihtmlStart; } if (-1L == this.iFragStart) { this.iFragStart = this.iHTMLStart; } if (-1L == this.iFragEnd) { this.iFragEnd = this.iHTMLEnd; } if (-1L == this.iSelStart) { this.iSelStart = this.iFragStart; } if (-1L == this.iSelEnd) { this.iSelEnd = this.iFragEnd; } switch (this.readMode) { case HTML_READ_ALL: { this.iStartOffset = this.iHTMLStart; this.iEndOffset = this.iHTMLEnd; break; } case HTML_READ_FRAGMENT: { this.iStartOffset = this.iFragStart; this.iEndOffset = this.iFragEnd; break; } default: { this.iStartOffset = this.iSelStart; this.iEndOffset = this.iSelEnd; break; } } this.bufferedStream.reset(); if (-1L == this.iStartOffset) { throw new IOException("Unable to parse HTML description: invalid HTML format."); } int n3; for (n3 = 0; (long)n3 < this.iStartOffset; n3 = (int)((long)n3 + this.bufferedStream.skip(this.iStartOffset - (long)n3))) {} this.iReadCount = (long)n3; if (this.iStartOffset != this.iReadCount) { throw new IOException("Unable to parse HTML description: Byte stream ends in description."); } this.descriptionParsed = true; return; } @Override public synchronized int read() throws IOException { if (this.closed) { throw new IOException("Stream closed"); } if (!this.descriptionParsed) { this.parseDescription(); } if (-1L != this.iEndOffset && this.iReadCount >= this.iEndOffset) { return -1; } int read = this.bufferedStream.read(); if (read == -1) { return -1; } ++this.iReadCount; return read; } @Override public synchronized void close() throws IOException { if (!this.closed) { this.closed = true; this.bufferedStream.close(); } } }