forked from arturmkrtchyan/zero-copy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JioChannel.java
74 lines (63 loc) · 1.96 KB
/
JioChannel.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
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class JioChannel {
public static void main(String[] args) {
JioChannel channel = new JioChannel();
try {
if(args.length < 3) {
System.out.println("usage: JioChannel <source> <destination> <mode>\n");
return;
}
if("1".equals(args[2])) {
channel.copy(args[0], args[1]);
} else {
channel.zeroCopy(args[0], args[1]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void zeroCopy(String from, String to) throws IOException {
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(from).getChannel();
destination = new FileOutputStream(to).getChannel();
source.transferTo(0, source.size(), destination);
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
}
public void copy(String from, String to) throws IOException {
byte[] data = new byte[8 * 1024];
FileInputStream fis = null;
FileOutputStream fos = null;
long bytesToCopy = new File(from).length();
long bytesCopied = 0;
try {
fis = new FileInputStream(from);
fos = new FileOutputStream(to);
while (bytesCopied < bytesToCopy) {
fis.read(data);
fos.write(data);
bytesCopied += data.length;
}
fos.flush();
} finally {
if(fis != null) {
fis.close();
}
if(fos != null) {
fos.close();
}
}
}
}