基于我最初关于RSA和Base64编码的问题以及评论,我想知道编写Base64OutputStream(或输入流)的最佳方法是什么。我最初将其称为Base64PrintWriter,并将其从PrintWriter扩展为传递PrintWriter到构造函数。
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(); } }
我将原来的实现更改为上述实现,并使用以下代码对其进行了初始化:
Base64OutputStream base64encoder = new Base64OutputStream(socket.getOutputStream());
我的问题是:
如果您愿意使用开放源代码,可以查看Apache Commons Codec。它包含应该满足您需求的Base64OutputStream。