我在开发阶段,在那里我有两个模块,从一个我得到输出作为一个OutputStream和第二个,它只接受InputStream。你知道如何将OutputStream转换为InputStream(反之亦然,我的意思是真的这样),我将能够连接这两个部分吗?

谢谢


当前回答

如果你想从一个InputStream生成一个OutputStream,有一个基本的问题。写入OutputStream的方法会阻塞,直到完成为止。因此,当编写方法完成时,结果是可用的。这有两个后果:

如果只使用一个线程,则需要等待所有内容写入(因此需要将流数据存储在内存或磁盘中)。 如果希望在数据完成之前访问数据,则需要第二个线程。

变体1可以使用字节数组或字段实现。 变体1可以使用pipies实现(直接或额外的抽象-例如RingBuffer或来自其他注释的谷歌库)。

事实上,在标准java中,没有其他方法可以解决这个问题。每个解决方案都是其中一个的实现。

有一个概念叫做“延续”(详见维基百科)。在这种情况下,这基本上意味着:

有一个特殊的输出流,它需要一定数量的数据 如果达到数量,则流将控制权交给对应的特殊输入流 输入流在读取数据之前提供可用的数据量,在读取之后,它将控制传递回输出流

虽然有些语言内置了这个概念,但对于java,您需要一些“魔法”。例如apache中的“commons-javaflow”实现了这样的java。缺点是这需要在构建时进行一些特殊的字节码修改。因此,将所有的东西都放在一个带有自定义构建脚本的额外库中是有意义的。

其他回答

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.

输出流是将数据写入其中的流。如果某个模块公开了一个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上

你需要一个中间类来缓冲。每次调用InputStream.read(byte[]…)时,缓冲类将用从OutputStream.write(byte[]…)传入的下一个块填充传入的字节数组。由于块的大小可能不相同,适配器类需要存储一定数量的块,直到它有足够的容量填满读缓冲区和/或能够存储任何缓冲区溢出。

这篇文章很好地分解了解决这个问题的几种不同方法:

http://blog.ostermiller.org/convert-java-outputstream-inputstream

easystream开源库直接支持将OutputStream转换为InputStream: http://io-tools.sourceforge.net/easystream/tutorial/tutorial.html

// create conversion
final OutputStreamToInputStream<Void> out = new OutputStreamToInputStream<Void>() {
    @Override
    protected Void doRead(final InputStream in) throws Exception {
           LibraryClass2.processDataFromInputStream(in);
           return null;
        }
    };
try {   
     LibraryClass1.writeDataToTheOutputStream(out);
} finally {
     // don't miss the close (or a thread would not terminate correctly).
     out.close();
}

他们还列出了其他选项:http://io-tools.sourceforge.net/easystream/outputstream_to_inputstream/implementations.html

Write the data the data into a memory buffer (ByteArrayOutputStream) get the byteArray and read it again with a ByteArrayInputStream. This is the best approach if you're sure your data fits into memory. Copy your data to a temporary file and read it back. Use pipes: this is the best approach both for memory usage and speed (you can take full advantage of the multi-core processors) and also the standard solution offered by Sun. Use InputStreamFromOutputStream and OutputStreamToInputStream from the easystream library.