File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Why is exception not thrown when the connection is terminated? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why is exception not thrown when the connection is terminated?" Watch "Why is exception not thrown when the connection is terminated?" New topic
Author

Why is exception not thrown when the connection is terminated?

Sanjay Giri
Greenhorn

Joined: Jul 27, 2014
Posts: 1
hello everyone,

I am here to solve an issue i am facing. I hope to get some help from the experts.

Below is a code of a simple file downloader in java. I want to detect an exception when the connection is broken in the middle of downloading a file. Now, I run the code and in the middle of downloading the file, i turn off my wifi with keyboard wifi off button. Doing this the program hangs forever without throwing any exceptions and without terminating. It seems it blocks forever. Firstly my confusion is why is exception not thrown? Secondly, in the following code you can see this line //con.setReadTimeout(2000); which is currently commented out. Removing the comment and running the program, now if connection breaks in the middle by turning off wifi then it waits for 2 seconds and then if it cannot read then it terminates without throwing exception. So, again in this case why is it just terminating and not throwing any exception? I am very puzzled by this behavior. I hope i can get some help. Thank you very much!

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Welcome to CodeRanch, Sanjay!

You should read the contract for FileChannel.transferFrom().
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
I recommend using con.getContentLength() to know how many bytes to expect, and then put the transferFrom() call in a loop, counting how many bytes have been transferred each time (see return value of transferFrom()), and stopping only when the expected number of bytes is reached.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why is exception not thrown when the connection is terminated?