title: Best way to read an input stream to a buffer
body: <p>For reading any input stream to a buffer there are two methods. Can someone help me understand which is the better method and why? And in which situation we should use each method?</p>
  
  <ul>
  <li>Reading line by line and appending it to the buffer.</li>
  </ul>
  
  <p>Eg:</p>
  
  <pre><code>public String fileToBuffer(InputStream is, StringBuffer strBuffer) throws IOException{
      StringBuffer buffer = strBuffer;
      InputStreamReader isr = null;
  
      try {
          isr = new InputStreamReader(is);
          BufferedReader br = new BufferedReader(isr);
          String line = null;
  
          while ((line = br.readLine()) != null) {
              buffer.append(line + "\n");
          }
      } finally {
          if (is != null) {
              is.close();
          }
          if (isr != null) {
              isr.close();
          }
      }
  
      return buffer.toString();
  }
  </code></pre>
  
  <hr>
  
  <ul>
  <li>Reading up to buffer size ie 1024 bytes in a char array.</li>
  </ul>
  
  <p>Eg:</p>
  
  <pre><code>InputStreamReader isr = new InputStreamReader(is);
  final int bufferSize = 1024;
  char[] buffer = new char[bufferSize];
  StringBuffer strBuffer = new StringBuffer();
  
  /* read the base script into string buffer */
  try {
      while (true) {
          int read = isr.read(buffer, 0, bufferSize);
          if (read == -1) {
             break;
          }
  
          strBuffer.append(buffer, 0, read);
       }
  } catch (IOException e) {
  }
  </code></pre>