title: end of stream error while downloading file?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/7402500/androidunexpected-end-of-stream-exception-downloading-large-files">Android:&ldquo;Unexpected end of stream&rdquo; exception downloading large files</a>  </p>
  </blockquote>
  
  
  
  <p>I'm downloading a file with the following code below. The file is approx. 5MB in size. However, I'm getting a java.io.IOException "unexpected end of stream" error when the download is around 60-90%</p>
  
  <p>I don't understand how to solve it and it's driving me crazy.</p>
  
  <p><strong>EDIT:</strong> Could someone at least just test it, if it downloads a file successfully on your phone. This will allow me to determine whether the problem is my phone or the code.</p>
  
  <pre><code>try {
      URL url = new URL(full_url);
      conexion = (URLConnection)url.openConnection();
  
      conexion.setReadTimeout(20000);
      conexion.connect();
      File file = new File(root.getAbsolutePath()+"/", fileName);
  
      int lenghtOfFile = conexion.getContentLength();
      System.out.println("content-length-header is: " + lenghtOfFile);
      InputStream input = conexion.getInputStream();
  
      OutputStream output = new FileOutputStream(file);
  
      byte data[] = new byte[8192];
      long total = 0;
      contentView.setTextViewText(R.id.status_text,"Downloading file " + (78 - GlobalData.missingFiles.size()) + " of " + 77);  
      int downloadProgress = (int)((total*100)/lenghtOfFile);
  
      int lastProgressUpdate=0;
  
      while ((count = input.read(data)) != -1) {
          System.out.println("available bytes:" + input.available());
          total += count;
  
          downloadProgress = (int)((total*100)/lenghtOfFile);
          Log.e("totaltotal","" + (int)((total*100)/lenghtOfFile));
  
          output.write(data,0,count);
          if(downloadProgress%20==0 &amp;&amp; downloadProgress != lastProgressUpdate) {
              notification.contentView.setProgressBar(R.id.status_progress, 100,downloadProgress, false);
              notificationManager.notify(1,notification);
              lastProgressUpdate=downloadProgress;    
          } 
          if(downloadProgress == 100){
              GlobalData.downloadFiles.add("" +fileName);
              GlobalData.missingFiles.remove(fileName);
          }
      }
  
      output.flush();
      output.close();
      input.close();
  
      if(downloadProgress != 100){
          File temp_file = new File(root.getAbsolutePath()+"/", fileName);
          try{
              if(temp_file.exists()){
                  boolean del_main = temp_file.delete();
                  Log.e("File","Does file exists: " + del_main);
                  Log.e("FilePath","PATH: " + temp_file.getAbsolutePath());
              }else{
                  Log.e("File Exists NOT","NOT EXISTING");
              }
          }catch(Exception e){
              Log.e("FileDelete","deleting is giving problems");
          }
      }
  } catch (Exception e) {
      Log.e("PRINTSTACK","STACK:" + e.getMessage());
      e.printStackTrace();
      System.out.println("Downloading didn't work");
  
      killService();
  }
  </code></pre>