wood burning stoves*
The moose likes Java in General and the fly likes Program hangs when closing InputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Program hangs when closing InputStream" Watch "Program hangs when closing InputStream" New topic
Author

Program hangs when closing InputStream

Ben Cho
Greenhorn

Joined: May 22, 2003
Posts: 2
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

Joined: Sep 29, 2000
Posts: 8521
Did you try flush() to clear the pipe first?


"JavaRanch, where the deer and the Certified play" - David O'Meara
Ben Cho
Greenhorn

Joined: May 22, 2003
Posts: 2
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Program hangs when closing InputStream
 
Similar Threads
Runtime.exec() file problem
serial connection w/ palm
A challenge for all of you
Parsing with DOM "hangs": inputstream problem
Problems reading from a .txt file