title: Java, Base64: How to write a Base64OutputStream class using Decorator Pattern
body: <p>Based on my original <a href="https://stackoverflow.com/questions/8834667/rsa-and-base64-encoding-too-many-bytes">question about RSA and Base64 encoding</a> and the comments there I am wondering what is the best approach to write a Base64OutputStream (or Input Stream). I originally had called it Base64PrintWriter and extended it from PrintWriter passing a PrintWriter to the Constructor.</p>
  
  <pre><code>import org.bouncycastle.util.encoders.Base64;
  
  public class Base64OutputStream extends FilterOutputStream {
  
      public Base64OutputStream(OutputStream out) {
          super(out);
      }
  
      public void write(byte[] decodedBytes) throws IOException {
          byte[] base64Message = Base64.encode(decodedBytes);
          super.write(base64Message);
      }
  
      public void writeln(byte[] decodedBytes) throws IOException {
          write(decodedBytes);
          super.write("\n".getBytes());
          super.flush();
      }
  }
  </code></pre>
  
  <p>I changed my original implementation to the one above and I initialize it with:</p>
  
  <pre><code>Base64OutputStream base64encoder = new Base64OutputStream(socket.getOutputStream());
  </code></pre>
  
  <p>My questions are: </p>
  
  <ol>
  <li>Is this a good design?</li>
  <li>Can it be done better?</li>
  <li>Have I applied the decorator pattern correctly.</li>
  <li>Is it good to extend FilteredOutputStream instead of OutputStream? <a href="http://java.sun.com/developer/technicalArticles/Streams/WritingIOSC/" rel="nofollow noreferrer">Oracle</a> indicates it is good but are there any disadvantages or reasons to still extend from OutputStream?</li>
  <li>And should I additionally decorate it with BufferedOutputStream when I call the constructor?</li>
  </ol>