Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Program hangs when closing InputStream

 
Ben Cho
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I've come across a problem closing InputStreams, and would appreciate any insight.
First of all, I'm trying to gather weather information from the Government NWS website. I'm able to establish a connection and read the data, but for some reason, my program hangs when it tries to close the input stream and buffered reader once the program is done executing. The weird part is that no exception is thrown.
Here's a piece of my code:
try
{
URL urlObj = new URL(m_hostName);
URLConnection m_urlConn = urlObj.openConnection();
InputStream input = m_urlConn.getInputStream();

BufferedReader fromServer = new BufferedReader newInputStreamReader(input));
while (blah)
{
// this loop retrieves data and parses it
..................
..................
..................
}
// when done executing, close all
fromServer.close();
input.close();
}
catch (Exception e)
{
logger.error("Error occurred in "+ this.getClass().getName() + ": " + e);
}
There is more to it, but I believe that's the gist of the program. I've also tried placing the close() methods in a 'finally' clause, but I still get the same results. In addition, this program is run as a thread, which constantly executes every minute.
If I comment out those close() methods, the program runs fine. Is it bad to not close those input streams when the thread is done executing?
Thanks for your help,
Ben
[ May 22, 2003: Message edited by: Ben Cho ]
[ May 22, 2003: Message edited by: Ben Cho ]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try flush() to clear the pipe first?
 
Ben Cho
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try flush() to clear the pipe first?

Thanks for the suggestion Cindy. I believe flush() only works with OutputStreams such as OutputStream and and BufferedOutputStream. Since I'm reading directly off the website and not writing to it, I'm not sure if I could use the flush() method in this case.
[ May 22, 2003: Message edited by: Ben Cho ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic