Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
170 lines (118 loc) · 4.97 KB
/
Copy pathMain.java
File metadata and controls
170 lines (118 loc) · 4.97 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import java.nio.file.Path;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.CountDownLatch;
public class Main {
public static void main(String args[]) {
try {
if (args.length == 2) {
Path srcPath = Path.of(args[0]);
Path destPath = Path.of(args[1]);
if (Files.exists(srcPath) && Files.isRegularFile(srcPath)) {
destPath = Files.createFile(destPath);
long srcFileSize = Files.size((srcPath));
RandomAccessFile destFile = new RandomAccessFile(destPath.toAbsolutePath().toString(), "rw");
destFile.setLength(srcFileSize);
destFile.close();
System.err.println("Source File size: " + srcFileSize);
long segmentSize = srcFileSize / ThreadsInPool.COUNT;
System.out.println("Segment size: " + segmentSize);
ThreadTask[] tasks = new ThreadTask[ThreadsInPool.COUNT];
CountDownLatch latch = new CountDownLatch(ThreadsInPool.COUNT);
long fSize = srcFileSize;
for (int i = 0; i < ThreadsInPool.COUNT; i++) {
tasks[i] = new ThreadTask(latch, srcPath, destPath, i * segmentSize,
(i + 1) * segmentSize - 1);
fSize -= segmentSize;
}
tasks[ThreadsInPool.COUNT - 1].setEnd(tasks[ThreadsInPool.COUNT - 1].getEnd() + fSize);
for (int i = 0; i < ThreadsInPool.COUNT; i++) {
System.out.println("Thread " + (i + 1) + ": start = " + tasks[i].getStart() + ", end = "
+ tasks[i].getEnd());
}
ExecutorService exe = Executors.newFixedThreadPool(ThreadsInPool.COUNT);
for (int i = 0; i < ThreadsInPool.COUNT; i++) {
exe.execute(tasks[i]);
}
System.err.println("close to ending.....1");
latch.await();
System.err.println("close to ending.....2");
exe.shutdown();
}
} else {
throw new Exception(
"Program requires only 2 arguments.\n 1- Source file path\n2- Destination File Path\n");
}
} catch (FileAlreadyExistsException e) {
System.out.println("Destination File Already Exists");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
class ThreadsInPool {
public static final int COUNT = 4;
}
class ThreadTask implements Runnable {
private long start = 0;
private long end = 0;
private Path SrcFilePath;
private Path DstFilePath;
private CountDownLatch latch;
private static final int BUFFER_SIZE = 8 * 1024; // 8KB
public ThreadTask(CountDownLatch latch, Path SrcFile, Path DstFile, long Start, long End) {
this.latch = latch;
this.SrcFilePath = SrcFile;
this.DstFilePath = DstFile;
this.start = Start;
this.end = End;
}
public void setStart(long Start) {
this.start = Start;
}
public void setEnd(long End) {
this.end = End;
}
public long getStart() {
return this.start;
}
public long getEnd() {
return this.end;
}
@Override
public void run() {
try (RandomAccessFile SrcFile = new RandomAccessFile(this.SrcFilePath.toAbsolutePath().toString(), "r");
RandomAccessFile DstFile = new RandomAccessFile(this.DstFilePath.toAbsolutePath().toString(), "rw");) {
byte[] buffer = new byte[ThreadTask.BUFFER_SIZE];
long bytesToRead = this.end - this.start + 1;
long TotalBytesRead = 0;
SrcFile.seek(this.start);
DstFile.seek(this.start);
while (TotalBytesRead < bytesToRead) {
long remainingBytesToRead = bytesToRead - TotalBytesRead;
int bytesRead = SrcFile.read(buffer, 0,
(remainingBytesToRead >= ThreadTask.BUFFER_SIZE) ? ThreadTask.BUFFER_SIZE
: (int) remainingBytesToRead);
DstFile.write(buffer, 0, bytesRead);
TotalBytesRead += bytesRead;
}
System.err.println("ending...");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
latch.countDown();
}
}
}
// Custom Exceptions
class InvalidArgumentsException extends Exception {
public InvalidArgumentsException(String msg) {
super(msg);
}
}