Skip to content

Commit 3592c90

Browse files
committed
Make things more clean inside SplitTask.java and MergeTask.java
1 parent 6993b89 commit 3592c90

File tree

3 files changed

+198
-178
lines changed

3 files changed

+198
-178
lines changed

src/main/java/nsusbloader/Utilities/WindowsDrivers/DownloadDriversTask.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ public class DownloadDriversTask extends Task<String> {
3232

3333
@Override
3434
protected String call() {
35-
if (isDriversDownloaded())
36-
return driversInstallerFile.getAbsolutePath();
37-
if (downloadDrivers())
35+
if (isDriversDownloaded() || downloadDrivers())
3836
return driversInstallerFile.getAbsolutePath();
3937
return null;
4038
}

src/main/java/nsusbloader/Utilities/splitmerge/MergeTask.java

Lines changed: 95 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -33,118 +33,136 @@ public class MergeTask extends Task<Boolean> {
3333
private final String saveToPath;
3434
private final String filePath;
3535

36+
private File splitFile;
37+
38+
private File[] chunkFiles;
39+
private long chunksTotalSize;
40+
private File resultFile;
41+
3642
public MergeTask(String filePath, String saveToPath) {
3743
this.filePath = filePath;
3844
this.saveToPath = saveToPath;
3945
logPrinter = Log.getPrinter(EModule.SPLIT_MERGE_TOOL);
4046
}
4147
@Override
4248
protected Boolean call() {
43-
logPrinter.print("Merge file: "+filePath, EMsgType.INFO);
44-
45-
File folder = new File(filePath);
49+
try {
50+
logPrinter.print("Merge file: " + filePath, EMsgType.INFO);
51+
splitFile = new File(filePath);
4652

47-
long cnkTotalSize = 0;
53+
collectChunks();
54+
validateChunks();
55+
sortChunks();
56+
calculateChunksSizeSum();
4857

49-
File[] chunkFiles = folder.listFiles((file, s) -> s.matches("^[0-9][0-9]$"));
58+
createFile();
59+
mergeChunksToFile();
60+
validateFile();
5061

51-
if (chunkFiles == null || chunkFiles.length == 0){
52-
logPrinter.print("Selected folder doesn't have any chunks. Nothing to do here.", EMsgType.FAIL);
62+
logPrinter.print("Merge task complete!", EMsgType.INFO);
63+
logPrinter.close();
64+
return true;
65+
}
66+
catch (Exception e){
67+
logPrinter.print(e.getMessage(), EMsgType.FAIL);
5368
logPrinter.close();
5469
return false;
5570
}
71+
}
72+
73+
private void collectChunks(){
74+
chunkFiles = splitFile.listFiles((file, s) -> s.matches("^[0-9][0-9]$"));
75+
}
76+
77+
private void validateChunks() throws Exception{
78+
if (chunkFiles == null || chunkFiles.length == 0){
79+
throw new Exception("Selected folder doesn't have any chunks. Nothing to do here.");
80+
}
81+
}
5682

83+
private void sortChunks(){
5784
Arrays.sort(chunkFiles);
85+
}
5886

87+
private void calculateChunksSizeSum(){
5988
logPrinter.print("Next files will be merged in following order: ", EMsgType.INFO);
6089
for (File cnk : chunkFiles){
6190
logPrinter.print(" "+cnk.getName(), EMsgType.INFO);
62-
cnkTotalSize += cnk.length();
91+
chunksTotalSize += cnk.length();
6392
}
93+
}
6494

65-
double chunkPercent = (4194240.0 / (cnkTotalSize / 100.0) / 100.0);
66-
long totalSizeCnt = 0;
95+
private void createFile() throws Exception{
96+
final String splitFileName = splitFile.getName();
6797

68-
File resultFile = new File(saveToPath+File.separator+"!_"+folder.getName());
69-
//*******
70-
for (int i = 0; ; i++){
98+
resultFile = new File(saveToPath+File.separator+"!_"+splitFileName);
99+
100+
for (int i = 0; i < 50 ; i++){
71101
if (this.isCancelled()){
72-
logPrinter.print("Split task interrupted!", EMsgType.PASS);
73-
logPrinter.close();
74-
return false;
102+
throw new InterruptedException("Split task interrupted!");
75103
}
76104

77105
if (resultFile.exists()){
78-
if (i >= 50){
79-
logPrinter.print("Can't create new file.", EMsgType.FAIL);
80-
logPrinter.close();
81-
return false;
82-
}
83-
84106
logPrinter.print("Trying to create a good new file...", EMsgType.WARNING);
85-
resultFile = new File(saveToPath+File.separator+"!_"+i+"_"+folder.getName());
107+
resultFile = new File(saveToPath+File.separator+"!_"+i+"_"+splitFileName);
86108
continue;
87109
}
110+
88111
logPrinter.print("Save results to: "+resultFile.getAbsolutePath(), EMsgType.INFO);
89-
break;
112+
return;
90113
}
91-
//*******
114+
throw new Exception("Can't create new file.");
115+
}
92116

93-
try {
94-
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(resultFile));
95-
96-
BufferedInputStream bis;
97-
byte[] chunk;
98-
int readBytesCnt;
99-
100-
for (File cnk : chunkFiles){
101-
bis = new BufferedInputStream(new FileInputStream(cnk));
102-
while (true){
103-
104-
if (this.isCancelled()){
105-
bos.close();
106-
bis.close();
107-
boolean isDeleted = resultFile.delete();
108-
logPrinter.print("Split task interrupted and file "+(isDeleted?"deleted.":"is not deleted."), EMsgType.PASS);
109-
logPrinter.close();
110-
return false;
111-
}
112-
113-
chunk = new byte[4194240];
114-
readBytesCnt = bis.read(chunk);
115-
116-
logPrinter.updateProgress(chunkPercent * totalSizeCnt);
117-
totalSizeCnt++;
118-
119-
if (readBytesCnt < 4194240){
120-
if (readBytesCnt > 0)
121-
bos.write(chunk, 0, readBytesCnt);
122-
break;
123-
}
124-
125-
bos.write(chunk);
117+
private void mergeChunksToFile() throws Exception{
118+
double chunkPercent = (4194240.0 / (chunksTotalSize / 100.0) / 100.0);
119+
long totalSizeCnt = 0;
120+
121+
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(resultFile));
122+
123+
BufferedInputStream bis;
124+
byte[] chunk;
125+
int readBytesCnt;
126+
127+
for (File chunkFile : chunkFiles){
128+
bis = new BufferedInputStream(new FileInputStream(chunkFile));
129+
while (true){
130+
131+
if (this.isCancelled()){
132+
bos.close();
133+
bis.close();
134+
boolean isDeleted = resultFile.delete();
135+
throw new InterruptedException("Merge task interrupted and file "
136+
+ (isDeleted ? "deleted." : "is not deleted."));
126137
}
127-
bis.close();
128-
}
129-
bos.close();
130-
//=============== let's check what we have ==============
131-
long resultFileSize = resultFile.length();
132-
logPrinter.print("Total chunks size: " + cnkTotalSize, EMsgType.INFO);
133-
logPrinter.print("Merged file size: " + resultFileSize, EMsgType.INFO);
134-
135-
if (cnkTotalSize != resultFileSize){
136-
logPrinter.print("Sizes are different! Do NOT use this file for installations!", EMsgType.FAIL);
137-
return false;
138+
139+
chunk = new byte[4194240];
140+
readBytesCnt = bis.read(chunk);
141+
142+
logPrinter.updateProgress(chunkPercent * totalSizeCnt);
143+
totalSizeCnt++;
144+
145+
if (readBytesCnt < 4194240){
146+
if (readBytesCnt > 0)
147+
bos.write(chunk, 0, readBytesCnt);
148+
break;
149+
}
150+
151+
bos.write(chunk);
138152
}
139-
logPrinter.print("Sizes are the same! Split file should be good!", EMsgType.PASS);
140-
}
141-
catch (Exception e){
142-
e.printStackTrace();
143-
logPrinter.print("Error: "+e.getMessage(), EMsgType.FAIL);
153+
bis.close();
144154
}
155+
bos.close();
156+
}
157+
158+
private void validateFile() throws Exception{
159+
long resultFileSize = resultFile.length();
160+
logPrinter.print("Total chunks size: " + chunksTotalSize, EMsgType.INFO);
161+
logPrinter.print("Merged file size: " + resultFileSize, EMsgType.INFO);
162+
163+
if (chunksTotalSize != resultFileSize)
164+
throw new Exception("Sizes are different! Do NOT use this file for installations!");
145165

146-
logPrinter.print("Merge task complete!", EMsgType.INFO);
147-
logPrinter.close();
148-
return true;
166+
logPrinter.print("Sizes are the same! Resulting file should be good!", EMsgType.PASS);
149167
}
150168
}

0 commit comments

Comments
 (0)