-
Notifications
You must be signed in to change notification settings - Fork 9
/
GatheringAndScattering.java
110 lines (84 loc) · 3.67 KB
/
GatheringAndScattering.java
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
105
106
107
108
109
110
package wiki;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
public class GatheringAndScattering {
private static String FILENAME = "config/sample.txt";
private static Charset charset = Charset.forName("UTF-8");
public static void main(String[] args) {
String data1 = "1A channel that can write bytes from a sequence of buffers.";
String data2 = "23A channel that can read bytes into a sequence of buffers.";
// We are going to store 2 data's to the file using GatheringByteChannel
// gathering(data1, data2);
scattering();
}
private static void scattering() {
ByteBuffer bblen1 = ByteBuffer.allocate(1024);
ByteBuffer bblen2 = ByteBuffer.allocate(1024);
ByteBuffer bbdata1 = null;
ByteBuffer bbdata2 = null;
FileInputStream in;
try {
in = new FileInputStream(FILENAME);
ScatteringByteChannel scatter = in.getChannel();
// Read 2 length first to get the length of 2 data
scatter.read(new ByteBuffer[] {bblen1, bblen2});
// We have to call rewind if want to read buffer again. It is same as bblen1.position(0).
// bblen1.rewind();
// bblen2.rewind();
// Seek position to 0 so that we can read the data again.
bblen1.position(0);
bblen2.position(0);
int len1 = bblen1.asIntBuffer().get();
int len2 = bblen2.asIntBuffer().get();
// Try to test lengths are correct or not.
System.out.println("Scattering : Len1= " + len1);
System.out.println("Scattering : Len2= " + len2);
bbdata1 = ByteBuffer.allocate(len1);
bbdata2 = ByteBuffer.allocate(len2);
// Read data from the channel
scatter.read(new ByteBuffer[] {bbdata1, bbdata2});
} catch (FileNotFoundException exObj) {
exObj.printStackTrace();
} catch (IOException ioObj) {
ioObj.printStackTrace();
}
// Testing the data is correct or not.
String data1 = new String(bbdata1.array(), charset);
String data2 = new String(bbdata2.array(), charset);
System.out.println(data1);
System.out.println(data2);
}
private static void gathering(String data1, String data2) {
// Store the length of 2 data using GatheringByteChannel
ByteBuffer bblen1 = ByteBuffer.allocate(1024);
ByteBuffer bblen2 = ByteBuffer.allocate(1024);
// Next two buffer hold the data we want to write
ByteBuffer bbdata1 = ByteBuffer.wrap(data1.getBytes());
ByteBuffer bbdata2 = ByteBuffer.wrap(data2.getBytes());
int len1 = data1.length();
int len2 = data2.length();
// Writing length(data) to the Buffer
bblen1.asIntBuffer().put(len1);
bblen2.asIntBuffer().put(len2);
System.out.println("Gathering : Len1= " + len1);
System.out.println("Gathering : Len2= " + len2);
// Write data to the file
try {
FileOutputStream out = new FileOutputStream(FILENAME);
GatheringByteChannel gather = out.getChannel();
gather.write(new ByteBuffer[] {bblen1, bblen2, bbdata1, bbdata2});
out.close();
gather.close();
} catch (FileNotFoundException exObj) {
exObj.printStackTrace();
} catch(IOException ioObj) {
ioObj.printStackTrace();
}
}
}