Java中的Base64:如何使用修饰模式编写Base64OutputStream类 [英] Java, Base64: How to write a Base64OutputStream class using Decorator Pattern

查看:500
本文介绍了Java中的Base64:如何使用修饰模式编写Base64OutputStream类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

评论有我想知道什么是写一个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());

我的问题是:


  1. 这是一个好的设计?

  2. 能不能做到更好呢?

  3. 我是否正确应用Decorator模式。

  4. 这是好延长FilteredOutputStream代替的OutputStream? 甲骨文表明它是好的,但是否有任何缺点或理由仍然延长OutputStream的?

  5. 而要我另外有装饰的BufferedOutputStream当我调用构造函数?

  1. Is this a good design?
  2. Can it be done better?
  3. Have I applied the decorator pattern correctly.
  4. 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?
  5. And should I additionally decorate it with BufferedOutputStream when I call the constructor?

在此先感谢那些所有的智慧。

Thanks in advance for every ones wisdom.

推荐答案

如果你是适合于使用开源,你可以看看的 Apache的共享codeC 。它包含一个<一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类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