天天看点

OutputStream怎么转换成InputStream

http://www.iteye.com/problems/13164 

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:

Java代码   

  1. ByteArrayOutputStream out = new ByteArrayOutputStream();   
  2. class1.putDataOnOutputStream(out);   
  3. class2.processDataFromInputStream(   
  4.   new ByteArrayInputStream(out.toByteArray())   
  5. );  
ByteArrayOutputStream out = new ByteArrayOutputStream();
  class1.putDataOnOutputStream(out);
  class2.processDataFromInputStream(
    new ByteArrayInputStream(out.toByteArray())
  );
           

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.

Java代码   

  1. PipedInputStream in = new PipedInputStream();   
  2. PipedOUtputStream out = new PipedOutputStream(in);   
  3. new Thread(   
  4.   new Runnable(){   
  5.     public void run(){   
  6.       class1.putDataOnOutputStream(out);   
  7.     }   
  8.   }   
  9. ).start();   
  10. class2.processDataFromInputStream(in);  
PipedInputStream in = new PipedInputStream();
  PipedOUtputStream out = new PipedOutputStream(in);
  new Thread(
    new Runnable(){
      public void run(){
        class1.putDataOnOutputStream(out);
      }
    }
  ).start();
  class2.processDataFromInputStream(in);
           

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

Java代码   

  1. CircularByteBuffer cbb = new CircularByteBuffer();   
  2. new Thread(   
  3.   new Runnable(){   
  4.     public void run(){   
  5.       class1.putDataOnOutputStream(cbb.getOutputStream());   
  6.     }   
  7.   }   
  8. ).start();   
  9. class2.processDataFromInputStream(cbb.getInputStream());  
CircularByteBuffer cbb = new CircularByteBuffer();
  new Thread(
    new Runnable(){
      public void run(){
        class1.putDataOnOutputStream(cbb.getOutputStream());
      }
    }
  ).start();
  class2.processDataFromInputStream(cbb.getInputStream());
           

Single Threaded Example of a Circular Buffer

Java代码   

  1. // buffer all data in a circular buffer of infinite size   
  2. CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);   
  3. class1.putDataOnOutputStream(cbb.getOutputStream());   
  4. class2.processDataFromInputStream(cbb.getInputStream());  
// 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());
           

playfish (架构师) 2009-03-18 Java代码   

  1. import java.io.*;   
  2. public class Out2In {   
  3.   public static void main(String[] args) throws Exception {   
  4.     PipedInputStream pipeIn = new PipedInputStream();   
  5.     BufferedReader reader = new BufferedReader(new InputStreamReader(pipeIn));   
  6.     PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);   
  7.     PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(pipeOut)));   
  8.     writer.println("Coming through the pipe...");   
  9.     writer.flush();   
  10.     System.out.println(reader.readLine()); // Coming through the pipe...   
  11.     reader.close();   
  12.     writer.close();   
  13.   }   
  14. }  
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();
  }
}
           

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