title: Read InputStream and Write to a File
body: <p>This is related to my previous question - <a href="https://stackoverflow.com/questions/23064917/datainputstream-giving-java-io-eofexception/23064989">DataInputStream giving java.io.EOFException</a></p>
  
  <p>In that Client-Server app there is method to retrieve file sent from server and save to a file.</p>
  
  <p>Client.java -</p>
  
  <pre><code> public void receiveFile(InputStream is, String fileName) throws Exception {
          int filesize = 6022386;
          int bytesRead;
          int current = 0;
          byte[] mybytearray = new byte[filesize];
  
          System.out.println("Receving File!");
          FileOutputStream fos = new FileOutputStream("RECEIVED_"+fileName);
          BufferedOutputStream bos = new BufferedOutputStream(fos);
          bytesRead = is.read(mybytearray, 0, mybytearray.length);
          current = bytesRead; 
          System.out.println(bytesRead);
          do {
              bytesRead = is.read(mybytearray, current,
                      (mybytearray.length - current));
              System.out.println(bytesRead);
              if (bytesRead &gt;= 0)
                  current += bytesRead;
          } while (bytesRead &gt; -1);
          System.out.println("Loop done");
          bos.write(mybytearray, 0, current);
          bos.flush();
          bos.close();
      }
  }
  </code></pre>
  
  <p>Server.Java</p>
  
  <pre><code>public void sendFile(OutputStream os, String fileName) throws Exception {
      File myFile = new File(fileName);
      byte[] mybytearray = new byte[(int) myFile.length() + 1];
      FileInputStream fis = new FileInputStream(myFile);
      BufferedInputStream bis = new BufferedInputStream(fis);
      bis.read(mybytearray, 0, mybytearray.length);
      System.out.println("Sending File!");
      os.write(mybytearray, 0, mybytearray.length);
      os.flush();
      bis.close();
  }
  </code></pre>
  
  <p>As you can see there are several stranded outs in client's <code>receiveFile</code> method. here is the output i received.</p>
  
  <p><img src="https://i.stack.imgur.com/kA3lv.png" alt="enter image description here"></p>
  
  <p>The issue is that method don't complete its task and never reached to <code>System.out.println("Loop done");</code></p>
  
  <p>What's the issue ?</p>