title: How to determine the buffer size for BufferedOutputStream's write method
body: <p>I am trying to copy a file using the following code:</p>
  
  <p>1:</p>
  
  <pre><code>        int data=0;
          byte[] buffer = new byte[4096];
          while((data = bufferedInputStream.read())!=-1){
              bufferedOutputStream.write(data);
          }
  </code></pre>
  
  <p>2:</p>
  
  <pre><code>        byte[] buffer = new byte[4096];
          while(bufferedInputStream.read(buffer)!=-1){
              bufferedOutputStream.write(buffer);
          }
  </code></pre>
  
  <p>Actual size of file is 3892028 bytes(on windows). The file will be uploaded by the user thro struts2 fileupload. Uploaded file size is exactly same as that of windows. When I try to copy the uploaded file from temporary folder, the copied file varies in size and the time taken also varies(it is negligible). Please find the below readings.</p>
  
  <pre><code>Without using buffer(Code 1)
  Time taken 77
  3892028
  3891200
  
  Buffer size 1024(Code 2)
  Time taken 17
  3892028
  3891200
  
  Buffer size 4096(Code 2)
  Time taken 18
  3892028
  3891200
  
  Buffer size 10240(Code 2)
  Time taken 14
  3892028
  3901440
  
  Buffer size 102400(Code 2)
  Time taken 9
  3892028
  3993600
  </code></pre>
  
  <p>If I increase the buffer size further, time taken increases, again it is negligible. So my questions are, </p>
  
  <ol>
  <li>Why the file size changes?</li>
  <li>Is there any subtle consequences due to this size variation? </li>
  <li>What is the best way to accomplish this functionality(copying a file)? </li>
  </ol>
  
  <p>I don't know what is going beneath? Thanks for any suggestion.
  <strong>Edit:</strong> I have flush() and close() method calls.
  <strong>Note:</strong> I have trimmed my code to make it simpler.</p>