title: A better Java loop?
body: <p>I have a piece of code to read a <code>InputStream</code> and write the content into an <code>OutputStream</code>:</p>
  
  <pre><code>BufferedInputStream in = new BufferedInputStream(...);
  FileOutputStream outStream = new FileOutputStream outStream(...);
  
  int read = in.read(buffer, 0, bufferSize);
  while (read != -1) {
      outStream.write(buffer, 0, read);
      read = in.read(buffer, 0, bufferSize);
  }
  </code></pre>
  
  <p>It works, but i don't like it since the variable <code>read</code> is declared out of the loop, and <code>read()</code> method is written twice.
  The revised version:</p>
  
  <pre><code> for (int read = 0; read != -1; read = in.read(buffer, 0, bufferSize)) {
        outStream.write(buffer, 0, read);
   }
  </code></pre>
  
  <p>It looks better but not good enough because the first iteration is useless (and maybe harmful) with read=0.</p>
  
  <p>Do you have a better solution?</p>