This discussion is archived
8 Replies Latest reply: Jun 3, 2008 9:16 PM by 807591 RSS

I add a timeout for InputStream read().

807591 Newbie
Currently Being Moderated
I am looking for a timeout for InputStream read() since Java doesn't support it. I use a Timer class to handle the timeout value and put the InputStream.read() into another thread. When the time is up, the Timer object will stop the thread of reading. What do you think about this solution? Do you have any other better solution?
thanks
  • 1. Re: I add a timeout for InputStream read().
    EJP Guru
    Currently Being Moderated
    I am looking for a timeout for InputStream read() since Java doesn't support it.
    Yes it does. The only time you need a timeout is when reading from a network. Java supports read timeouts for both Sockets and HttpURLConnections. See the Javadoc. You don't need all this Timer stuff, or a separate thread; Thread.stop() is strongly deprecated for very good reasons, if that's what you were planning to use, and there isn't any other way to stop the thread, if it isn't.
  • 2. Re: I add a timeout for InputStream read().
    807591 Newbie
    Currently Being Moderated
    I would try something like:
    public void doRead() throws IOException {  
      int ch = 0;
      while( (ch = is.read()) != -1 && keepReading) {
        // do your thing
      }
      is.close();
    }
    In the timer task set the keepReading to false when you want it to stop. This way you are not doing any extra threading to read then killing off the thread when you want to stop reading.

    Edited by: avalanche333 on Mar 26, 2008 11:23 PM

    Edited by: avalanche333 on Mar 26, 2008 11:25 PM
  • 3. Re: I add a timeout for InputStream read().
    EJP Guru
    Currently Being Moderated
    I would try something like:
    I would try something like reading the thread before you make things worse. And your solution doesn't solve the problem any more than the OP's suggestion, because the read() will still block forever unless a read timeout is set or in some cases if the thread is interrupted.
  • 4. Re: I add a timeout for InputStream read().
    807591 Newbie
    Currently Being Moderated
    ejp wrote:
    I would try something like:
    I would try something like reading the thread before you make things worse. And your solution doesn't solve the problem any more than the OP's suggestion, because the read() will still block forever unless a read timeout is set or in some cases if the thread is interrupted.
    yeah. ejp is right.
  • 5. Re: I add a timeout for InputStream read().
    807591 Newbie
    Currently Being Moderated
    Whoops I was under the impression that read() would read until data was no longer available or it throws an exception. Didn't realize it could just block and not do anything.
  • 6. Re: I add a timeout for InputStream read().
    807591 Newbie
    Currently Being Moderated
    and any ideas on how to stop this blocking read() method from an InputStream (Java 5)???? When googling on this topic I find an incredible amount of questions on this subject BUT no solutions. What to do?? I'm a little bit affraid it comes down to hacking the JVM finding the Thread in some memory block and removing it with brute force.



    hmmm when I think of it, it's really driving my crazy.... the only thing I can think of is throwing my PC out of the window and buy a new one. Unfortunately there's no budget for that it will cost to many PCs :-)

    Edited by: R_e_n_s on Jun 3, 2008 6:45 AM
  • 7. Re: I add a timeout for InputStream read().
    EJP Guru
    Currently Being Moderated
    and any ideas on how to stop this blocking read() method from an InputStream (Java 5)????
    Err, yes, the answer was posted on 27/03/2008 13:28 (reply 1 of 6). It's about a foot further up the page.
    hmmm when I think of it, it's really driving my crazy....
    People who don't even read the threads they're posting to are already crazy if you ask me, but there seem to be an awful lot of them out there.
  • 8. Re: I add a timeout for InputStream read().
    807591 Newbie
    Currently Being Moderated
    R_e_n_s wrote:
    and any ideas on how to stop this blocking read() method from an InputStream (Java 5)???? When googling on this topic I find an incredible amount of questions on this subject BUT no solutions.
    Wrong.
    What to do??
    Try reading.
    I'm a little bit affraid it comes down to hacking the JVM finding the Thread in some memory block and removing it with brute force.
    Jeebus.