title: Java buffered base64 encoder for streams
body: <p>I have lots of PDF files that I need to get its content encoded using base64. I have an Akka app which fetch the files as stream and distributes to many workers to encode these files and returns the string base64 for each file. I got a basic solution for encoding:</p>
  
  <pre><code>    org.apache.commons.codec.binary.Base64InputStream;
      ...
      Base64InputStream b64IStream = null;
      InputStreamReader reader = null;
      BufferedReader br = null;
      StringBuilder sb = new StringBuilder();
      try {
          b64IStream = new Base64InputStream(input, true);
          reader = new InputStreamReader(b64IStream);
          br = new BufferedReader(reader);
          String line;
          while ((line = br.readLine()) != null) {
              sb.append(line);
          }
      } finally {
          if (b64IStream != null) {
              b64IStream.close();
          }
          if (reader != null) {
              reader.close();
          }
          if (br != null) {
              br.close();
          }
      }
  </code></pre>
  
  <p>It works, but I would like to know what would be the best way that I can encode the files using a buffer and if there is a faster alternative for this. </p>
  
  <p>I tested some other approaches such as:</p>
  
  <ul>
  <li>Base64.getEncoder</li>
  <li>sun.misc.BASE64Encoder</li>
  <li>Base64.encodeBase64</li>
  <li>javax.xml.bind.DatatypeConverter.printBase64</li>
  <li>com.google.guava.BaseEncoding.base64</li>
  </ul>
  
  <p>They are faster but they need the entire file, correct? Also, I do not want to block other threads while encoding 1 PDF file.</p>
  
  <p>Any input is really helpful. Thank you!</p>