title: The server is sending data through the socket, but the client is not receving it (Java)
body: <p>I am writing a file storage and transfer system using Java. Here's the code on the client side to receive a file:</p>
  
  <pre><code>public static void receiveFile(Socket socket) throws IOException{   
      String fileLocation="/home/limafoxtrottango/Downloads/receivedFile";
      int bytesRead=0;
      int current = 0;
      FileOutputStream fileOutputStream = null;
      BufferedOutputStream bufferedOutputStream = null;
  
      try {
          // receive file
          byte [] byteArray  = new byte [60022386];
          System.out.println("Waiting to receive a file...");
          //reading file from socket
          InputStream inputStream = socket.getInputStream();
          fileOutputStream = new FileOutputStream(fileLocation);
          bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
          bytesRead = inputStream.read(byteArray,0,byteArray.length);                 //copying file from socket to byteArray
          current = bytesRead;
          do {
              bytesRead =inputStream.read(byteArray, current, (byteArray.length-current));
              if(bytesRead &gt;= 0) current += bytesRead;
          } while(bytesRead &gt; -1);
          bufferedOutputStream.write(byteArray, 0 , current);                         //writing byteArray to file
          bufferedOutputStream.flush();                                               //flushing buffers
  
          System.out.println("File " + fileLocation  + " downloaded ( size: " + current + " bytes read)");
      } catch(SocketException e){
          System.out.println("Some error occured");
      }
      catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      finally {
          if (fileOutputStream != null) fileOutputStream.close();
          if (bufferedOutputStream != null) bufferedOutputStream.close();
          if (socket != null) socket.close();
      }
  }
  </code></pre>
  
  <p>While receiving a file, I get the following error:</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
      at java.lang.System.arraycopy(Native Method)
      at java.io.BufferedOutputStream.write(BufferedOutputStream.java:128)
      at Test.receiveFile(Test.java:211)
      at Test.main(Test.java:70)
  </code></pre>
  
  <p>Note: The error is in the following line of the code:</p>
  
  <pre><code>bufferedOutputStream.write(byteArray, 0 , current);  
  </code></pre>
  
  <p>After debugging, I found-out that the client does not have any data in it's input stream, and hence, the read() method always returns -1 (eof). But the server is sending the file successfully.</p>
  
  <p>Here is the code for the server:</p>
  
  <pre><code>public static void sendFile(Socket socket, String fileLocation)
  {
      FileInputStream fileInputStream = null;
      BufferedInputStream bufferedInputStream = null;
      OutputStream outputStream = null;
      File file = new File (fileLocation);
      byte [] byteArray  = new byte [(int)file.length()];
      try {
          socket=new Socket(socket.getInetAddress(),port_no);
          fileInputStream = new FileInputStream(file);
          bufferedInputStream = new BufferedInputStream(fileInputStream);
          bufferedInputStream.read(byteArray,0,byteArray.length); // copied file into byteArray
  
          //sending file through socket
          outputStream = socket.getOutputStream();
          System.out.println("Sending " + fileLocation + "( size: " + byteArray.length + " bytes)");
          outputStream.write(byteArray,0,byteArray.length);           //copying byteArray to socket
          outputStream.flush();                                       //flushing socket
          System.out.println("Done sending!");    
      } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>And here is my call to the above method:</p>
  
  <pre><code>sendFile(clientSocket, "/home/limafoxtrottango/Downloads/serverDownloads/"+sender);
  </code></pre>
  
  <p>The thing is that the server is successfully writing the byte into the stream, but the client doesn't seem to have any data in it's input stream. </p>