我在开发阶段,在那里我有两个模块,从一个我得到输出作为一个OutputStream和第二个,它只接受InputStream。你知道如何将OutputStream转换为InputStream(反之亦然,我的意思是真的这样),我将能够连接这两个部分吗?
谢谢
我在开发阶段,在那里我有两个模块,从一个我得到输出作为一个OutputStream和第二个,它只接受InputStream。你知道如何将OutputStream转换为InputStream(反之亦然,我的意思是真的这样),我将能够连接这两个部分吗?
谢谢
当前回答
库io-extras可能很有用。例如,如果你想使用GZIPOutputStream gzip一个InputStream,并且你希望它同步发生(使用默认的缓冲区大小8192):
InputStream is = ...
InputStream gz = IOUtil.pipe(is, o -> new GZIPOutputStream(o));
请注意,该库具有100%的单元测试覆盖率(当然,这是值得的!),并且位于Maven Central上。Maven依赖项是:
<dependency>
<groupId>com.github.davidmoten</groupId>
<artifactId>io-extras</artifactId>
<version>0.1</version>
</dependency>
一定要查看更新的版本。
其他回答
输出流是将数据写入其中的流。如果某个模块公开了一个OutputStream,则期望在另一端有一些内容正在读取。
另一方面,公开InputStream的内容表明您将需要侦听此流,并且将有您可以读取的数据。
因此,可以将InputStream连接到OutputStream
InputStream----read——> intermediateBytes[n] ----write----> OutputStream
正如有人提到的,这就是IOUtils的copy()方法让您做的事情。相反的方向是没有意义的。希望这能让你们理解
更新:
当然,我越想这一点,就越能看出这实际上是一个要求。我知道有些评论提到了管道输入/输出流,但还有另一种可能性。
如果公开的输出流是bybyterayoutputstream,那么您总是可以通过调用toByteArray()方法来获得完整的内容。然后,您可以使用ByteArrayInputStream子类创建输入流包装器。这两个是伪流,它们基本上都只是包装一个字节数组。因此,以这种方式使用流在技术上是可行的,但对我来说还是很奇怪……
我遇到了同样的问题,将ByteArrayOutputStream转换为ByteArrayInputStream,并通过使用ByteArrayOutputStream的派生类来解决它,它能够返回一个ByteArrayInputStream,该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;
}
}
我把这些东西放在github: https://github.com/nickrussler/ByteArrayInOutStream上
Though you cannot convert an OutputStream to an InputStream, java provides a way using PipedOutputStream and PipedInputStream that you can have data written to a PipedOutputStream to become available through an associated PipedInputStream. Sometime back I faced a similar situation when dealing with third party libraries that required an InputStream instance to be passed to them instead of an OutputStream instance. The way I fixed this issue is to use the PipedInputStream and PipedOutputStream. By the way they are tricky to use and you must use multithreading to achieve what you want. I recently published an implementation on github which you can use. Here is the link . You can go through the wiki to understand how to use it.
似乎有许多链接和其他类似的东西,但没有使用管道的实际代码。使用java.io.PipedInputStream和java.io.PipedOutputStream的优点是不会额外消耗内存。ByteArrayOutputStream.toByteArray()返回原始缓冲区的副本,因此这意味着无论内存中有什么,现在都有它的两个副本。然后写入InputStream意味着现在有了数据的三个副本。
使用lambdas的代码(从评论中向@John Manko致敬):
PipedInputStream in = new PipedInputStream();
final PipedOutputStream out = new PipedOutputStream(in);
// in a background thread, write the given output stream to the
// PipedOutputStream for consumption
new Thread(() -> {originalOutputStream.writeTo(out);}).start();
@John Manko注意到的一件事是,在某些情况下,当您无法控制OutputStream的创建时,您可能会在创建者过早地清理OutputStream对象的情况下结束。如果你正在获取ClosedPipeException,那么你应该尝试反向构造函数:
PipedInputStream in = new PipedInputStream(out);
new Thread(() -> {originalOutputStream.writeTo(out);}).start();
注意,您也可以为下面的示例反转构造函数。
也感谢@AlexK纠正我开始一个线程,而不是仅仅启动一个Runnable。
使用try-with-resources的代码:
// take the copy of the stream and re-write it to an InputStream
PipedInputStream in = new PipedInputStream();
new Thread(new Runnable() {
public void run () {
// try-with-resources here
// putting the try block outside the Thread will cause the
// PipedOutputStream resource to close before the Runnable finishes
try (final PipedOutputStream out = new PipedOutputStream(in)) {
// 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
}
}
}).start();
我写的原始代码:
// 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();
这段代码假设原始ByteArrayOutputStream是一个ByteArrayOutputStream,因为它通常是唯一可用的输出流,除非您正在写入文件。这样做的好处是,因为它在一个单独的线程中,所以它也是并行工作的,所以无论消耗你的输入流的是什么,它也会从你的旧输出流中流出。这是有益的,因为缓冲区可以保持更小,您将有更少的延迟和内存使用。
如果你没有ByteArrayOutputStream,那么你将不得不使用java.io.OutputStream类中的write()方法之一或子类中可用的其他方法之一,而不是使用writeTo()。
你需要一个中间类来缓冲。每次调用InputStream.read(byte[]…)时,缓冲类将用从OutputStream.write(byte[]…)传入的下一个块填充传入的字节数组。由于块的大小可能不相同,适配器类需要存储一定数量的块,直到它有足够的容量填满读缓冲区和/或能够存储任何缓冲区溢出。
这篇文章很好地分解了解决这个问题的几种不同方法:
http://blog.ostermiller.org/convert-java-outputstream-inputstream