简体   繁体   中英

DataOutputStream writing too much

What I currently have

I'm currently trying to create a little download manager in Java and I have a problem with writing the loaded bytes in a file. I'm using a DataOutputStream to write the byte-array which I read from a DataInputStream . Here is the class I created to do that:

public class DownloadThread extends Thread{

    private String url_s;
    private File datei;

    public DownloadThread(String url_s, File datei){
        this.url_s = url_s;
        this.datei = datei;
    }

    public void run(){
        // Connect:
        int size = 0;
        URLConnection con = null;
        try {
            URL url = new URL(url_s);
            con = url.openConnection();
            size = con.getContentLength();
            // Set Min and Max of the JProgressBar
            prog.setMinimum(0);
            prog.setMaximum(size);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Download:
        if (con != null || size != 0){
            byte[] buffer = new byte[4096];
            DataInputStream down_reader = null;
            // Output:
            DataOutputStream out = null;
            try {
                out = new DataOutputStream(new FileOutputStream(datei));
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            // Load:
            try {
                down_reader = new DataInputStream(con.getInputStream());
                int byte_counter = 0;
                int tmp = 0;
                int progress = 0;
                // Read:
                while (true){
                    tmp = down_reader.read(buffer);
                    // Check for EOF
                    if (tmp == -1){
                        break;
                    }
                    out.write(buffer);
                    out.flush();
                    // Set Progress:
                    byte_counter += tmp;
                    progress = (byte_counter * 100) / size;
                    prog.setValue( byte_counter );
                    prog.setString(progress+"% - "+byte_counter+"/"+size+" Bytes");
                }
                // Check Filesize:
                prog.setString("Checking Integrity...");
                if (size == out.size()){
                    prog.setString("Integrity Check passed!");
                } else {
                    prog.setString("Integrity Check failed!");
                    System.out.println("Size: "+size+"\n"+
                            "Read: "+byte_counter+"\n"+
                            "Written: "+out.size() );
                }
                // ENDE
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                try {
                    out.close();
                    down_reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        // Clean Up...
        load.setEnabled(true);
        try {
            this.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

This is currently an inner-class and the prog -Object is a JProgressBar from it's mother-class, so it can be accessed directly.

Example

I'm trying to download the Windows .exe Version of the TinyCC, which should be 281KB size. The file i downloaded with my download manager is 376KB big.

The Output from the Script looks like this:

Size: 287181
Read: 287181
Written: 385024

So it seems that the read bytes match the file-size but there are more bytes written. What am I missing here?

This is wrong:

out.write(buffer);

It should be

out.write(buffer, 0, tmp);

You need to specify how many bytes to write, a read doesn't always read a full buffer.

Memorize this. It is the canonical way to copy a stream in Java.

int count;
while ((count = in.read(buffer)) > 0)
{
  out.write(buffer, 0, count);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM