• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Timeout for InputStream

 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible somehow to get an input stream from an HttpURLConnection
object to timeout on a read() operation?
How can I do it?

TIA,
- Manish
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, as far as I know, a blocking read() blocks forever, if there is no data available, but the end of stream has not been reached. You can't have a time-out. You can't interrupt it. Closing the stream, from another thread, might work for some types of stream, but if it does work, it's an undocumented and unreliable feature. Rubbish, ain't it?!
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, HttpUrlConnection is maddening that way. Some ideas ...

The Apache Commons HTTPClient package has a compatible class with timeout.

Pass a reference to the HttpUrlConnection to a Timer task. When the timer task fires, make it close the connection. That will cause an exception in the original thread. If you're not familiar with that kind of threading just holler - we can provide some more detail.

Using Timer and your own threads is discouraged in EJB servers. I made an HTTPSocket class that extends HttpUrlConnection (because there's no interface, dang it!) and overrode the methods I needed to implement the HTTP protocol. Socket has a timeout parameter. Implementing HTTP turned out to be no fun, and I didn't have the nerve to put the thing in production.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!