본문 바로가기

안드로이드

PipedInputStream, PipedOutputStream 시스템에서 읽은거 출력하기


        while(true){
        PipedInputStream writeIn = new PipedInputStream();
        PipedOutputStream readOut = new PipedOutputStream();
       
        ReadThread rt = new ReadThread(System.in, readOut);
        ReadThread wt = new ReadThread(writeIn,System.out);
        rt.start();
        wt.start();
        }


class ReadThread extends Thread implements Runnable{
InputStream pi = null;
OutputStream po = null;
public ReadThread(InputStream pi, OutputStream po) {
this.pi = pi;
this.po = po;
}
public void run(){
int ch;
byte [] buffer = new byte [512];
int bytes_read;
try{
for(;;){
bytes_read = pi.read(buffer);
if(bytes_read == -1){return;}
po.write(buffer, 0, bytes_read);
}
}catch (Exception e) {
// TODO: handle exception
}finally{
try{
pi.close();
po.close();
}catch (Exception e) {
// TODO: handle exception
}
}
}
}