出力ストリームOutputStreamを入力ストリームInputStreamに変換する方法



    OutputStream      InputStream   
 :
package test.io;   
import java.io.ByteArrayInputStream;   
import java.io.ByteArrayOutputStream;   
import java.io.IOException;   
/** 
*    OutputStream     InputStream。 
*         ,                。 
* 
*/  
public class Test1 {   
/** 
* @param args 
* @throws IOException 
*/  
public static void main(String[] args) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] bs = new byte[] { 1, 2, 3, 4, 5 };  
out.write(bs); 

ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()) 
byte[] bs = new byte[1024];   
int len = in.read(bs);   
for (int i = 0; i < len; i++) {   
System.out.println(bs[i]);   
}   
}
} 

 :
package test.io;   
import java.io.IOException;   
import java.io.PipedInputStream;   
import java.io.PipedOutputStream;   
/** 
*    OutputStream     InputStream。           ,           ,          。 
*/  
public class Test2 {   
/** 
* @param args 
* @throws IOException 
*/  
public static void main(String[] args) throws IOException {   
//   Piped       
PipedInputStream in = new PipedInputStream();
final PipedOutputStream out = new PipedOutputStream(in);
//     ,          
new Thread(new Runnable() {
public void run() {
try {
byte[] bs = new byte[2];
for (int i = 0; i <= 100; i++) {
bs[0] = (byte) i;
bs[1] = (byte) (i + 1);
//         
out.write(bs);
out.flush();
//   0.1 
Thread.sleep(100);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
//          
//               
byte[] bs = new byte[1024];
int len;
//     ,     
try {
while ((len = in.read(bs)) != -1) {
for (int i = 0; i < len; i++) {
System.out.println(bs[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

      PipedOutputStream  API  
               ,       。            。  ,          PipedOutputStream   ,           PipedInputStream   。                 ,            。

      PipedInputStream API  
               ;                       。  ,         PipedInputStream     ,               PipedOutputStream。                 ,            。            ,                      。

 :
package test.io;
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;   
import com.Ostermiller.util.CircularByteBuffer;   
/** 
*    OutputStream     InputStream。 
* <p> 
*   CircilarBuffer     <br> 
*       <A href="http://ostermiller.org/utils/download.html 
http://ostermiller.org/utils/download.html<br> 
*       http://ostermiller.org/utils/CircularBuffer.html 
* </p>
*/  
public class Test3 {   
/** 
* @param args 
* @throws IOException 
*/  
public static void main(String[] args) throws IOException {   
//   CircularByteBuffer  
final CircularByteBuffer cbb = new CircularByteBuffer();   
//     ,            
new Thread(new Runnable() {   
public void run() {   
try {   
OutputStreamClass3.putDataOnOutputStream(cbb.getOutputStream());   
} catch (IOException e) {   
e.printStackTrace();   
}   
}   
}).start();   
//            
//                 
InputStreamClass3.processDataFromInputStream(cbb.getInputStream());   
}   
}   
class OutputStreamClass3 {   
public static void putDataOnOutputStream(OutputStream out) throws IOException {   
byte[] bs = new byte[2];   
for (int i = 0; i <= 100; i++) {   
bs[0] = (byte) i;   
bs[1] = (byte) (i + 1);   
//           
out.write(bs);   
out.flush();   
try {   
//   0.1   
Thread.sleep(100);   
} catch (InterruptedException e) {   
e.printStackTrace();   
}   
}   
}   
}   
class InputStreamClass3 {   
public static void processDataFromInputStream(InputStream in) {   
byte[] bs = new byte[1024];   
int len;   
//     ,       
try {   
while ((len = in.read(bs)) != -1) {   
for (int i = 0; i < len; i++) {   
System.out.println(bs[i]);   
}   
}   
} catch (IOException e) {   
e.printStackTrace();   
}   
}   
}  

           ,     ,                                       

package test.io;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;   
import com.Ostermiller.util.CircularByteBuffer;   
/** 
*    OutputStream     InputStream。 
* <p> 
*   CircilarBuffer     <br> 
*       <A href="http://ostermiller.org/utils/download.html 
* http://ostermiller.org/utils/download.html<br> 
*       http://ostermiller.org/utils/CircularBuffer.html 
* </p>
*/  
public class Test4 {   
/** 
* @param args 
* @throws IOException 
*/  
public static void main(String[] args) throws IOException {   
//          ,        
CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);   
OutputStreamClass4.putDataOnOutputStream(cbb.getOutputStream());   
InputStreamClass4.processDataFromInputStream(cbb.getInputStream());   
}   
}   
class OutputStreamClass4 {   
public static void putDataOnOutputStream(OutputStream out) throws IOException {   
byte[] bs = new byte[] { 1, 2, 3, 4, 5 };   
out.write(bs);   
}   
}   
class InputStreamClass4 {   
public static void processDataFromInputStream(InputStream in) throws IOException {   
byte[] bs = new byte[1024];   
int len = in.read(bs);   
for (int i = 0; i < len; i++) {   
System.out.println(bs[i]);   
}   
}   
}