Forums Register Login

Why is exception not thrown when the connection is terminated?

+Pie Number of slices to send: Send
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!

+Pie Number of slices to send: Send
Welcome to CodeRanch, Sanjay!

You should read the contract for FileChannel.transferFrom().
+Pie Number of slices to send: Send
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.
Paper beats rock. Scissors beats tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 676 times.
Similar Threads
Why Firefox is four times faster to download zip file then my own Java program?
JSSE and NIO
Editing a .jar archive in a Java application.
How to download a Zip file
Problem with code after first execution
Thread Boost feature
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:24:24.