title: DataOutputStream writing too much
body: <h2>What I currently have</h2>
  
  <p>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 <code>DataOutputStream</code> to write the byte-array which I read from a <code>DataInputStream</code>. Here is the class I created to do that:</p>
  
  <pre><code>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();
          }
      }
  
  }
  </code></pre>
  
  <p>This is currently an inner-class and the <code>prog</code>-Object is a <code>JProgressBar</code> from it's mother-class, so it can be accessed directly.</p>
  
  <h2>Example</h2>
  
  <p>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.</p>
  
  <p>The Output from the Script looks like this:</p>
  
  <pre><code>Size: 287181
  Read: 287181
  Written: 385024
  </code></pre>
  
  <p>So it seems that the read bytes match the file-size but there are more bytes written. What am I missing here?</p>