title: Java DataInputStream length
body: <p>I am creating a file server application for school assignment. What I currently have is a simple <code>Client</code> class that sends an image through TCP and a <code>Server</code> class that receives it and writes it to the file.</p>
  
  <p>this is my client code</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  class Client {
      public static void main(String args[]) throws Exception {
          long start = System.currentTimeMillis();
          Socket clientSocket = new Socket("127.0.0.1", 6789);
          DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
  
          File file = new File("hot.jpg");
          FileInputStream fin = new FileInputStream(file);
          byte sendData[] = new byte[(int)file.length()];
          fin.read(sendData);
  
          outToServer.write(sendData, 0, sendData.length);
          clientSocket.close();
  
          long end = System.currentTimeMillis();
          System.out.println("Took " + (end - start) + "ms");
      }
  }
  </code></pre>
  
  <p>and this is my server code.</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  class Server {
      public static void main(String args[]) throws Exception {
          ServerSocket serverSocket = new ServerSocket(6789);
          Socket connectionSocket = serverSocket.accept();
          DataInputStream dis = new DataInputStream(connectionSocket.getInputStream());
  
          byte[] receivedData = new byte[61500]; // &lt;- THIS NUMBER
  
          for(int i = 0; i &lt; receivedData.length; i++)
              receivedData[i] = dis.readByte();
  
          connectionSocket.close();
          serverSocket.close();
  
          FileOutputStream fos = new FileOutputStream("received.jpg");
          fos.write(receivedData);
          fos.close();
      }
  }
  </code></pre>
  
  <p>My question is how to get the size of the file that is being sent. If you check the <code>Server</code> code you'll see that I've hardcoded the number i.e. 61500 at the moment. How can I retrieve this number dynamically?</p>
  
  <p>Or, am I doing this the wrong way? What an alternative solution would be?</p>