Way*_*int 308 java inputstream outputstream
我在开发阶段,我有两个模块,从一个我得到输出作为OutputStream第二个,只接受InputStream.你知道如何转换OutputStream到InputStream(而不是相反,我是说真的这样),我将能够这两部分连接?
谢谢
mik*_*eho 180
似乎有很多链接和其他类似的东西,但没有使用管道的实际代码.使用的优点java.io.PipedInputStream和java.io.PipedOutputStream是有记忆没有额外的消费.ByteArrayOutputStream.toByteArray()返回原始缓冲区的副本,这意味着无论你在内存中拥有什么,你现在都有两个副本.然后写入一个InputStream意味着你现在有三个数据副本.
代码:
// take the copy of the stream and re-write it to an InputStream
PipedInputStream in = new PipedInputStream();
final PipedOutputStream out = new PipedOutputStream(in);
new Thread(new Runnable() {
public void run () {
try {
// write the original OutputStream to the PipedOutputStream
// note that in order for the below method to work, you need
// to ensure that the data has finished writing to the
// ByteArrayOutputStream
originalByteArrayOutputStream.writeTo(out);
}
catch (IOException e) {
// logging and exception handling should go here
}
finally {
// close the PipedOutputStream here because we're done writing data
// once this thread has completed its run
if (out != null) {
// close the PipedOutputStream cleanly
out.close();
}
}
}
}).start();
Run Code Online (Sandbox Code Playgroud)
此代码假定它originalByteArrayOutputStream是a,ByteArrayOutputStream因为它通常是唯一可用的输出流,除非您正在写入文件.我希望这有帮助!关于这一点的好处是,因为它在一个单独的线程中,它也是并行工作的,所以无论消耗什么输入流都将流出旧的输出流.这是有益的,因为缓冲区可以保持较小,并且您将具有更少的延迟和更少的内存使用.
Jav*_*ker 105
一个OutputStream是一个,你将数据写入.如果某个模块暴露了一个OutputStream,那么期望在另一端有一些东西读取.
InputStream另一方面,暴露出来的东西表明你需要收听这个流,并且会有你可以阅读的数据.
所以可以连接InputStream一个OutputStream
InputStream----read---> intermediateBytes[n] ----write----> OutputStream
正如有人提到的,这就是IOUtils允许你做的copy()方法.走另一条路是没有意义的......希望这是有道理的
更新:
当然,我越想到这一点,我就越能看出这实际上是如何要求的.我知道一些评论提到了Piped输入/输出流,但还有另一种可能性.
如果公开的输出流是a ByteArrayOutputStream,则可以通过调用该toByteArray()方法始终获取完整内容.然后,您可以使用ByteArrayInputStream子类创建输入流包装器.这两个是伪流,它们基本上只包装一个字节数组.因此,以这种方式使用流是技术上可行的,但对我来说它仍然很奇怪......
小智 31
由于输入和输出流只是起点和终点,因此解决方案是以字节数组临时存储数据.因此,您必须创建中间件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);
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.
mck*_*mey 20
您将需要一个将在其间缓冲的中间类.每次InputStream.read(byte[]...)调用时,缓冲类将填充传入的字节数组,并传入下一个传入的块OutputStream.write(byte[]...).由于块的大小可能不同,因此适配器类将需要存储一定量,直到它足以填充读缓冲区和/或能够存储任何缓冲区溢出.
本文对这个问题的一些不同方法进行了很好的细分:
http://blog.ostermiller.org/convert-java-outputstream-inputstream
Vij*_*pta 17
ByteArrayOutputStream buffer = (ByteArrayOutputStream) aOutputStream;
byte[] bytes = buffer.toByteArray();
InputStream inputStream = new ByteArrayInputStream(bytes);
Run Code Online (Sandbox Code Playgroud)
kop*_*por 16
该easystream开源库有直接支持到OutputStream转换成一个InputStream:http://io-tools.sourceforge.net/easystream/tutorial/tutorial.html
他们还列出了其他选项:http://io-tools.sourceforge.net/easystream/OutputStream_to_InputStream.html
我遇到了将a转换ByteArrayOutputStream为a ByteArrayInputStream并通过使用派生类解决它的相同问题,派生类ByteArrayOutputStream能够返回ByteArrayInputStream使用内部缓冲区初始化的派生类ByteArrayOutputStream.这样就不会使用额外的内存而且'转换'非常快:
package info.whitebyte.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/**
* This class extends the ByteArrayOutputStream by
* providing a method that returns a new ByteArrayInputStream
* which uses the internal byte array buffer. This buffer
* is not copied, so no additional memory is used. After
* creating the ByteArrayInputStream the instance of the
* ByteArrayInOutStream can not be used anymore.
* <p>
* The ByteArrayInputStream can be retrieved using <code>getInputStream()</code>.
* @author Nick Russler
*/
public class ByteArrayInOutStream extends ByteArrayOutputStream {
/**
* Creates a new ByteArrayInOutStream. The buffer capacity is
* initially 32 bytes, though its size increases if necessary.
*/
public ByteArrayInOutStream() {
super();
}
/**
* Creates a new ByteArrayInOutStream, with a buffer capacity of
* the specified size, in bytes.
*
* @param size the initial size.
* @exception IllegalArgumentException if size is negative.
*/
public ByteArrayInOutStream(int size) {
super(size);
}
/**
* Creates a new ByteArrayInputStream that uses the internal byte array buffer
* of this ByteArrayInOutStream instance as its buffer array. The initial value
* of pos is set to zero and the initial value of count is the number of bytes
* that can be read from the byte array. The buffer array is not copied. This
* instance of ByteArrayInOutStream can not be used anymore after calling this
* method.
* @return the ByteArrayInputStream instance
*/
public ByteArrayInputStream getInputStream() {
// create new ByteArrayInputStream that respects the current count
ByteArrayInputStream in = new ByteArrayInputStream(this.buf, 0, this.count);
// set the buffer of the ByteArrayOutputStream
// to null so it can't be altered anymore
this.buf = null;
return in;
}
}
Run Code Online (Sandbox Code Playgroud)
我把这些东西放在github上:https://github.com/nickrussler/ByteArrayInOutStream
| 归档时间: |
|
| 查看次数: |
290244 次 |
| 最近记录: |