1

Possible Duplicate:
Android:“Unexpected end of stream” exception downloading large files

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%

I don't understand how to solve it and it's driving me crazy.

EDIT: 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.

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 && 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();
}
Community
  • 1
  • 1
bytebiscuit
  • 3,446
  • 10
  • 33
  • 53

1 Answers1

1

For some reason the input stream is closing prematurely. Often this is since an inputstream is read mulitple times at once, but I'm guessing this is not the case here, even though you for some reason have conexion available in a wider scope than we see here.

You say conexion = (URLConnection)url.openConnection(); instead of the expected URLConnection conexion = url.openConnection();. Please make sure you do not try to fetch the input stream twice.

The only other thing I can see that is weird when I look is that you use the inputstream from conexion.getInputStream() directly. Try wrapping it in a buffered input stream as such:

InputStream input = new BufferedInputStream(connexion.getInputStream());
Sebastian Olsson
  • 836
  • 6
  • 10