readstream.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,347 行 · 第 1/2 页
JAVA
1,347 行
else if (cb.length() == 0) return null; else return cb.toString(); } /** * Fills the buffer with the next line from the input stream. * * @return true on success, false on end of file. */ public final boolean readln(CharBuffer cb) throws IOException { return readLine(cb, true); } /** * Reads a line into the character buffer. \r\n is converted to \n. * * @param buf character buffer to fill * @return false on end of file */ public final boolean readLine(CharBuffer cb) throws IOException { return readLine(cb, true); } /** * Reads a line into the character buffer. \r\n is converted to \n. * * @param buf character buffer to fill * @return false on end of file */ public final boolean readLine(CharBuffer cb, boolean isChop) throws IOException { if (_readEncoding != null) return readlnEncoded(cb, isChop); int capacity = cb.getCapacity(); int offset = cb.getLength(); char []buf = cb.getBuffer(); byte []readBuffer = _readBuffer; while (true) { int readOffset = _readOffset; int sublen = _readLength - readOffset; if (capacity - offset < sublen) sublen = capacity - offset; for (; sublen > 0; sublen--) { int ch = readBuffer[readOffset++] & 0xff; if (ch != '\n') { buf[offset++] = (char) ch; } else if (isChop) { if (offset > 0 && buf[offset - 1] == '\r') cb.setLength(offset - 1); else cb.setLength(offset); _readOffset = readOffset; return true; } else { buf[offset++] = (char) '\n'; cb.setLength(offset); _readOffset = readOffset; return true; } } _readOffset = readOffset; if (_readLength <= readOffset) { if (! readBuffer()) { cb.setLength(offset); return offset > 0; } } if (capacity <= offset) { cb.setLength(offset + 1); capacity = cb.getCapacity(); buf = cb.getBuffer(); } } } /** * Reads a line into the character buffer. \r\n is converted to \n. * * @param buf character buffer to fill. * @param length number of characters to fill. * * @return -1 on end of file or the number of characters read. */ public final int readLine(char []buf, int length) throws IOException { return readLine(buf, length, true); } /** * Reads a line into the character buffer. \r\n is converted to \n. * * @param buf character buffer to fill. * @param length number of characters to fill. * * @return -1 on end of file or the number of characters read. */ public final int readLine(char []buf, int length, boolean isChop) throws IOException { byte []readBuffer = _readBuffer; int offset = 0; while (true) { int readOffset = _readOffset; int sublen = _readLength - readOffset; if (sublen < length) sublen = length; for (; sublen > 0; sublen--) { int ch = readBuffer[readOffset++] & 0xff; if (ch != '\n') { } else if (isChop) { _readOffset = readOffset; if (offset > 0 && buf[offset - 1] == '\r') return offset - 1; else return offset; } else { buf[offset++] = (char) ch; _readOffset = readOffset; return offset + 1; } buf[offset++] = (char) ch; } _readOffset = readOffset; if (readOffset <= _readLength) { if (! readBuffer()) { return offset; } } if (length <= offset) return length + 1; } } private boolean readlnEncoded(CharBuffer cb, boolean isChop) throws IOException { while (true) { int ch = readChar(); if (ch < 0) return cb.length() > 0; if (ch != '\n') { } else if (isChop) { if (cb.length() > 0 && cb.getLastChar() == '\r') cb.setLength(cb.getLength() - 1); return true; } else { cb.append('\n'); return true; } cb.append((char) ch); } } // // data api // /** * Reads a 4-byte network encoded integer */ public int readInt() throws IOException { if (_readOffset + 4 < _readLength) { return (((_readBuffer[_readOffset++] & 0xff) << 24) + ((_readBuffer[_readOffset++] & 0xff) << 16) + ((_readBuffer[_readOffset++] & 0xff) << 8) + ((_readBuffer[_readOffset++] & 0xff))); } else { return ((read() << 24) + (read() << 16) + (read() << 8) + (read())); } } /** * Reads an 8-byte network encoded long */ public long readLong() throws IOException { return (((long) read() << 56) + ((long) read() << 48) + ((long) read() << 40) + ((long) read() << 32) + ((long) read() << 24) + ((long) read() << 16) + ((long) read() << 8) + ((long) read())); } /** * Reads a utf-8 string */ public int readUTF8ByByteLength(char []buffer, int offset, int byteLength) throws IOException { int k = 0; for (int i = 0; i < byteLength; i++) { if (_readLength <= _readOffset) { readBuffer(); } int ch = _readBuffer[_readOffset++]; if (ch < 0x80) buffer[k++] = (char) ch; else if ((ch & 0xe0) == 0xc0) { int c2 = read(); i += 1; buffer[k++] = (char) (((ch & 0x1f) << 6) + (c2 & 0x3f)); } else { int c2 = read(); int c3 = read(); i += 2; buffer[k++] = (char) (((ch & 0x1f) << 12) + ((c2 & 0x3f) << 6) + ((c3 & 0x3f))); } } return k; } /** * Copies this stream to the output stream. * * @param os destination stream. */ public void writeToStream(OutputStream os) throws IOException { if (_readLength <= _readOffset) { readBuffer(); } while (_readOffset < _readLength) { os.write(_readBuffer, _readOffset, _readLength - _readOffset); readBuffer(); } } /** * Writes <code>len<code> bytes to the output stream from this stream. * * @param os destination stream. * @param len bytes to write. */ public void writeToStream(OutputStream os, int len) throws IOException { while (len > 0) { if (_readLength <= _readOffset) { if (! readBuffer()) return; } int sublen = _readLength - _readOffset; if (len < sublen) sublen = len; os.write(_readBuffer, _readOffset, sublen); _readOffset += sublen; len -= sublen; } } /** * Copies this stream to the output stream. * * @param out destination writer */ public void writeToWriter(Writer out) throws IOException { int ch; while ((ch = readChar()) >= 0) out.write((char) ch); } /** * Fills the buffer from the underlying stream. */ public int fillBuffer() throws IOException { if (! readBuffer()) return -1; else return _readLength; } /** * Fills the buffer with a non-blocking read. */ public boolean readNonBlock() throws IOException { if (_readOffset < _readLength) return true; if (_readBuffer == null) { _readOffset = 0; _readLength = 0; return false; } if (_sibling != null) _sibling.flush(); _readOffset = 0; _readLength = _source.readNonBlock(_readBuffer, 0, _readBuffer.length); // Setting to 0 is needed to avoid int to long conversion errors with AIX if (_readLength > 0) { _position += _readLength; _readTime = Alarm.getCurrentTime(); return true; } else { _readLength = 0; return false; } } /** * Fills the buffer with a non-blocking read. */ public boolean fillWithTimeout(long timeout) throws IOException { if (_readOffset < _readLength) return true; if (_readBuffer == null) { _readOffset = 0; _readLength = 0; return false; } if (_sibling != null) _sibling.flush(); _readOffset = 0; _readLength = _source.readTimeout(_readBuffer, 0, _readBuffer.length, timeout); // Setting to 0 is needed to avoid int to long conversion errors with AIX if (_readLength > 0) { _position += _readLength; _readTime = Alarm.getCurrentTime(); return true; } else { _readLength = 0; return false; } } /** * Fills the read buffer, flushing the write buffer. * * @return false on end of file and true if there's more data. */ private boolean readBuffer() throws IOException { if (_readBuffer == null || _source == null) { _readOffset = 0; _readLength = 0; return false; } if (_sibling != null) _sibling.flush(); _readOffset = 0; _readLength = _source.read(_readBuffer, 0, _readBuffer.length); // Setting to 0 is needed to avoid int to long conversion errors with AIX if (_readLength > 0) { _position += _readLength; _readTime = Alarm.getCurrentTime(); return true; } else { _readLength = 0; return false; } } private boolean readBuffer(int off) throws IOException { if (_readBuffer == null) return false; if (_sibling != null) _sibling.flush(); _readOffset = 0; _readLength = _source.read(_readBuffer, off, _readBuffer.length - off); // Setting to 0 is needed to avoid int to long conversion errors with AIX if (_readLength > 0) { _position += _readLength; _readTime = Alarm.getCurrentTime(); return true; } else { _readLength = 0; return false; } } /** * Disables close. Sometimes an application will pass a stream * to a client that may close the stream at an inappropriate time. * Setting disable close gives the calling routine control over closing * the stream. */ public void setDisableClose(boolean disableClose) { _disableClose = disableClose; } /** * Disables closing of the underlying source. */ public void setDisableCloseSource(boolean disableClose) { _isDisableCloseSource = disableClose; } /** * Close the stream. */ @Override public final void close() { try { if (_disableClose) return; if (! _reuseBuffer) { if (_tempRead != null) { TempBuffer.free(_tempRead); _tempRead = null; } _readBuffer = null; } if (_readEncoding != null) { Reader reader = _readEncoding; _readEncoding = null; reader.close(); } if (_source != null && ! _isDisableCloseSource) { StreamImpl s = _source; _source = null; s.close(); } } catch (IOException e) { log().log(Level.FINE, e.toString(), e); } } /** * Returns a named attribute. For example, an HTTP stream * may use this to return header values. */ public Object getAttribute(String name) throws IOException { if (_sibling != null) _sibling.flush(); return _source.getAttribute(name); } /** * Lists all named attributes. */ public Iterator getAttributeNames() throws IOException { if (_sibling != null) _sibling.flush(); return _source.getAttributeNames(); } /** * Sets a named attribute. For example, an HTTP stream * may use this to set header values. */ public void setAttribute(String name, Object value) throws IOException { _source.setAttribute(name, value); } /** * Removes a named attribute. */ public void removeAttribute(String name) throws IOException { _source.removeAttribute(name); } /** * Returns the Path which opened this stream. */ public Path getPath() { return _source == null ? null : _source.getPath(); } /** * Returns the user path which opened this stream. * * <p>Parsing routines typically use this for error reporting. */ public String getUserPath() { if (_source == null || _source.getPath() == null) return "stream"; else return _source.getPath().getUserPath(); } /** * Returns the user path which opened this stream. * * <p>Parsing routines typically use this for error reporting. */ public String getURL() { if (_source == null || _source.getPath() == null) return "stream:"; else return _source.getPath().getURL(); } /** * Sets a path name associated with the stream. */ public void setPath(Path path) { _source.setPath(path); } /** * Returns a Reader reading to this stream. */ public Reader getReader() { if (_reader == null) _reader = new StreamReader(); return _reader; } private static Logger log() { return Logger.getLogger(ReadStream.class.getName()); } /** * Returns a printable representation of the read stream. */ public String toString() { return "ReadStream[" + _source + "]"; } public boolean lock(boolean shared, boolean block) { if (! (_source instanceof LockableStream)) return true; LockableStream ls = (LockableStream) _source; return ls.lock(shared, block); } public boolean unlock() { if (! (_source instanceof LockableStream)) return true; LockableStream ls = (LockableStream) _source; return ls.unlock(); } public class StreamReader extends Reader { public final int read() throws IOException { return ReadStream.this.readChar(); } public final int read(char []cbuf, int off, int len) throws IOException { return ReadStream.this.read(cbuf, off, len); } public boolean ready() throws IOException { return ReadStream.this.available() > 0; } public final void close() throws IOException { } public ReadStream getStream() { return ReadStream.this; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?