问题描述
评论有我想知道什么是写一个Base64OutputStream(或输入流)的最佳方法。我原本把它称为Base64PrintWriter和PrintWriter的扩展它传递一个PrintWriter到构造函数。
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());
我的问题是:
- 这是一个好的设计?
- 能不能做到更好呢?
- 我是否正确应用Decorator模式。
- 这是好延长FilteredOutputStream代替的OutputStream? 表明它是好的,但是否有任何缺点或理由仍然延长OutputStream的?
- 而要我另外有装饰的BufferedOutputStream当我调用构造函数?
- 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?
在此先感谢那些所有的智慧。
Thanks in advance for every ones wisdom.
推荐答案
如果你是适合于使用开源,你可以看看的。它包含一个<一href=\"http://commons.apache.org/$c$cc/api-release/org/apache/commons/$c$cc/binary/Base64OutputStream.html\"相对=nofollow> Base64OutputStream应当能够满足您需求
If you are amenable to using open source, you can look at Apache Commons Codec. It contains a Base64OutputStream that should meet you needs.
这篇关于Java中的Base64:如何使用修饰模式编写Base64OutputStream类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!