title: Java. Reading from BufferedInputStream and write to FileOutputStream
body: <p>I'm trying to write client and server side on java. Server side is ok (checked on several clients).</p>
  
  <p>So, the problem is on client side. I allocate memory for <code>bytearray</code>, read from <code>BufferedInputStream</code> and write to that <code>bytearray</code>. Then writing from <code>bytearray</code> to FileOutputStream. Everything is ok but free space of <code>bytearray</code> is filling by NULLs, so received file is not correct(e.g. images).</p>
  
  <p>I found 2 decision of that problem:</p>
  
  <ol>
  <li>Read to <code>bytearray</code> till the end of file(but I don't know where the end of file)</li>
  <li>Read from <code>BufferedInputStream</code> to <code>FileInputStream</code>, but it doesn't work:</li>
  </ol>
  
  <p>I actually need receive header and file. Output header to console and write the file to the disc.</p>
  
  <p>Full Source</p>
  
  <pre><code>public class SClient {
  private static int bufferSize = 8192;
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
      System.out.println("Enter the address:");
      BufferedReader bufferRead = new BufferedReader
                  (new InputStreamReader(System.in));
  
      try {
          String address = bufferRead.readLine();
          System.out.println("Enter the extention of receiving file:");
          String fileExt = bufferRead.readLine();
          // TODO code application logic here
          Socket socket = new Socket(address,4040);
          BufferedInputStream bis = new BufferedInputStream
                  (socket.getInputStream());
  
          BufferedOutputStream bout = new BufferedOutputStream
                  (socket.getOutputStream());
          System.out.println("Enter the request:");
          String message = bufferRead.readLine();// GET /index.html HTTP/1.0
  
          System.out.println("Header read");
          if(message!=null){
              bout.write(message.getBytes());
          }
          FileOutputStream fout = new FileOutputStream("out"+fileExt);
          String s1 = "\r\n\r\n";
          bout.write(s1.getBytes());
          bout.flush();
          System.out.println("Header sent");
  
          byte[] res = new byte[bufferSize];
          int got;
          while((got = bis.read(res))!=-1){
              fout.write(res,0,got);
          }
          fout.close();
          bout.flush();
          socket.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
    }
  }
  </code></pre>
  
  <p>Server side source:</p>
  
  <pre><code>    String endLine = "\r\n";
      File f = new File(fileName);
      FileInputStream fstream;
      fstream = new FileInputStream(f);
      response = "HTTP/1.0 200 OK" + endLine;
      header = "Content-type: "+ contentType + endLine + "Content-length: " + f.length() + endLine + endLine;
      bout.write(response.getBytes());
      bout.write(header.getBytes());
      while(fstream.read(buffer) != -1) {
          bout.write(buffer);
      }
      System.out.println("Message sent");
      bout.flush();
      socket.close();
  </code></pre>