Can I set a timeout for a InputStream's read() function?

Not on the InputStream generally, but you can use Socket#setSoTimeout(int) to set a timeout for all read operations on the socket itself.

Ah thanks, I missed that one. My only follow up question is this: this time appears to count for each time read() is called, which may be multiple times. Is there a way I can set the entire timeout time such that it it will timeout if the sum of each read() exceeds the timeout?

– Zombies Apr 14 '10 at 15:27 Not automatically, but you can set a shorter S0-timeout than your real timeout and check in a loop if you've exceeded your total allowed run time. – jarnbjo Apr 14 '10 at 16:58 1 Be careful with using a socket timeout and DataInputStream together (and also a BufferredInputStream). These will buffer or read some data into temporary memory (like in getLong()) and then if the timeout occurs (waiting on more data from the socket) you will lose any of the previously read data and there is no way to recover.

SocketTimeoutException extends InterruptedIOException which provides the number of bytes successfully transferred prior to the timeout. With a raw socket you can use this to retry/continue (perhaps to flush the bad message). – Kevin Brock Apr 15 '10 at 3:32 @Zombies: probably better to create another question for your additional request.

– Kevin Brock Apr 15 '10 at 3:34.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions