Based on my original question about RSA and Base64 encoding 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.
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();
}
}
I changed my original implementation to the one above and I initialize it with:
Base64OutputStream base64encoder = new Base64OutputStream(socket.getOutputStream());
My questions are:
- Is this a good design?
- Can it be done better?
- Have I applied the decorator pattern correctly.
- Is it good to extend FilteredOutputStream instead of OutputStream? Oracle indicates it is good but are there any disadvantages or reasons to still extend from OutputStream?
- And should I additionally decorate it with BufferedOutputStream when I call the constructor?
If you are amenable to using open source, you can look at Apache Commons Codec. It contains a Base64OutputStream that should meet you needs.