Re: Timeout for InputStream

Discussion in 'Java' started by Filip Larsen, Nov 2, 2004.

  1. Filip Larsen

    Filip Larsen Guest

    Manish Hatwalne wrote

    > Is it possible somehow to get an input stream from an

    HttpURLConnection
    > object to timeout on a read() operation? How can I do it?


    Recently I had to do the same in a way that did not require major change
    to an existing input stream pipeline, and I came up with the
    PolledInputStream class included below. You may be able to modify it to
    suit your needs.

    Note: to work it relies on the underlying inputstream being able to
    report available() > 0 when there is input, which do not necessarily
    hold for all InputStreams. However, it do seem to hold for Sun's
    HttpURLConnection input streams as far as I have been able to test.

    ====PolledInputStream.java====

    import java.io.IOException;
    import java.io.InputStream;

    /**
    * Adapter to change blocking input into polled input with timeout.
    *
    * @author Filip Larsen
    */
    public class PolledInputStream extends InputStream {

    public PolledInputStream(InputStream input, long timeout) {
    this.input = input;
    this.timeout = timeout;
    }

    public int available() throws IOException {
    return input.available();
    }

    public void close() throws IOException {
    input.close();
    }

    public void mark(int readlimit) {
    input.mark(readlimit);
    }

    public boolean markSupported() {
    return input.markSupported();
    }

    public int read() throws IOException {
    waitForAvailable();
    return input.read();
    }

    public int read(byte[] b) throws IOException {
    return read(b,0,b.length);
    }

    public int read(byte[] b, int off, int len) throws IOException {
    waitForAvailable();
    int n = available();
    return input.read(b, off, Math.min(len,n));
    }

    public void reset() throws IOException {
    input.reset();
    }

    public long skip(long n) throws IOException {
    return input.skip(n);
    }


    private InputStream input;
    private long timeout;

    private void sleep(long time) {
    try {
    Thread.sleep(Math.max(0,time));
    } catch (InterruptedException ignore) {
    }
    }

    private void waitForAvailable() throws IOException {
    long until = System.currentTimeMillis() + timeout;
    while (available() == 0) {
    if (System.currentTimeMillis() > until) {
    throw new IOException("input timed out");
    }
    try {
    Thread.sleep(100);
    } catch (InterruptedException ignore) {
    }
    }
    }


    }

    ====End of PolledInputStream.java====


    Regards,
    --
    Filip Larsen
    Filip Larsen, Nov 2, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Johnson
    Replies:
    0
    Views:
    3,738
    Bob Johnson
    Aug 7, 2003
  2. R
    Replies:
    5
    Views:
    2,103
    Kevin McMurtrie
    Mar 13, 2005
  3. Replies:
    0
    Views:
    1,416
  4. Knute Johnson
    Replies:
    7
    Views:
    626
    Gilbert
    Aug 18, 2009
  5. Mark Probert

    Timeout::timeout and Socket timeout

    Mark Probert, Oct 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    1,271
    Brian Candler
    Oct 6, 2004
Loading...

Share This Page