如何将OutputStream转换为InputStream? [英] How to convert OutputStream to InputStream?

查看:1833
本文介绍了如何将OutputStream转换为InputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正处于开发阶段,我有两个模块,其中一个输出为 OutputStream ,第二个只接受的InputStream 。你知道如何将 OutputStream 转换为 InputStream (反之亦然,我的意思是这样)我将成为能够连接这两个部分吗?

I am on the stage of development, where I have two modules and from one I got output as a OutputStream and second one, which accepts only InputStream. Do you know how to convert OutputStream to InputStream (not vice versa, I mean really this way) that I will be able to connect these two parts?

谢谢

推荐答案

OutputStream 是您将数据写入的地方。如果某个模块公开 OutputStream ,则期望在另一端读取一些内容。

An OutputStream is one where you write data to. If some module exposes an OutputStream, the expectation is that there is something reading at the other end.

另一方面,公开 InputStream 表示你需要监听这个流,并且会有你可以读取的数据。

Something that exposes an InputStream, on the other hand, is indicating that you will need to listen to this stream, and there will be data that you can read.

因此可以将 InputStream 连接到 OutputStream

InputStream ---- read ---> intermediateBytes [n] ----写----> OutputStream

正如有人提到的,这就是 copy()方法中的< a href =http://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/IOUtils.html\"rel =noreferrer> IOUtils 让你做。走另一条路是没有意义的......希望这有点道理

As someone metioned, this is what the copy() method from IOUtils lets you do. It does not make sense to go the other way... hopefully this makes some sense

更新:

当然,我越想到这一点,就越能看出这实际上是多么需要。我知道一些评论提到管道输入输入/输出流,但还有另一种可能性。

Of course the more I think of this, the more I can see how this actually would be a requirement. I know some of the comments mentioned Piped input/ouput streams, but there is another possibility.

如果输出暴露的流是 ByteArrayOutputStream ,然后您可以通过调用 toByteArray()方法获得完整内容。然后,您可以使用 ByteArrayInputStream 子类创建输入流包装器。这两个是伪流,它们基本上只包装一个字节数组。因此,以这种方式使用流在技术上是可行的,但对我来说它仍然很奇怪......

If the output stream that is exposed is a ByteArrayOutputStream, then you can always get the full contents by calling the toByteArray() method. Then you can create an input stream wrapper by using the ByteArrayInputStream sub-class. These two are pseudo-streams, they both basically just wrap an array of bytes. Using the streams this way, therefore, is technically possible, but to me it is still very strange...

这篇关于如何将OutputStream转换为InputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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