📄 inputstreamwrapper.java
字号:
return new InputStreamReader(new WrappedStream(), m_encodingName); } } /** * Get encoding for input document. This call may not return an accurate * result until after {@link #getReader} is called. * * @return character encoding for input document */ public String getEncoding() { return m_encodingName; } /** * Close document input. Completes reading of document input, including * closing the input medium. * * @throws IOException on error closing document */ public void close() throws IOException { if (m_stream != null) { m_stream.close(); m_stream = null; } reset(); } /** * Reset to initial state for reuse. */ public void reset() { m_isEnd = false; m_endOffset = 0; m_emptyOffset = 0; m_encodingName = null; } /** * Stream that just uses the enclosing class to buffer input from the * wrapped stream. */ private class WrappedStream extends InputStream { /* (non-Javadoc) * @see java.io.InputStream#available() */ public int available() throws IOException { return m_endOffset - m_emptyOffset + m_stream.available(); } /* (non-Javadoc) * @see java.io.InputStream#close() */ public void close() throws IOException { InputStreamWrapper.this.close(); } /* (non-Javadoc) * @see java.io.InputStream#read(byte[], int, int) */ public int read(byte[] b, int off, int len) throws IOException { int avail; int actual = 0; while (len > (avail = m_endOffset - m_emptyOffset)) { System.arraycopy(m_buffer, m_emptyOffset, b, off, avail); off += avail; len -= avail; actual += avail; m_emptyOffset = m_endOffset = 0; if (!fillBuffer()) { return actual == 0 ? -1 : actual; } } System.arraycopy(m_buffer, m_emptyOffset, b, off, len); m_emptyOffset += len; return actual + len; } /* (non-Javadoc) * @see java.io.InputStream#read(byte[]) */ public int read(byte[] b) throws IOException { return read(b, 0, b.length); } /* (non-Javadoc) * @see java.io.InputStream#skip(long) */ public long skip(long n) throws IOException { int avail = m_endOffset - m_emptyOffset; if (n >= (long)avail) { return avail + m_stream.skip(n - avail); } else { m_emptyOffset += (int)n; return n; } } /* (non-Javadoc) * @see java.io.InputStream#read() */ public int read() throws IOException { if (m_emptyOffset >= m_endOffset && !fillBuffer()) { return -1; } else { return m_buffer[m_emptyOffset++]; } } } /** * Reader for input stream using UTF-8 encoding. This uses the enclosing * class to buffer input from the stream, interpreting it as characters on * demand. */ private class WrappedStreamUTF8Reader extends Reader { /* (non-Javadoc) * @see java.io.Reader#close() */ public void close() throws IOException { InputStreamWrapper.this.close(); } /* (non-Javadoc) * @see java.io.Reader#read(char[], int, int) */ public int read(char[] b, int off, int len) throws IOException { // load up local variables for conversion loop int end = off + len; int empty = m_emptyOffset; byte[] buff = m_buffer; while (off < end) { // fill buffer if less than maximum byte count in character if (empty + 3 > m_endOffset) { m_emptyOffset = empty; fillBuffer(); empty = m_emptyOffset; if (empty == m_endOffset) { int actual = len + off - end; return actual > 0 ? actual : -1; } } // check for single-byte vs multi-byte character next int byt = buff[empty++]; if (byt >= 0) { // single-byte character, just store to output array b[off++] = (char)byt; if (byt == 0) { System.err.println("Wrote null"); } } else if ((byt & 0xE0) == 0xC0) { // double-byte character, check bytes available and store if (empty < m_endOffset) { b[off++] = (char)(((byt & 0x1F) << 6) + (buff[empty++] & 0x3F)); if (b[off-1] == 0) { System.err.println("Wrote null"); } } else { throw new IOException("UTF-8 conversion error"); } } else { // three-byte character, check bytes available and store if (empty + 1 < m_endOffset) { int byt2 = buff[empty++] & 0x3F; b[off++] = (char)((((byt & 0x0F) << 6) + byt2 << 6) + (buff[empty++] & 0x3F)); if (b[off-1] == 0) { System.err.println("Wrote null"); } } else { throw new IOException("UTF-8 conversion error"); } } } m_emptyOffset = empty; return len; } /* (non-Javadoc) * @see java.io.Reader#read(char[]) */ public int read(char[] b) throws IOException { return read(b, 0, b.length); } /* (non-Javadoc) * @see java.io.Reader#read() */ public int read() throws IOException { // fill buffer if less than maximum byte count in character if (m_emptyOffset + 3 > m_endOffset) { fillBuffer(); if (m_emptyOffset == m_endOffset) { return -1; } } // check for single-byte vs multi-byte character next int byt = m_buffer[m_emptyOffset++]; if (byt >= 0) { // single-byte character, just store to output array return byt & 0xFF; } else if ((byt & 0xE0) == 0xC0) { // double-byte character, check bytes available and store if (m_emptyOffset < m_endOffset) { return ((byt & 0x1F) << 6) + (m_buffer[m_emptyOffset++] & 0x3F); } else { throw new IOException("UTF-8 conversion error"); } } else { // three-byte character, check bytes available and store if (m_emptyOffset + 1 < m_endOffset) { int byt2 = m_buffer[m_emptyOffset++] & 0xFF; return (((byt & 0x0F) << 6) + byt2 << 6) + (m_buffer[m_emptyOffset++] & 0x3F); } else { throw new IOException("UTF-8 conversion error"); } } } /* (non-Javadoc) * @see java.io.Reader#ready() */ public boolean ready() throws IOException { return m_emptyOffset + 2 < m_endOffset; } } /** * Reader for input stream using ISO8859-1 encoding. This uses the enclosing * class to buffer input from the stream, interpreting it as characters on * demand. */ private class WrappedStreamISO88591Reader extends Reader { /* (non-Javadoc) * @see java.io.Reader#close() */ public void close() throws IOException { InputStreamWrapper.this.close(); } /* (non-Javadoc) * @see java.io.Reader#read(char[], int, int) */ public int read(char[] b, int off, int len) throws IOException { // load up local variables for conversion loop int end = off + len; int empty = m_emptyOffset; byte[] buff = m_buffer; while (off < end) { // make sure there's data in buffer int avail = m_endOffset - empty; if (avail == 0) { m_emptyOffset = empty; if (fillBuffer()) { empty = m_emptyOffset; avail = m_endOffset - empty; } else { int actual = len + off - end; return actual > 0 ? actual : -1; } } // find count of bytes to convert to characters int use = end - off; if (use > avail) { use = avail; } // convert bytes directly to characters int limit = empty + use; while (empty < limit) { b[off++] = (char)(buff[empty++] & 0xFF); } } m_emptyOffset = empty; return len; } /* (non-Javadoc) * @see java.io.Reader#read(char[]) */ public int read(char[] b) throws IOException { return read(b, 0, b.length); } /* (non-Javadoc) * @see java.io.Reader#read() */ public int read() throws IOException { if (m_emptyOffset >= m_endOffset && !fillBuffer()) { return -1; } else { return m_buffer[m_emptyOffset++] & 0xFF; } } /* (non-Javadoc) * @see java.io.Reader#ready() */ public boolean ready() throws IOException { return m_emptyOffset < m_endOffset; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -