Skip to content

Commit 731ac3a

Browse files
committed
File Channel
1 parent daa723d commit 731ac3a

File tree

11 files changed

+243
-0
lines changed

11 files changed

+243
-0
lines changed

Input & Output/007_FileChannel_to_copy_Files_to_another/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Input & Output/007_FileChannel_to_copy_Files_to_another/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Input & Output/007_FileChannel_to_copy_Files_to_another/.idea/workspace.xml

+82
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Line 1
2+
Line 2
3+
Line 3
4+
Line 4
5+
Line 5
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import java.io.IOException;
2+
import java.nio.ByteBuffer;
3+
import java.nio.channels.Pipe;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
9+
try {
10+
Pipe pipe = Pipe.open();
11+
12+
Runnable writer = new Runnable() {
13+
@Override
14+
public void run() {
15+
try {
16+
Pipe.SinkChannel sinkChannel = pipe.sink();
17+
ByteBuffer buffer = ByteBuffer.allocate(56);
18+
19+
for(int i=0; i<10; i++) {
20+
String currentTime = "The time is: " + System.currentTimeMillis();
21+
22+
buffer.put(currentTime.getBytes());
23+
buffer.flip();
24+
25+
while(buffer.hasRemaining()) {
26+
sinkChannel.write(buffer);
27+
}
28+
buffer.flip();
29+
Thread.sleep(100);
30+
31+
}
32+
33+
} catch(Exception e) {
34+
e.printStackTrace();
35+
}
36+
}
37+
};
38+
39+
40+
Runnable reader = new Runnable() {
41+
@Override
42+
public void run() {
43+
44+
try {
45+
Pipe.SourceChannel sourceChannel = pipe.source();
46+
ByteBuffer buffer = ByteBuffer.allocate(56);
47+
48+
for(int i=0; i<10; i++) {
49+
int bytesRead = sourceChannel.read(buffer);
50+
byte[] timeString = new byte[bytesRead];
51+
buffer.flip();
52+
buffer.get(timeString);
53+
System.out.println("Reader Thread: " + new String(timeString));
54+
buffer.flip();
55+
Thread.sleep(100);
56+
}
57+
58+
} catch(Exception e) {
59+
e.printStackTrace();
60+
}
61+
62+
}
63+
};
64+
65+
new Thread(writer).start();
66+
new Thread(reader).start();
67+
68+
} catch(IOException e) {
69+
e.printStackTrace();
70+
}
71+
72+
// try (FileOutputStream binFile = new FileOutputStream("data.dat");
73+
// FileChannel binChannel = binFile.getChannel()) {
74+
//
75+
// ByteBuffer buffer = ByteBuffer.allocate(100);
76+
//
77+
//
78+
// byte[] outputBytes = "Hello World!".getBytes();
79+
// buffer.put(outputBytes);
80+
// long int1Pos = outputBytes.length;
81+
// buffer.putInt(245);
82+
// long int2Pos = int1Pos + Integer.BYTES;
83+
// buffer.putInt(-98765);
84+
// byte[] outputBytes2 = "Nice to meet you".getBytes();
85+
// buffer.put(outputBytes2);
86+
// long int3Pos = int2Pos + Integer.BYTES + outputBytes2.length;
87+
// buffer.putInt(1000);
88+
// buffer.flip();
89+
//
90+
// binChannel.write(buffer);
91+
//
92+
//
93+
// RandomAccessFile ra = new RandomAccessFile("data.dat", "rwd");
94+
// FileChannel channel = ra.getChannel();
95+
//
96+
// ByteBuffer readBuffer = ByteBuffer.allocate(Integer.BYTES);
97+
// channel.position(int3Pos);
98+
// channel.read(readBuffer);
99+
// readBuffer.flip();
100+
//
101+
// System.out.println("int3 = " + readBuffer.getInt());
102+
// readBuffer.flip();
103+
// channel.position(int2Pos);
104+
// channel.read(readBuffer);
105+
// readBuffer.flip();
106+
//
107+
// System.out.println("int2 = " + readBuffer.getInt());
108+
// readBuffer.flip();
109+
// channel.position(int1Pos);
110+
// channel.read(readBuffer);
111+
// readBuffer.flip();
112+
//
113+
// System.out.println("int1 = " + readBuffer.getInt());
114+
//
115+
// RandomAccessFile copyFile = new RandomAccessFile("datacopy.dat", "rw");
116+
// FileChannel copyChannel = copyFile.getChannel();
117+
// channel.position(0);
118+
//// long numTransferred = copyChannel.transferFrom(channel, 0, channel.size());
119+
// long numTransferred = channel.transferTo(0, channel.size(), copyChannel);
120+
// System.out.println("Num transferred = " + numTransferred);
121+
//
122+
// channel.close();
123+
// ra.close();
124+
// copyChannel.close();
125+
//
126+
//
127+
// } catch (IOException e) {
128+
// e.printStackTrace();
129+
// }
130+
}
131+
}

0 commit comments

Comments
 (0)