Vag*_*rdi 80 java io inputstream stream bytearrayoutputstream
此页面:http://blog.ostermiller.org/convert-java-outputstream-inputstream 描述了如何从OutputStream创建InputStream:
new ByteArrayInputStream(out.toByteArray())
Run Code Online (Sandbox Code Playgroud)
其他替代方案是使用PipedStreams和新线程,这很麻烦.
我不喜欢将许多兆字节复制到内存字节数组中的新内容.有没有一个库可以更有效地完成这项工作?
编辑:
根据Laurence Gonsalves的建议,我尝试了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))
Run Code Online (Sandbox Code Playgroud)
Lau*_*ves 68
如果您不想一次性将所有数据复制到内存缓冲区中,那么您将不得不使用使用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);
Run Code Online (Sandbox Code Playgroud)
Gab*_*Gab 15
还有另一个名为EasyStream的开源库,它以透明的方式处理管道和线程.如果一切顺利,这并不是很复杂.问题出现时(看Laurence Gonsalves的例子)
class1.putDataOnOutputStream(下);
引发异常.在该示例中,线程完成并且异常丢失,而外部InputStream可能被截断.
Easystream处理异常传播和其他令人讨厌的问题,我已经调试了大约一年.(我是图书馆的傻瓜:显然我的解决方案是最好的;))以下是如何使用它的示例:
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)
}
};
Run Code Online (Sandbox Code Playgroud)
还有一个很好的介绍,其中解释了将OutputStream转换为InputStream的所有其他方法.值得一看.
避免复制缓冲区的简单解决方案是创建一个特殊目的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);
}
}
Run Code Online (Sandbox Code Playgroud)
根据需要写入上面的输出流,然后调用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.*/
Run Code Online (Sandbox Code Playgroud)
在我看来,这个代码有两个主要优点:
1 - 除缓冲区外没有额外的内存消耗.
2 - 您无需手动处理数据排队
| 归档时间: |
|
| 查看次数: |
93224 次 |
| 最近记录: |