如何将输出流转换为输入流? [英] How to convert OutputStream to InputStream?

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

问题描述

我正处于开发阶段,我有两个模块,一个模块作为 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

So it is possible to connect an InputStream to an OutputStream

InputStream----读取--->中间字节[n] ----写入---->输出流

正如有人提到的,这就是 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

更新:

当然,我想得越多,我就越能明白这实际上是一个怎样的要求.我知道一些评论提到了 Piped 输入/输出流,但还有另一种可能性.

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...

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

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