title: Issue with file transfer using Java sockets and DataStreams
body: <p>I've been working on a chat client for myself and some friends, and decided to attempt to add functionality to allow file transfers between clients. I am able to send a file, but it arrives in a different state than the file sent. As an example, here is a link comparing the before and after of an image sent:</p>
  
  <p><a href="http://i.imgur.com/qbMo0gy.png" rel="nofollow">http://i.imgur.com/qbMo0gy.png</a></p>
  
  <p>Don't ask why I used that picture, it's just the first one I found in my pictures folder...
  And here are the send and receive methods:</p>
  
  <pre><code>public static Runnable sendFile(String target, File file, Client client) {
              Runnable run = () -&gt; {
                      FileOutputStream fileOut = null;
                      try {
                              int total = 0;
                              int count;
                              byte[] fileBuffer = new byte[8192];
                              BufferedInputStream fileStream = new BufferedInputStream(new FileInputStream(file));
                              client.getTextOutData().writeUTF("*!sendfile: " + client.getClientName() + " " + target + " " +                                 file.getName() + " " + (int) file.length());
                              fileOut = new FileOutputStream(new File(downloadsPath + "/" + file.getName()));
                              System.out.println("Send length: " + file.length());
                              while ((count = fileStream.read(fileBuffer, 0, (int) Math.min(8192, file.length() - total))) ==         8192) {
                                      total += count;
                                      System.out.println("Sender count: " + count);
                                      client.getDLOutData().write(fileBuffer, 0, count);
                                      client.getDLOutData().flush();
                                      fileOut.write(fileBuffer, 0, count);
                              }
                              total += count;
                              System.out.println("Sender count: " + count);
                              System.out.println("Sender total: " + total);
                              //client.getDLOutData().write(fileBuffer, 0, count);
                              fileOut.write(fileBuffer, 0, count);
  
                              System.out.println("Sending done, eh?");
                              fileStream.close();
                              Thread.currentThread().interrupt();
                      } catch (Exception e) {
                              Platform.runLater(() -&gt;Popups.startInfoDlg("Download Error", "Failed to send file!"));
                              e.printStackTrace();
                      }
              };
  
              return run;
  
      }
  
  public static Runnable dlFile(MainScreenController sc, File file, long length) {
              Runnable run = () -&gt; {
                      FileOutputStream fileOut = null;
                      try {
                              int total = 0;
                              int count;
                              byte[] fileBuffer = new byte[8192];
                              fileOut = new FileOutputStream(file);
                              System.out.println("DL length: " + length);
                              while ((count = sc.getClient().getDLInData().read(fileBuffer, 0, (int) Math.min(8192, length - total))) == 8192){
                                      total += count;
                                      System.out.println("DL count: " + count);
                                      fileOut.write(fileBuffer, 0, count);
                              }
                              total += count;
                              System.out.println("DL count: " + count);
                              System.out.println("DL total: " + total);
                              //fileOut.write(fileBuffer, 0, count);
                      } catch (IOException e) {
                              System.out.println("Failed to download file.");
                              e.printStackTrace();
                      } finally {
                              try {
                                      fileOut.flush();
                                      fileOut.close();
                              } catch (IOException e) {
                                      e.printStackTrace();
                              }
                              System.out.println("Download complete.");
                              Thread.currentThread().interrupt();
                      }
              };
  
              return run;
  
      }
  </code></pre>
  
  <p>If you have any ideas as to what the problem is, I'd love to hear them. I can also try and set up a Git for the entire project if you need to see more to help. Any help is appreciated, I'm still just beginning to learn java.</p>