⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sharedfileinputstream.java

📁 java Email you can use it to send email to others
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    len = (int)(datalen - (bufpos - start + pos));	int n = in.read(buf, pos, len);        if (n > 0)            count = n + pos;    }    /**     * See the general contract of the <code>read</code>     * method of <code>InputStream</code>.     *     * @return     the next byte of data, or <code>-1</code> if the end of the     *             stream is reached.     * @exception  IOException  if an I/O error occurs.     */    public synchronized int read() throws IOException {        ensureOpen();	if (pos >= count) {	    fill();	    if (pos >= count)		return -1;	}	return buf[pos++] & 0xff;    }    /**     * Read characters into a portion of an array, reading from the underlying     * stream at most once if necessary.     */    private int read1(byte[] b, int off, int len) throws IOException {	int avail = count - pos;	if (avail <= 0) {	    if (false) {	    /* If the requested length is at least as large as the buffer, and	       if there is no mark/reset activity, do not bother to copy the	       bytes into the local buffer.  In this way buffered streams will	       cascade harmlessly. */	    if (len >= buf.length && markpos < 0) {		// XXX - seek, update bufpos - how?		return in.read(b, off, len);	    }	    }	    fill();	    avail = count - pos;	    if (avail <= 0) return -1;	}	int cnt = (avail < len) ? avail : len;	System.arraycopy(buf, pos, b, off, cnt);	pos += cnt;	return cnt;    }    /**     * Reads bytes from this stream into the specified byte array,     * starting at the given offset.     *     * <p> This method implements the general contract of the corresponding     * <code>{@link java.io.InputStream#read(byte[], int, int) read}</code>     * method of the <code>{@link java.io.InputStream}</code> class.     *     * @param      b     destination buffer.     * @param      off   offset at which to start storing bytes.     * @param      len   maximum number of bytes to read.     * @return     the number of bytes read, or <code>-1</code> if the end of     *             the stream has been reached.     * @exception  IOException  if an I/O error occurs.     */    public synchronized int read(byte b[], int off, int len)	throws IOException    {        ensureOpen();        if ((off | len | (off + len) | (b.length - (off + len))) < 0) {	    throw new IndexOutOfBoundsException();	} else if (len == 0) {	    return 0;	}	int n = read1(b, off, len);	if (n <= 0) return n;	while ((n < len) /* && (in.available() > 0) */) {	    int n1 = read1(b, off + n, len - n);	    if (n1 <= 0) break;	    n += n1;	}	return n;    }    /**     * See the general contract of the <code>skip</code>     * method of <code>InputStream</code>.     *     * @param      n   the number of bytes to be skipped.     * @return     the actual number of bytes skipped.     * @exception  IOException  if an I/O error occurs.     */    public synchronized long skip(long n) throws IOException {        ensureOpen();	if (n <= 0) {	    return 0;	}	long avail = count - pos;             if (avail <= 0) {            // If no mark position set then don't keep in buffer	    /*            if (markpos <0)                 return in.skip(n);	    */                        // Fill in buffer to save bytes for reset            fill();            avail = count - pos;            if (avail <= 0)                return 0;        }                long skipped = (avail < n) ? avail : n;        pos += skipped;        return skipped;    }    /**     * Returns the number of bytes that can be read from this input      * stream without blocking.      *     * @return     the number of bytes that can be read from this input     *             stream without blocking.     * @exception  IOException  if an I/O error occurs.     */    public synchronized int available() throws IOException {        ensureOpen();	return (count - pos) + in_available();    }    private int in_available() throws IOException {	// XXX - overflow	return (int)((start + datalen) - (bufpos + count));    }    /**      * See the general contract of the <code>mark</code>     * method of <code>InputStream</code>.     *     * @param   readlimit   the maximum limit of bytes that can be read before     *                      the mark position becomes invalid.     * @see     #reset()     */    public synchronized void mark(int readlimit) {	marklimit = readlimit;	markpos = pos;    }    /**     * See the general contract of the <code>reset</code>     * method of <code>InputStream</code>.     * <p>     * If <code>markpos</code> is <code>-1</code>     * (no mark has been set or the mark has been     * invalidated), an <code>IOException</code>     * is thrown. Otherwise, <code>pos</code> is     * set equal to <code>markpos</code>.     *     * @exception  IOException  if this stream has not been marked or     *               if the mark has been invalidated.     * @see        #mark(int)     */    public synchronized void reset() throws IOException {        ensureOpen();	if (markpos < 0)	    throw new IOException("Resetting to invalid mark");	pos = markpos;    }    /**     * Tests if this input stream supports the <code>mark</code>      * and <code>reset</code> methods. The <code>markSupported</code>      * method of <code>SharedFileInputStream</code> returns      * <code>true</code>.      *     * @return  a <code>boolean</code> indicating if this stream type supports     *          the <code>mark</code> and <code>reset</code> methods.     * @see     java.io.InputStream#mark(int)     * @see     java.io.InputStream#reset()     */    public boolean markSupported() {	return true;    }    /**     * Closes this input stream and releases any system resources      * associated with the stream.      *     * @exception  IOException  if an I/O error occurs.     */    public void close() throws IOException {        if (in == null)            return;	try {	    if (master)		sf.forceClose();	    else		sf.close();	} finally {	    sf = null;	    in = null;	    buf = null;	}    }    /**     * Return the current position in the InputStream, as an     * offset from the beginning of the InputStream.     *     * @return  the current position     */    public long getPosition() {//System.out.println("getPosition: start " + start + " pos " + pos + " bufpos " + bufpos + " = " + (bufpos + pos - start));	if (in == null)	    throw new RuntimeException("Stream closed");	return bufpos + pos - start;    }    /**     * Return a new InputStream representing a subset of the data     * from this InputStream, starting at <code>start</code> (inclusive)     * up to <code>end</code> (exclusive).  <code>start</code> must be     * non-negative.  If <code>end</code> is -1, the new stream ends     * at the same place as this stream.  The returned InputStream     * will also implement the SharedInputStream interface.     *     * @param	start	the starting position     * @param	end	the ending position + 1     * @return		the new stream     */    public InputStream newStream(long start, long end) {	if (in == null)	    throw new RuntimeException("Stream closed");	if (start < 0)	    throw new IllegalArgumentException("start < 0");	if (end == -1)	    end = datalen;	return new SharedFileInputStream(sf,			this.start + (int)start, (int)(end - start), bufsize);    }    // for testing...    /*    public static void main(String[] argv) throws Exception {	SharedFileInputStream is = new SharedFileInputStream(argv[0]);	java.util.Random r = new java.util.Random();	int b;	while ((b = is.read()) >= 0) {	    System.out.write(b);	    if (r.nextDouble() < 0.3) {		InputStream is2 = is.newStream(is.getPosition(), -1);		int b2;		while ((b2 = is2.read()) >= 0)		    ;	    }	}    }    */    /**     * Force this stream to close.     */    protected void finalize() throws Throwable {	super.finalize();	close();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -