7

Basically, I have a URL that streams xml updates from a chat room when new messages are posted. I'd like to turn that URL into an InputStream and continue reading from it as long as the connection is maintained and as long as I haven't sent a Thread.interrupt(). The problem I'm experiencing is that BufferedReader.ready() doesn't seem to become true when there is content to be read from the stream.

I'm using the following code:

BufferedReader buf = new BufferedReader(new InputStreamReader(ins));


String str = "";
while(Thread.interrupted() != true)
{
    connected = true;
    debug("Listening...");

    if(buf.ready())
    {
        debug("Something to be read.");
        if ((str = buf.readLine()) != null) {
            // str is one line of text; readLine() strips the newline character(s)
            urlContents += String.format("%s%n", str);
            urlContents = filter(urlContents);
        }
    }

    // Give the system a chance to buffer or interrupt.
    try{Thread.sleep(1000);} catch(Exception ee) {debug("Caught thread exception.");}
}

When I run the code, and post something to the chat room, buf.ready() never becomes true, resulting in the lines never being read. However, if I skip the "buf.ready()" part and just read lines directly, it blocks further action until lines are read.

How do I either a) get buf.ready() to return true, or b) do this in such a way as to prevent blocking?

Thanks in advance, James

Warkior
  • 155
  • 1
  • 3
  • 9

5 Answers5

9

How to create a Java non-blocking InputStream

You can't. Your question embodies a contradiciton in terms. Streams in Java are blocking. There is therefore no such thing as a 'non-blocking InputStream'.

Reader.ready() returns true when data can be read without blocking. Period. InputStreams and Readers are blocking. Period. Everything here is working as designed. If you want more concurrency with these APIs you will have to use multiple threads. Or Socket.setSoTimeout() and its near relation in HttpURLConnection.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I know I can split things off into threads... this code is already in it's own thread (runnable) object. What I want is to know how to stop the thread by sending some sort of interrupt. When the input stream is waiting for more data to be published to the stream, it seems to block everything else, including thread.interrupts. – Warkior Feb 19 '11 at 19:10
  • 1
    Can you describe a situation where Reader.ready() would be able to return true, if (as you said) Readers are naturally blocking? Based on what you said above, it would seem that Reader.ready() is a useless method. – Warkior Feb 19 '11 at 19:17
  • If data is already available, ready() returns true and a Reader will not block. If no data is available, ready() returns false and a Reader will block. – user207421 Feb 19 '11 at 23:30
  • So, if I KNOW there is data available in the stream to be read, how come buf.ready() continues to return false? That's the part that is confusing to me. I KNOW there is data ready to be read in the stream. – Warkior Feb 20 '11 at 01:47
  • You can't 'know' that. Only ready() knows that (and InputStream.available(), in both cases where supported). There is no other test. For some streams like SSL neither is supported, so ready() returns false and available() returns zero. Aso there is a difference between data being available and a complete line being available to readLine(), including the line terminator. readLine() will block until all that arrives – user207421 Feb 20 '11 at 22:19
7

For nonblocking IO don't use InputStream and Reader (or OutputStream/Writer), but use the java.nio.* classes, in this case a SocketChannel (and additional a CharsetDecoder).


Edit: as an answer to your comment:

Specifically looking for how to create a socket channel to an https url.

Sockets (and also SocketChannels) work on the transport layer (TCP), one (or two) level(s) below application layer protocols like HTTP. So you can't create a socket channel to an https url.

You would instead have to open a Socket-Channel to the right server and the right port (443 if nothing else given in the URI), create an SSLEngine (in javax.net.ssl) in client mode, then read data from the channel, feeding it to the SSL engine and the other way around, and send/get the right HTTP protocol lines to/from your SSLEngine, always checking the return values to know how many bytes were in fact processed and what would be the next step to take.

This is quite complicated (I did it once), and you don't really want to do this if you are not implementing a server with lots of clients connected at the same time (where you can't have a single thread for each connection). Instead, stay with your blocking InputStream which reads from your URLConnection, and put it simply in a spare thread which does not hinder the rest of your application.

