-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScatterGatherNio.java
More file actions
104 lines (73 loc) · 2.69 KB
/
ScatterGatherNio.java
File metadata and controls
104 lines (73 loc) · 2.69 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
/*
Gathering Write means Write the Data in Multiple Buffer in to One Channel
*/
/*
Scattering read means read the Data in to Single Channel in to Multiple buffer
*/
public class Scatter_gather_nio {
@SuppressWarnings("resource")
public static void main(String[] args) {
try {
// For Write the Data
FileChannel outChannel = new FileOutputStream("G:/1.txt").getChannel();
boolean writeStatus = gathering(outChannel);
System.out.println("Write Status : " + writeStatus);
// For read the Data
FileChannel inChannel = new FileInputStream("G:/1.txt").getChannel();
boolean readStatus = scattering(inChannel);
System.out.println("Read Status : " + readStatus);
} catch (Exception e) {
System.out.println("Somthing Went wronge");
}
}
public static boolean gathering(FileChannel outChannel) {
try {
ByteBuffer randomBuffer = ByteBuffer.allocate(70);
ByteBuffer stringBuffer = ByteBuffer.allocate(300);
ByteBuffer doubleBuffer = ByteBuffer.allocate(222);
// Put data in Both Buffer
randomBuffer.asIntBuffer().put(1);
stringBuffer.asCharBuffer().put("Write Data using Gathering");
doubleBuffer.asDoubleBuffer().put(1.1);
// Use Gathering
GatheringByteChannel gatheringByteChannel = outChannel;
// Write Multiple Buffer Data in to Single Channel
ByteBuffer[] byteBuffers = { randomBuffer, stringBuffer, doubleBuffer };
gatheringByteChannel.write(byteBuffers);
outChannel.close();
return true;
} catch (Exception e) {
}
return false;
}
public static boolean scattering(FileChannel inChannel) {
try {
// read From Multiple Buffer using one read Function
ByteBuffer randomReadBuffer = ByteBuffer.allocate(70);
ByteBuffer stringReadBuffer = ByteBuffer.allocate(300);
ByteBuffer doubleReadBuffer = ByteBuffer.allocate(222);
// use Scatter
ScatteringByteChannel scatteringByteChannel = inChannel;
ByteBuffer[] byteReadBuffers = { randomReadBuffer, stringReadBuffer, doubleReadBuffer };
scatteringByteChannel.read(byteReadBuffers);
// Rewind All buffer
randomReadBuffer.rewind();
stringReadBuffer.rewind();
doubleReadBuffer.rewind();
int intData = randomReadBuffer.asIntBuffer().get();
String stringData = stringReadBuffer.asCharBuffer().toString();
Double doubleData = doubleReadBuffer.asDoubleBuffer().get();
System.out.println(intData + " - " + stringData + " - " + doubleData);
inChannel.close();
return true;
} catch (Exception e) {
}
return false;
}
}