title: DataInputStream not getting the right value
body: <p>I'm trying to setup a server-client file transfer where one user sends a file to the server, and the server sends that file to another user (users in this case are instances of the Client.java class).</p>
  <p>From the first client to the server, everything goes as it should, but then the server should send a long, which is the file size, and then send the actual file. The client then should read that long as the file size, and then read and write the file, but with reading the file size something goes wrong.</p>
  <p>Anyone that can spot what goes wrong?</p>
  <p>Client class:</p>
  <pre><code>private void SEND() {
      try {
          Scanner scanner = new Scanner(System.in);
  
          System.out.println(&quot;Please enter the full path of the file:&quot;);
          String path = scanner.nextLine();
  
          LIST();
          System.out.println(&quot;To who do you want to send the file? &quot;);
          String receiver = scanner.nextLine();
          message.setBody(receiver);
  
          message.setHeader(&quot;SEND&quot;);
  
          writer.println(message.getHeader() + &quot; &quot; + message.getUsername() + &quot; &quot; + message.getBody());
  
          int bytes = 0;
          File file = new File(path);
          FileInputStream fileInputStream;
          fileInputStream = new FileInputStream(file);
  
          byte[] byteArray = new byte[(int) file.length()];
          // send file size
          dataOutputStream.writeLong(byteArray.length);
          // break file into chunks
          byte[] buffer = new byte[4 * 1024];
          while ((bytes = fileInputStream.read(buffer)) != -1) {
              dataOutputStream.write(buffer, 0, bytes);
              dataOutputStream.flush();
          }
          fileInputStream.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  public void RECEIVE() {
      try {
          long size = dataInputStream.readLong();
          Scanner scanner = new Scanner(System.in);
          char choice = scanner.next().charAt(0);
  
          while (!(choice == 'y') &amp;&amp; !(choice == 'n')) {
              System.out.println(&quot;incorrect option, please enter y/n&quot;);
              choice = scanner.next().charAt(0);
          }
  
  
          if (choice == 'y') {
              System.out.println(&quot;Downloading file...&quot;);
              int bytes = 0;
              FileOutputStream fileOutputStream = new FileOutputStream(FILE_STORE_PLACE);
              System.out.println(size);
              byte[] buffer = new byte[4 * 1024];
              while (size &gt; 0 &amp;&amp; (bytes = dataInputStream.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
                  fileOutputStream.write(buffer, 0, bytes);
                  size -= bytes;      // read upto file size
              }
              System.out.println(&quot;File received&quot;);
              fileOutputStream.close();
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  <p>Server class:</p>
  <pre><code>// User sends their file to the server, which then reads al its contents and puts them in a file
  public void SEND(String[] messageSplit) {
      try {
          int bytes = 0;
          long size = dataInputStream.readLong();     // read file size
          System.out.println(size);
          byte[] buffer = new byte[4 * 1024];
  
          ClientHandler receiver = findConnectedUser(messageSplit[2]);
  
          promptReceiver(receiver, size);
          receiver.dataOutputStream.writeLong(size);
          receiver.dataOutputStream.flush();
          //write incoming data to the receiver
          while (size &gt; 0 &amp;&amp; (bytes = dataInputStream.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
              receiver.dataOutputStream.write(buffer, 0, bytes);
              size -= bytes;      // read upto file size
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  public void promptReceiver(ClientHandler client, long size) {
      client.writer.println(&quot;would like to send you a file, do you want to receive this file? (&quot; + size + &quot; bytes) (type y/n)&quot;);
  }
  
  public ClientHandler findConnectedUser(String username) {
      for (ClientHandler client : connectedUsers) {
          if (client.getUsername().equals(username)) {
              return client;
          }
      }
      return null;
  }
  </code></pre>