Dave Moten
  • 11,957
  • 2
  • 40
  • 47
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • Specifically looking for how to create a socket channel to an https url. – Warkior Feb 19 '11 at 19:15
  • 1
    @Warkior: see my last edit - you don't really want to do this. – Paŭlo Ebermann Feb 19 '11 at 20:15
  • Hi Paŭlo, Thanks for the advice. That makes a lot of sense, and answers my main concern of that particular method. I really have no control over the server... just the client reading from the stream. Is there a proper way to terminate a blocked connection in this situation? This will need to happen if the user changes to a new chat room. (means the system needs to start listening to a different stream, terminating the old listener) – Warkior Feb 19 '11 at 22:52
1

You can use the Java NIO library which provides non-blocking I/O capabilities. Take a look at this article for details and sample code: http://www.drdobbs.com/java/184406242.

Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
Alex Abdugafarov
  • 6,112
  • 7
  • 35
  • 59
  • This looks like it has the means I was looking for to cause the threads to time-out, breaking the blocking connection. Going to give it a try. Thank you. – Warkior Feb 19 '11 at 19:27
  • Hmm. Haven't managed to find a way to properly interrupt the thread that is blocked yet. Maybe I'm just not doing it right. – Warkior Feb 19 '11 at 22:54
0

There is no HTTP/HTTPS implementation using Channels. There is no way to read the inputstream from a httpurlconnaction in a non-blocking way. You either have to use a third party lib or implement http over SocketChannel yourself.

bla
  • 1
-3
import java.io.InputStream;
import java.util.Arrays;

/**
 * This code demonstrates non blocking read from standard input using separate
 * thread for reading.
 */
public class NonBlockingRead {

    // Holder for temporary store of read(InputStream is) value
    private static String threadValue = "";

    public static void main(String[] args) throws InterruptedException {

        NonBlockingRead test = new NonBlockingRead();

        while (true) {
            String tmp = test.read(System.in, 100);
            if (tmp.length() > 0)
                System.out.println(tmp);
            Thread.sleep(1000);
        }
    }

    /**
     * Non blocking read from input stream using controlled thread
     * 
     * @param is
     *            — InputStream to read
     * @param timeout
     *            — timeout, should not be less that 10
     * @return
     */
    String read(final InputStream is, int timeout) {

        // Start reading bytes from stream in separate thread
        Thread thread = new Thread() {

            public void run() {
                byte[] buffer = new byte[1024]; // read buffer
                byte[] readBytes = new byte[0]; // holder of actually read bytes
                try {
                    Thread.sleep(5);
                    // Read available bytes from stream
                    int size = is.read(buffer);
                    if (size > 0)
                        readBytes = Arrays.copyOf(buffer, size);
                    // and save read value in static variable
                    setValue(new String(readBytes, "UTF-8"));
                } catch (Exception e) {
                    System.err.println("Error reading input stream\nStack trace:\n" + e.getStackTrace());
                }
            }
        };
        thread.start(); // Start thread
        try {
            thread.join(timeout); // and join it with specified timeout
        } catch (InterruptedException e) {
            System.err.println("Data were note read in " + timeout + " ms");
        }
        return getValue();

    }

    private synchronized void setValue(String value) {
        threadValue = value;
    }

    private synchronized String getValue() {
        String tmp = new String(threadValue);
        setValue("");
        return tmp;
    }

}
valdisvi
  • 53
  • 3
  • This is not non-blocking I/O. It is a futile and redundant example of blocking I/O with a timeout, which can already be accomplished via a read timeout and `SocketTimeoutException`. The code is clearly not even tested. `InputStream.read(byte[])` cannot return zero unless the buffer was zero length, which it isn't here. You are not testing for end of stream, nor communicating it back to the original caller. `String` is not a container for potentially binary data. Answer is incorrect in every particular. – user207421 May 11 '18 at 04:04
  • Thanks for pointed logic errors. I fixed them. In general, this code does its purpose to run unit tests in text oriented server sockets. If you will provide more elegant solution, I'll happily use it. – valdisvi Aug 30 '18 at 07:09
  • Nobody can provide code for a problem that embodies a contradicition in terms. – user207421 Sep 11 '18 at 11:04