OutputStream怎么转换成InputStream

如题,不论方法,只要成功
[b]问题补充:[/b]
你可以采用间接的方式
[b]问题补充:[/b]
TO RednaxelaFX:
谢谢你,你能提供一个示例给我吗,让我最终从OutputStream到InputStream
[b]问题补充:[/b]
TO RednaxelaFX:
[quote]就这样。你的OutputStream是ZipOutputStream的话,在建立这个ZipOutputStream实例的时候底下接一个PipedOutputStream就行。[/quote]

问题就在这里了.我就是不知道怎么能够把ZipOutputStream对象和PipedOutputStream关联起来
我运行了你的示例没有看出来什么效果.
加入我现在提供你一个方法的接口 :
[code="java"]
public InputStream o2i(ZipOutputStream o);//你能帮我实现这个方法么?
[/code]

不行,要在创建那个ZipOutputStream的时候就以PipedOutputStream为底层stream,而不能等到已经创建出ZipOutputStream实例后再想办法。

就我上面的代码来说,如果我已经创建好一个PrintWriter,再想办法去通过pipe转换成InputStream,就已经迟了;是在创建PrintWriter的时候就以一个PipedOutputStream为底层stream作为构造参数来创建的。你的ZipOutputStream也同理。

与其这样设计:
[code="java"]public InputStream o2i(ZipOutputStream o)[/code]
还不如这样:
[code="java"]public ZipOutputStream createZipOut(OutputStream baseStream) {
ZipOutputStream zipOut = new ZipOutputStream(baseStream);
// ... add files here?
return zipOut;
}

// ...
PipedInputStream pipeIn = new PipedInputStream();
PipedOutputStream pipeOut = new PipedOutputStream(pipeIn); // 这两部的顺序重要
ZipOutputStream zipOut = creatZipOut(pipedOut); // 以pipe为底层stream来创建ZipOutputStream
// ... or add files here, whatever
// ... and remember to flush when done[/code]

一个是输出流一个是输入流,这要怎么转换??

用[url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/PipedOutputStream.html]java.io.PipedOutputStream[/url]和[url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/PipedInputStream.html]java.io.PipedInputStream[/url]就行,方法读文档吧,很容易。

可以的。

参看:

Method 1: Buffer the data using a byte array

The easiest method is to buffer the data using a byte array. The code will look something like this:

[code="java"] ByteArrayOutputStream out = new ByteArrayOutputStream();
class1.putDataOnOutputStream(out);
class2.processDataFromInputStream(
new ByteArrayInputStream(out.toByteArray())
);[/code]

That's it! The OutputStream has been converted to an InputStream.
Method 2: Use pipes

The problem with the first method is that you must actually have enough memory to buffer the entire amount of data. You could buffer larger amounts of data by using the filesystem rather than memory, but either way there is a hard limit to the size of the data that can be handled. The solution is create a thread to produce the data to the PipedOutputStream. The current thread can then read the data as it comes in.

[code="java"] PipedInputStream in = new PipedInputStream();
PipedOUtputStream out = new PipedOutputStream(in);
new Thread(
new Runnable(){
public void run(){
class1.putDataOnOutputStream(out);
}
}
).start();
class2.processDataFromInputStream(in);[/code]

Method 3: Use Circular Buffers
The two piped streams in method two actually manage a hidden circular buffer. It is conceptually easier to use an explicit Circular Buffer. CircularBuffers offer several advantages:

* One CircularBuffer class rather than two pipe classes.
* It is easier to convert between the "buffer all data" and "extra threads" approaches.
* You can change the buffer size rather than relying on the hard-coded 1k of buffer in the pipes.

Multiple Threaded Example of a Circular Buffer
[code="java"]
CircularByteBuffer cbb = new CircularByteBuffer();
new Thread(
new Runnable(){
public void run(){
class1.putDataOnOutputStream(cbb.getOutputStream());
}
}
).start();
class2.processDataFromInputStream(cbb.getInputStream());[/code]

Single Threaded Example of a Circular Buffer

[code="java"] // buffer all data in a circular buffer of infinite size
CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
class1.putDataOnOutputStream(cbb.getOutputStream());
class2.processDataFromInputStream(cbb.getInputStream());[/code]

[code="java"]import java.io.*;

public class Out2In {
public static void main(String[] args) throws Exception {
PipedInputStream pipeIn = new PipedInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(pipeIn));

PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);
PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(pipeOut)));

writer.println("Coming through the pipe...");
writer.flush();

System.out.println(reader.readLine()); // Coming through the pipe...

reader.close();
writer.close();

}
}[/code]
就这样。你的OutputStream是ZipOutputStream的话,在建立这个ZipOutputStream实例的时候底下接一个PipedOutputStream就行。