package joeho.net.csdn.blog.io;
import java.io.*;
public class PipedSender extends Thread {
private PipedOutputStream outer = new PipedOutputStream();
/**
* Method run
*
*
*/
public void run() {
try{
String strBuff = new String("a message from sender");
outer.write(strBuff.getBytes());
outer.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* Method getPipedOutputStream
*
*
* @return
*
*/
public PipedOutputStream getPipedOutputStream() {
return outer;
}
}
public class PipedReceiver extends Thread {
private PipedInputStream inputer = new PipedInputStream();
/**
* Method run
*
*
*/
public void run() {
try{
byte[] buff = new byte[1024];
int len = inputer.read(buff);
System.out.println("received a message from:/n"+new String(buff,0,len));
inputer.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* Method getPipedInputStream
*
*
* @return
*
*/
public PipedInputStream getPipedInputStream() {
return inputer;
}
}
public class PipedStreamTest {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) throws Exception {
PipedSender ps = new PipedSender();
PipedReceiver pr = new PipedReceiver();
PipedOutputStream send = ps.getPipedOutputStream();
PipedInputStream receive = pr.getPipedInputStream();
send.connect(receive);
ps.start();
pr.start();
}
}