此页: http://blog.ostermiller.org/convert-java-outputstream -inputstream 描述了如何创建从OutputStream的一个InputStream:

new ByteArrayInputStream(out.toByteArray())

其它替代方案是使用PipedStreams和新主题这是麻烦的。

我不喜欢在存储器的字节数组复制许多兆字节到新的想法。 有,这是否更有效?

编辑:

这是劳伦斯·贡萨尔维斯的意见,我想PipedStreams它原来他们是不是很难对付。 下面是Clojure中的示例代码:

(defn #^PipedInputStream create-pdf-stream [pdf-info]
  (let [in-stream (new PipedInputStream)
        out-stream (PipedOutputStream. in-stream)]
    (.start (Thread. #(;Here you write into out-stream)))
    in-stream))
有帮助吗?

解决方案

如果你不希望所有的数据复制到内存缓冲区一下子那么你将必须有你的代码,使用的OutputStream(生产者)和使用InputStream的代码(消费者)在同一个线程要么替代,或者在两个单独的线程同时工作。让他们在同一个线程操作可能要复杂得多,使用两个独立的线程,是更容易出错(你需要确保消费者的从不的块等待输入,或者你”会有效地死锁)并且将需要具有在相同的循环,这似乎太紧密耦合的生产者和消费者的运行。

因此,使用第二线程。这真的不是那么复杂。您链接到页面有一个很好的例子:

  PipedInputStream in = new PipedInputStream();
  PipedOutputStream out = new PipedOutputStream(in);
  new Thread(
    new Runnable(){
      public void run(){
        class1.putDataOnOutputStream(out);
      }
    }
  ).start();
  class2.processDataFromInputStream(in);

其他提示

有一种叫 EasyStream 与以透明的方式管和螺纹交易。 如果一切顺利的话是不是真的很复杂。当(看着劳伦斯贡萨尔维斯例子)时会出现问题

  

class1.putDataOnOutputStream(下);

,则抛出异常。 在该示例中该线程简单地完成,并且该异常丢失,而外部InputStream可能会被截断。

Easystream有异常的传播和其他令人讨厌的问题,我一直在调试大约一年的交易。 (我是图书馆的mantainer:显然,我的解决方案是最好的;)) 下面是如何使用它的示例:

final InputStreamFromOutputStream<String> isos = new InputStreamFromOutputStream<String>(){
 @Override
 public String produce(final OutputStream dataSink) throws Exception {
   /*
    * call your application function who produces the data here
    * WARNING: we're in another thread here, so this method shouldn't 
    * write any class field or make assumptions on the state of the outer class. 
    */
   return produceMydata(dataSink)
 }
};

还有一个很好的引入其中所有其他方式的OutputStream转换成一个InputStream进行说明。值得一看。

这避免了复制缓冲器A简单的解决方案是创建一个特殊用途的ByteArrayOutputStream

public class CopyStream extends ByteArrayOutputStream {
    public CopyStream(int size) { super(size); }

    /**
     * Get an input stream based on the contents of this output stream.
     * Do not use the output stream after calling this method.
     * @return an {@link InputStream}
     */
    public InputStream toInputStream() {
        return new ByteArrayInputStream(this.buf, 0, this.count);
    }
}

写入到根据需要在上述输出流,然后调用toInputStream在基础缓冲区获得的输入流。考虑输出流在该点之后为关闭。

我想的InputStream连接到一个OutputStream最好的方法是通过的管道流 - java.io中封装,如下:

// 1- Define stream buffer
private static final int PIPE_BUFFER = 2048;

// 2 -Create PipedInputStream with the buffer
public PipedInputStream inPipe = new PipedInputStream(PIPE_BUFFER);

// 3 -Create PipedOutputStream and bound it to the PipedInputStream object
public PipedOutputStream outPipe = new PipedOutputStream(inPipe);

// 4- PipedOutputStream is an OutputStream, So you can write data to it
// in any way suitable to your data. for example:
while (Condition) {
     outPipe.write(mByte);
}

/*Congratulations:D. Step 4 will write data to the PipedOutputStream
which is bound to the PipedInputStream so after filling the buffer
this data is available in the inPipe Object. Start reading it to
clear the buffer to be filled again by the PipedInputStream object.*/

在我看来有两个主要的优点为以下代码:

1 - 有存储器没有额外消耗除了缓冲

2 - 你并不需要处理的数据手动排队

我通常会尽量避免,因为死锁的几率就越大,理解代码的难度增加,以及处理异常的问题创建一个单独的线程。

下面是我提出的解决方案:

:通过重复调用produceChunk()创建块内容ProducerInputStream
public abstract class ProducerInputStream extends InputStream {

    private ByteArrayInputStream bin = new ByteArrayInputStream(new byte[0]);
    private ByteArrayOutputStream bout = new ByteArrayOutputStream();

    @Override
    public int read() throws IOException {
        int result = bin.read();
        while ((result == -1) && newChunk()) {
            result = bin.read();
        }
        return result;
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        int result = bin.read(b, off, len);
        while ((result == -1) && newChunk()) {
            result = bin.read(b, off, len);
        }
        return result;
    }

    private boolean newChunk() {
        bout.reset();
        produceChunk(bout);
        bin = new ByteArrayInputStream(bout.toByteArray());
        return (bout.size() > 0);
    }

    public abstract void produceChunk(OutputStream out);

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top