forked from TimSongCoder/LearnJavaForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipeDemo.java
More file actions
60 lines (56 loc) · 1.6 KB
/
Copy pathPipeDemo.java
File metadata and controls
60 lines (56 loc) · 1.6 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
import java.nio.channels.Pipe;
import java.nio.channels.Pipe.SinkChannel;
import java.nio.channels.Pipe.SourceChannel;
import java.nio.ByteBuffer;
import java.io.IOException;
public class PipeDemo{
private static final int BUFFER_SIZE = 10;
private static final int SEND_LIMIT = 3;
public static void main(String[] args) throws IOException{
Pipe pipe = Pipe.open();
Runnable senderTask = new Runnable(){
public void run(){
SinkChannel writeChannle = pipe.sink();
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
for(int i=0;i<SEND_LIMIT;i++){
buffer.clear(); // prepare for new round
for(int j=0; j<BUFFER_SIZE; j++){
buffer.put((byte)(Math.random()*256));
}
buffer.flip();
try{
while(writeChannle.write(buffer) > 0);
}catch(IOException ioe){
ioe.printStackTrace();
}
}
if(writeChannle!=null){
try{
writeChannle.close(); // prevent the reading thread block
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
};
Runnable receiverTask = new Runnable(){
public void run(){
SourceChannel srcChannle = pipe.source();
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
try{
while(srcChannle.read(buffer) >= 0){ // -1 indicating the end-of-stream
buffer.flip();
while(buffer.hasRemaining()){
System.out.println(Byte.toUnsignedInt(buffer.get()));
}
buffer.clear(); // prepare for next read operation.
}
}catch(IOException ioe){
ioe.printStackTrace();
}
}
};
new Thread(senderTask).start();
new Thread(receiverTask).start();
}
}