public int read()

in src/main/java/org/apache/commons/net/telnet/TelnetInputStream.java [171:266]


    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) {
                    final IOException e;
                    e = ioException;
                    ioException = null;
                    throw e;
                }

                if (bytesAvailable == 0) {
                    // Return EOF if at end of file
                    if (hasReachedEOF) {
                        return EOF;
                    }

                    // Otherwise, we have to wait for queue to get something
                    if (threaded) {
                        queue.notify();
                        try {
                            readIsWaiting = true;
                            queue.wait();
                            readIsWaiting = false;
                        } catch (final InterruptedException e) {
                            throw new InterruptedIOException("Fatal thread interruption during read.");
                        }
                    } else {
                        // alreadyread = false;
                        readIsWaiting = true;
                        int ch;
                        boolean mayBlock = true; // block on the first read only

                        do {
                            try {
                                if ((ch = read(mayBlock)) < 0 && ch != WOULD_BLOCK) {
                                    // must be EOF
                                    return ch;
                                }
                            } catch (final InterruptedIOException e) {
                                synchronized (queue) {
                                    ioException = e;
                                    queue.notifyAll();
                                    try {
                                        queue.wait(100);
                                    } catch (final InterruptedException interrupted) {
                                        // Ignored
                                    }
                                }
                                return EOF;
                            }

                            try {
                                if (ch != WOULD_BLOCK) {
                                    processChar(ch);
                                }
                            } catch (final InterruptedException e) {
                                if (isClosed) {
                                    return EOF;
                                }
                            }

                            // Reads should not block on subsequent iterations. Potentially, this could happen if the
                            // remaining buffered socket data consists entirely of Telnet command sequence and no "user" data.
                            mayBlock = false;

                        }
                        // Continue reading as long as there is data available and the queue is not full.
                        while (super.available() > 0 && bytesAvailable < queue.length - 1);

                        readIsWaiting = false;
                    }
                    continue;
                }
                final 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; // NOPMD TODO?
            }
        }
    }