telnetinputstream.java

来自「apache推出的net包」· Java 代码 · 共 602 行 · 第 1/2 页

JAVA
602
字号
            if (++__queueTail >= __queue.length)                __queueTail = 0;        }    }    public int read() throws IOException    {        // Critical section because we're altering __bytesAvailable,        // __queueHead, and the contents of _queue in addition to        // testing value of __hasReachedEOF.        synchronized (__queue)        {            while (true)            {                if (__ioException != null)                {                    IOException e;                    e = __ioException;                    __ioException = null;                    throw e;                }                if (__bytesAvailable == 0)                {                    // Return -1 if at end of file                    if (__hasReachedEOF)                        return -1;                    // Otherwise, we have to wait for queue to get something                    if(__threaded)                    {                        __queue.notify();                        try                        {                            __readIsWaiting = true;                            __queue.wait();                            __readIsWaiting = false;                        }                        catch (InterruptedException e)                        {                            throw new IOException("Fatal thread interruption during read.");                        }                    }                    else                    {                        //__alreadyread = false;                        __readIsWaiting = true;                        int ch;                        do                        {                            try                            {                                if ((ch = __read()) < 0)                                    if(ch != -2)                                        return (ch);                            }                            catch (InterruptedIOException e)                            {                                synchronized (__queue)                                {                                    __ioException = e;                                    __queue.notifyAll();                                    try                                    {                                        __queue.wait(100);                                    }                                    catch (InterruptedException interrupted)                                    {                                    }                                }                                return (-1);                            }                            try                            {                                if(ch != -2)                                {                                    __processChar(ch);                                }                            }                            catch (InterruptedException e)                            {                                if (__isClosed)                                    return (-1);                            }                        }                        while (super.available() > 0);                        __readIsWaiting = false;                    }                    continue;                }                else                {                    int ch;                    ch = __queue[__queueHead];                    if (++__queueHead >= __queue.length)                        __queueHead = 0;                    --__bytesAvailable;		    // Need to explicitly notify() so available() works properly		    if(__bytesAvailable == 0 && __threaded) {			    __queue.notify();		    }		                        return ch;                }            }        }    }    /***     * Reads the next number of bytes from the stream into an array and     * returns the number of bytes read.  Returns -1 if the end of the     * stream has been reached.     * <p>     * @param buffer  The byte array in which to store the data.     * @return The number of bytes read. Returns -1 if the     *          end of the message has been reached.     * @exception IOException If an error occurs in reading the underlying     *            stream.     ***/    public int read(byte buffer[]) throws IOException    {        return read(buffer, 0, buffer.length);    }    /***     * Reads the next number of bytes from the stream into an array and returns     * the number of bytes read.  Returns -1 if the end of the     * message has been reached.  The characters are stored in the array     * starting from the given offset and up to the length specified.     * <p>     * @param buffer The byte array in which to store the data.     * @param offset  The offset into the array at which to start storing data.     * @param length   The number of bytes to read.     * @return The number of bytes read. Returns -1 if the     *          end of the stream has been reached.     * @exception IOException If an error occurs while reading the underlying     *            stream.     ***/    public int read(byte buffer[], int offset, int length) throws IOException    {        int ch, off;        if (length < 1)            return 0;        // Critical section because run() may change __bytesAvailable        synchronized (__queue)        {            if (length > __bytesAvailable)                length = __bytesAvailable;        }        if ((ch = read()) == -1)            return -1;        off = offset;        do        {            buffer[offset++] = (byte)ch;        }        while (--length > 0 && (ch = read()) != -1);        //__client._spyRead(buffer, off, offset - off);        return (offset - off);    }    /*** Returns false.  Mark is not supported. ***/    public boolean markSupported()    {        return false;    }    public int available() throws IOException    {        // Critical section because run() may change __bytesAvailable        synchronized (__queue)        {            return __bytesAvailable;        }    }    // Cannot be synchronized.  Will cause deadlock if run() is blocked    // in read because BufferedInputStream read() is synchronized.    public void close() throws IOException    {        // Completely disregard the fact thread may still be running.        // We can't afford to block on this close by waiting for        // thread to terminate because few if any JVM's will actually        // interrupt a system read() from the interrupt() method.        super.close();        synchronized (__queue)        {            __hasReachedEOF = true;            __isClosed      = true;            if (__thread != null && __thread.isAlive())            {                __thread.interrupt();            }            __queue.notifyAll();        }        __threaded = false;    }    public void run()    {        int ch;        try        {_outerLoop:            while (!__isClosed)            {                try                {                    if ((ch = __read()) < 0)                        break;                }                catch (InterruptedIOException e)                {                    synchronized (__queue)                    {                        __ioException = e;                        __queue.notifyAll();                        try                        {                            __queue.wait(100);                        }                        catch (InterruptedException interrupted)                        {                            if (__isClosed)                                break _outerLoop;                        }                        continue;                    }                } catch(RuntimeException re) {                    // We treat any runtime exceptions as though the                    // stream has been closed.  We close the                    // underlying stream just to be sure.                    super.close();                    // Breaking the loop has the effect of setting                    // the state to closed at the end of the method.                    break _outerLoop;                }                try                {                    __processChar(ch);                }                catch (InterruptedException e)                {                    if (__isClosed)                        break _outerLoop;                }            }        }        catch (IOException ioe)        {            synchronized (__queue)            {                __ioException = ioe;            }        }        synchronized (__queue)        {            __isClosed      = true; // Possibly redundant            __hasReachedEOF = true;            __queue.notify();        }        __threaded = false;    }}/* Emacs configuration * Local variables:        ** * mode:             java  ** * c-basic-offset:   4     ** * indent-tabs-mode: nil   ** * End:                    ** */

⌨️ 快捷键说明

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