博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何将OutputStream转换为InputStream?
阅读量:2289 次
发布时间:2019-05-09

本文共 4370 字,大约阅读时间需要 14 分钟。

本文翻译自:

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 . 我在开发阶段,我有两个模块,从一个我得到输出作为OutputStream和第二个,它只接受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转换为InputStream (反之亦然,我的意思是这样),我将能够连接这两个部分吗?

Thanks 谢谢


#1楼

参考:


#2楼

You will need an intermediate class which will buffer between. 您将需要一个将在其间缓冲的中间类。 Each time InputStream.read(byte[]...) is called, the buffering class will fill the passed in byte array with the next chunk passed in from OutputStream.write(byte[]...) . 每次调用InputStream.read(byte[]...) ,缓冲类将使用从OutputStream.write(byte[]...)传入的下一个块填充传入的字节数组。 Since the sizes of the chunks may not be the same, the adapter class will need to store a certain amount until it has enough to fill the read buffer and/or be able to store up any buffer overflow. 由于块的大小可能不同,因此适配器类将需要存储一定量,直到它足以填充读缓冲区和/或能够存储任何缓冲区溢出。

This article has a nice breakdown of a few different approaches to this problem: 本文对这个问题的一些不同方法进行了很好的细分:


#3楼

The open source library has direct support to convert an OutputStream to an InputStream: 开源库直接支持将OutputStream转换为InputStream: :

They also list other options: 他们还列出了其他选项: :


#4楼

If you want to make an OutputStream from an InputStream there is one basic problem. 如果要从InputStream创建OutputStream,则存在一个基本问题。 A method writing to an OutputStream blocks until it is done. 写入OutputStream的方法将一直阻塞,直到完成为止。 So the result is available when the writing method is finished. 因此,当写入方法完成时,结果可用。 This has 2 consequences: 这有两个后果:

  1. If you use only one thread, you need to wait until everything is written (so you need to store the stream's data in memory or disk). 如果只使用一个线程,则需要等到所有内容都写入(因此需要将流的数据存储在内存或磁盘中)。
  2. If you want to access the data before it is finished, you need a second thread. 如果要在数据完成之前访问数据,则需要第二个线程。

Variant 1 can be implemented using byte arrays or filed. 变体1可以使用字节数组或字段来实现。 Variant 1 can be implemented using pipies (either directly or with extra abstraction - eg RingBuffer or the google lib from the other comment). 变体1可以使用pipies实现(直接或使用额外的抽象实现 - 例如RingBuffer或来自其他注释的google lib)。

Indeed with standard java there is no other way to solve the problem. 确实,使用标准java,没有其他方法可以解决问题。 Each solution is an implementataion of one of these. 每个解决方案都是其中一个解决方案的实现。

There is one concept called "continuation" (see for details). 有一个概念称为“延续”(详见 )。 In this case basically this means: 在这种情况下基本上这意味着:

  • there is a special output stream that expects a certain amount of data 有一个特殊的输出流需要一定数量的数据
  • if the ammount is reached, the stream gives control to it's counterpart which is a special input stream 如果达到ammount,则流控制它的对应方,这是一个特殊的输入流
  • the input stream makes the amount of data available until it is read, after that, it passes back the control to the output stream 输入流使数据量可用,直到读取为止,之后,它将控制传递回输出流

While some languages have this concept built in, for java you need some "magic". 虽然有些语言内置了这个概念,但对于java来说,你需要一些“魔力”。 For example "commons-javaflow" from apache implements such for java. 例如,来自apache的“commons-javaflow”为java实现了这样的功能。 The disadvantage is that this requires some special bytecode modifications at build time. 缺点是这需要在构建时进行一些特殊的字节码修改。 So it would make sense to put all the stuff in an extra library whith custom build scripts. 因此,将所有内容放在一个额外的库中,并使用自定义构建脚本是有意义的。


#5楼

Old post but might help others, Use this way: 老帖但可能会帮助别人,使用这种方式:

OutputStream out = new ByteArrayOutputStream();...out.write();...ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(out.toString().getBytes()));

#6楼

As input and output streams are just start and end point, the solution is to temporary store data in byte array. 由于输入和输出流只是起点和终点,因此解决方案是以字节数组临时存储数据。 So you must create intermediate ByteArrayOutputStream , from which you create byte[] that is used as input for new ByteArrayInputStream . 因此,您必须创建中间ByteArrayOutputStream ,从中创建byte[] ,用作新ByteArrayInputStream输入。

public void doTwoThingsWithStream(InputStream inStream, OutputStream outStream){   //create temporary bayte array output stream  ByteArrayOutputStream baos = new ByteArrayOutputStream();  doFirstThing(inStream, baos);  //create input stream from baos  InputStream isFromFirstData = new ByteArrayInputStream(baos.toByteArray());   doSecondThing(isFromFirstData, outStream);}

Hope it helps. 希望能帮助到你。

转载地址:http://igcnb.baihongyu.com/

你可能感兴趣的文章
hdu5810——Balls and Boxes(数学推导)
查看>>
poj3268——Silver Cow Party(最短路+godv之力)
查看>>
poj1860——Currency Exchange(Eellman-Ford+权值为正的环路)
查看>>
poj3259——Wormholes(Eellman-Ford算法)
查看>>
poj1502——MPI Maelstrom(dijkstra算法)
查看>>
poj3660——Cow Contest(判断绝对顺序)
查看>>
poj2240——Arbitrage(Bellman-Ford算法)
查看>>
poj1511——Invitation Cards(SPFA+邻接表)
查看>>
poj3159——Candies(差分约束+SPFA堆栈)
查看>>
Lightoj1074——Extended Traffic(SPFA判断负环)
查看>>
poj1062——昂贵的聘礼(dijkstra变形)
查看>>
poj1847——Tram(dijkstra)
查看>>
hdu4725——The Shortest Path in Nya Graph(SPFA+两层图)
查看>>
山东省第四届ACM省赛题——Proxy(dijistra+多条最短路)
查看>>
poj3169——Layout(差分约束+SPFA判断负环)
查看>>
poj1611——The Suspects(并查集)
查看>>
hdu1272——小希的迷宫(并查集+判断环)
查看>>
poj1308——Is It A Tree?(判断是否为树)
查看>>
poj1251——Jungle Roads(prim)
查看>>
poj1287——Networking(prim)
查看>>