您的位置:首页 > 编程语言 > PHP开发

学习 PipedInputStream PipedOutputStream

2015-12-09 09:34 295 查看
博客地址 http://blog.csdn.net/ftx2540993425

PipedOutputStream类 和PipedInputStream 类为管道输出流 和管道输入流。通常都是以PipedOutputStream作为管道的起始端,通常管道输出流和管道输入流通过connect方法连接起来,实现数据从管道输出流 流入 管道输入流中。管道输出流提供管道输入流的所有字节。

PipedOutputStream类介绍:

构造方法:PipedOutputStream();//创建一个尚未连接到管道输入流的管道输出流。
PipedOutputStream(pipedInputStream in);//创建一个连接到该管道输入流的管道输出流。
主要方法。void write(int b);//将指定 byte 写入传送的输出流。
void write(byte[] buf);//将该字符数组写入到管道输出流。
void close();
void connect(PipedInputStream in);使此管道输出流连接到管道输入流 in。
PipedInputStream类介绍:
构造方法:PipedInputStream();//创建一个尚未连接到管道输出流的管道输入流。
PipedInputStream(PipedOutputStream in);//创建一个连接到该管道输出入流的管道输入流。
主要方法:int read(byte[] b,int off,int len);//将最多 len 个数据字节从此管道输入流读入 byte 数组
void connect(PipedOutputStream out);//使此管道输入流连接到管道输出流 out。

简单示例:

Test.class
public class Test {

/**
* @author FTX
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {

PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(in);

new Thread(new Input(in)).start();
new Thread(new Output(out)).start();
}
}


Input.class

class Input implements Runnable{

private PipedInputStream in;

public Input(PipedInputStream in) {
super();
this.in = in;
}
@Override
public void run() {
byte []buf = new byte[1024];
int len;
try {
len = in.read(buf);
String s = new String(buf,0,len);

System.out.println("InPut read from Output "+s);
in.close();
} catch (IOException e) {

e.printStackTrace();
}
}

}

Output.class

class Output implements Runnable{
 
private PipedOutputStream out;
public Output(PipedOutputStream out) { super(); this.out = out; }
@Override
public void run() {
try {
out.write("管道流。。。。".getBytes());
} catch (IOException e) {

e.printStackTrace();
}

}

}


博客地址 http://blog.csdn.net/ftx2540993425
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: