Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

InputStream & timeout  RSS feed

 
Rob Chung
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to set timeout on InputStream. After some search I have the following:
import java.io.InputStream;
import java.io.IOException;
public class TimeoutInputStream extends InputStream {
InputStream is;
int timeout;
public TimeoutInputStream(InputStream is, int timeout) {
this.is = is;
this.timeout = timeout;
}
public int read() throws IOException {
long starttime = System.currentTimeMillis();
while(is.available() < 1) {
long time = System.currentTimeMillis();
if((time - starttime) > timeout) throw new IOException("Timeout");
}
return is.read();
}
}
I think I'll need to override available() too. Is there any other better way?
Thanks in advance.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're working with Sockets, you may find that the socket timeout setting is sufficient to do whatever it is that you need to do.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!