title: How to detect if network connection is down while downloading file using InputStream and FileOutputStream in android?
body: <p>I am downloading a file and then writing it into sdcard using following code:</p>
  
  <pre><code> HttpResponse response = httpClient.execute(request);
   HttpEntity entity = response.getEntity();
   InputStream is = entity.getContent();
   File file = new File(Environment.getExternalStorageDirectory()+"/MyProject/data");
   FileOutputStream fos = new FileOutputStream(file);
   int inByte;
   while((inByte = is.read()) != -1){ 
       fos.write(inByte);
       System.out.println("in while loop inByte: "+inByte);
   }
   fos.close();
   is.close();
  </code></pre>
  
  <p>Everything is working fine. Normally it takes 2 minutes to download file. If i take out network cable during downloading then seems like it stays in while loop forever. That code is in try catch block but i do not get any exception. Is there anyway to get out of that while loop when network cable is unplugged. Thanks in advance.</p>