-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipeExemple.java
More file actions
70 lines (60 loc) · 1.48 KB
/
Copy pathPipeExemple.java
File metadata and controls
70 lines (60 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package io;
import java.io.*;
/**
* @author yangxp
* @date 2017年8月14日 上午10:54:41
* 管道流测试
*/
public class PipeExemple {
public static void main(String[] args) {
final PipedOutputStream out = new PipedOutputStream();
PipedInputStream in = null;
try {
in = new PipedInputStream(out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PipeThreadReadA pipeThreadReadA = new PipeThreadReadA(in);
PipeThreadWirteB pipeThreadWirteB = new PipeThreadWirteB(out);
pipeThreadReadA.start();
pipeThreadWirteB.start();
}
}
class PipeThreadReadA extends Thread {
private PipedInputStream in;
public PipeThreadReadA(PipedInputStream in){
this.in = in;
}
@Override
public void run() {
try {
byte b[] = new byte[1024];
int data = in.read(b);
// 读入缓冲区的总字节数;如果由于已到达流末尾而不再有数据,则返回 -1。
while (data != -1) {
data = in.read(b);
}
String teString = new String(b,0,b.length);
System.out.println(teString);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class PipeThreadWirteB extends Thread {
private PipedOutputStream out;
public PipeThreadWirteB(PipedOutputStream out){
this.out = out;
}
@Override
public void run() {
try {
out.write("hello world".getBytes());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}