I have a simple client - server app that downloads files thru a REST API. As I use basic http auth the files are sent via ssl.
//my download loop
HttpsURLConnection conn;
//some handling of response codes and content-length
InputStream s = conn.getInputStream();
while (true) {
i = br.read(buffer); //if server shuts down, I stall here
if (i == -1)
break;
downloadedSize += i;
fis.write(buffer, 0, i);
}
This all works fine and without issues until the code encounters a situation where the server shuts down a file transfer. At that time the code simply stalls and I have a useless thread.
My question is how to detect that a server has been shut down? In Wireshark I see a RST signal sent, but no exceptions are raised. This seems to me to be a fairly common task and yet I havent found a straightforward answer to it.
Currently Im using a scheduled task, that checks if there was any progres in downloading every now and then. If it detects a problem then it creates a new workerThread and tells my communication service to use that one.
This is a very ugly solution as threads then can end up orphaned.
I tried using available() to peek if there is any data available, but as this is SSL, it always returns 0.
InputStreamin aBufferedStreamReaderand use theready()method, or (b) go up throughconnto get the socket and use thegetSoTimeout()method. Can't vouch for the effectiveness of either though.