java - 从 BufferedReader 获取 OutputStream

标签 java iostream

我有两个对象:

BufferedReader br;
CMSSignedData cms;

我必须将cms内容写入BufferedReader cms有这个方法:

 cms.getSignedContent().write(OutputStream os); 

但是如何获得 OutputStream来自BufferedReader

这是我的尝试:

        ByteArrayOutputStream os=new ByteArrayOutputStream();
        cms.getSignedContent().write(os);
        InputStream is=new ByteArrayInputStream(os.toByteArray());  
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

但我认为这不是最好的方法。

最佳答案

我认为您想要的是管道输入/输出流。

PipedOutputStream os = new PipedOutputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(new PipedInputStream(os)));
cms.getSignedContent().write(os);

但是请注意piet.t 对线程的评论。

以及查看相关 API 文档:

关于java - 从 BufferedReader 获取 OutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20399502/

相关文章:

C++:将流中的小数解析为 uint8_t

java - 控制 2d 游戏的动画速度

java - 使用我自己的 hashcode 作为 hashmap java

c++ - 我对 c++ istream 赋值很好奇

c++ - 在 C++ 中是否允许在 iostream 之后编写任何内容

c++ - 使用 iostream 打印 CComBSTR (std::wcout)

java - 如何在 Eclipse 项目中使用 JSTL?

java - 在 java 程序中发出 STARTTLS 命令后,startHandshake 出现不支持或无法识别的 ssl 消息异常

java - 如何在微调器中获取国家/地区名称和国家/地区调用 (ISD) 代码

c# - "The process cannot access the file because it is being used by another process "