Make things more clean inside SplitTask.java and MergeTask.java

This commit is contained in:
Dmitry Isaenko 2020-07-12 05:34:57 +03:00
parent 6993b89d52
commit 3592c9086b
3 changed files with 217 additions and 197 deletions

View file

@ -32,9 +32,7 @@ public class DownloadDriversTask extends Task<String> {
@Override @Override
protected String call() { protected String call() {
if (isDriversDownloaded()) if (isDriversDownloaded() || downloadDrivers())
return driversInstallerFile.getAbsolutePath();
if (downloadDrivers())
return driversInstallerFile.getAbsolutePath(); return driversInstallerFile.getAbsolutePath();
return null; return null;
} }

View file

@ -33,6 +33,12 @@ public class MergeTask extends Task<Boolean> {
private final String saveToPath; private final String saveToPath;
private final String filePath; private final String filePath;
private File splitFile;
private File[] chunkFiles;
private long chunksTotalSize;
private File resultFile;
public MergeTask(String filePath, String saveToPath) { public MergeTask(String filePath, String saveToPath) {
this.filePath = filePath; this.filePath = filePath;
this.saveToPath = saveToPath; this.saveToPath = saveToPath;
@ -40,74 +46,94 @@ public class MergeTask extends Task<Boolean> {
} }
@Override @Override
protected Boolean call() { protected Boolean call() {
logPrinter.print("Merge file: "+filePath, EMsgType.INFO); try {
logPrinter.print("Merge file: " + filePath, EMsgType.INFO);
splitFile = new File(filePath);
File folder = new File(filePath); collectChunks();
validateChunks();
sortChunks();
calculateChunksSizeSum();
long cnkTotalSize = 0; createFile();
mergeChunksToFile();
validateFile();
File[] chunkFiles = folder.listFiles((file, s) -> s.matches("^[0-9][0-9]$")); logPrinter.print("Merge task complete!", EMsgType.INFO);
logPrinter.close();
if (chunkFiles == null || chunkFiles.length == 0){ return true;
logPrinter.print("Selected folder doesn't have any chunks. Nothing to do here.", EMsgType.FAIL); }
catch (Exception e){
logPrinter.print(e.getMessage(), EMsgType.FAIL);
logPrinter.close(); logPrinter.close();
return false; return false;
} }
}
private void collectChunks(){
chunkFiles = splitFile.listFiles((file, s) -> s.matches("^[0-9][0-9]$"));
}
private void validateChunks() throws Exception{
if (chunkFiles == null || chunkFiles.length == 0){
throw new Exception("Selected folder doesn't have any chunks. Nothing to do here.");
}
}
private void sortChunks(){
Arrays.sort(chunkFiles); Arrays.sort(chunkFiles);
}
private void calculateChunksSizeSum(){
logPrinter.print("Next files will be merged in following order: ", EMsgType.INFO); logPrinter.print("Next files will be merged in following order: ", EMsgType.INFO);
for (File cnk : chunkFiles){ for (File cnk : chunkFiles){
logPrinter.print(" "+cnk.getName(), EMsgType.INFO); logPrinter.print(" "+cnk.getName(), EMsgType.INFO);
cnkTotalSize += cnk.length(); chunksTotalSize += cnk.length();
}
} }
double chunkPercent = (4194240.0 / (cnkTotalSize / 100.0) / 100.0); private void createFile() throws Exception{
long totalSizeCnt = 0; final String splitFileName = splitFile.getName();
File resultFile = new File(saveToPath+File.separator+"!_"+folder.getName()); resultFile = new File(saveToPath+File.separator+"!_"+splitFileName);
//*******
for (int i = 0; ; i++){ for (int i = 0; i < 50 ; i++){
if (this.isCancelled()){ if (this.isCancelled()){
logPrinter.print("Split task interrupted!", EMsgType.PASS); throw new InterruptedException("Split task interrupted!");
logPrinter.close();
return false;
} }
if (resultFile.exists()){ if (resultFile.exists()){
if (i >= 50){
logPrinter.print("Can't create new file.", EMsgType.FAIL);
logPrinter.close();
return false;
}
logPrinter.print("Trying to create a good new file...", EMsgType.WARNING); logPrinter.print("Trying to create a good new file...", EMsgType.WARNING);
resultFile = new File(saveToPath+File.separator+"!_"+i+"_"+folder.getName()); resultFile = new File(saveToPath+File.separator+"!_"+i+"_"+splitFileName);
continue; continue;
} }
logPrinter.print("Save results to: "+resultFile.getAbsolutePath(), EMsgType.INFO);
break;
}
//*******
try { logPrinter.print("Save results to: "+resultFile.getAbsolutePath(), EMsgType.INFO);
return;
}
throw new Exception("Can't create new file.");
}
private void mergeChunksToFile() throws Exception{
double chunkPercent = (4194240.0 / (chunksTotalSize / 100.0) / 100.0);
long totalSizeCnt = 0;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(resultFile)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(resultFile));
BufferedInputStream bis; BufferedInputStream bis;
byte[] chunk; byte[] chunk;
int readBytesCnt; int readBytesCnt;
for (File cnk : chunkFiles){ for (File chunkFile : chunkFiles){
bis = new BufferedInputStream(new FileInputStream(cnk)); bis = new BufferedInputStream(new FileInputStream(chunkFile));
while (true){ while (true){
if (this.isCancelled()){ if (this.isCancelled()){
bos.close(); bos.close();
bis.close(); bis.close();
boolean isDeleted = resultFile.delete(); boolean isDeleted = resultFile.delete();
logPrinter.print("Split task interrupted and file "+(isDeleted?"deleted.":"is not deleted."), EMsgType.PASS); throw new InterruptedException("Merge task interrupted and file "
logPrinter.close(); + (isDeleted ? "deleted." : "is not deleted."));
return false;
} }
chunk = new byte[4194240]; chunk = new byte[4194240];
@ -127,24 +153,16 @@ public class MergeTask extends Task<Boolean> {
bis.close(); bis.close();
} }
bos.close(); bos.close();
//=============== let's check what we have ============== }
private void validateFile() throws Exception{
long resultFileSize = resultFile.length(); long resultFileSize = resultFile.length();
logPrinter.print("Total chunks size: " + cnkTotalSize, EMsgType.INFO); logPrinter.print("Total chunks size: " + chunksTotalSize, EMsgType.INFO);
logPrinter.print("Merged file size: " + resultFileSize, EMsgType.INFO); logPrinter.print("Merged file size: " + resultFileSize, EMsgType.INFO);
if (cnkTotalSize != resultFileSize){ if (chunksTotalSize != resultFileSize)
logPrinter.print("Sizes are different! Do NOT use this file for installations!", EMsgType.FAIL); throw new Exception("Sizes are different! Do NOT use this file for installations!");
return false;
}
logPrinter.print("Sizes are the same! Split file should be good!", EMsgType.PASS);
}
catch (Exception e){
e.printStackTrace();
logPrinter.print("Error: "+e.getMessage(), EMsgType.FAIL);
}
logPrinter.print("Merge task complete!", EMsgType.INFO); logPrinter.print("Sizes are the same! Resulting file should be good!", EMsgType.PASS);
logPrinter.close();
return true;
} }
} }

View file

@ -33,6 +33,10 @@ public class SplitTask extends Task<Boolean> {
private final String saveToPath; private final String saveToPath;
private final String filePath; private final String filePath;
private File file;
private File splitFile;
private long originalFileLen;
public SplitTask(String filePath, String saveToPath){ public SplitTask(String filePath, String saveToPath){
this.filePath = filePath; this.filePath = filePath;
this.saveToPath = saveToPath; this.saveToPath = saveToPath;
@ -41,46 +45,57 @@ public class SplitTask extends Task<Boolean> {
@Override @Override
protected Boolean call() { protected Boolean call() {
File file = new File(filePath); try {
File folder = new File(saveToPath+File.separator+"!_"+file.getName());
logPrinter.print("Split file: "+filePath, EMsgType.INFO); logPrinter.print("Split file: "+filePath, EMsgType.INFO);
this.file = new File(filePath);
createSplitFile();
splitFileToChunks();
validateSplitFile();
logPrinter.print("Split task complete!", EMsgType.INFO);
logPrinter.close();
return true;
}
catch (Exception e){
logPrinter.print(e.getMessage(), EMsgType.FAIL);
logPrinter.close();
return false;
}
}
private void createSplitFile() throws Exception{
splitFile = new File(saveToPath+File.separator+"!_"+file.getName());
for (int i = 0; i < 50 ; i++){
for (int i = 0; ; i++){
if (this.isCancelled()){ if (this.isCancelled()){
logPrinter.print("Split task interrupted!", EMsgType.PASS); throw new InterruptedException("Split task interrupted!");
logPrinter.close();
return false;
} }
if (! folder.mkdir()){
if (folder.exists()){ if (splitFile.mkdir()){
if (i >= 50){ logPrinter.print("Save results to: "+splitFile.getAbsolutePath(), EMsgType.INFO);
logPrinter.print("Can't create new file.", EMsgType.FAIL); return;
logPrinter.close();
return false;
} }
if (splitFile.exists()){
logPrinter.print("Trying to create a good new folder...", EMsgType.WARNING); logPrinter.print("Trying to create a good new folder...", EMsgType.WARNING);
folder = new File(saveToPath+File.separator+"!_"+i+"_"+file.getName()); splitFile = new File(saveToPath+File.separator+"!_"+i+"_"+file.getName());
continue; continue;
} }
else { // folder not created and not exists - return
logPrinter.print("Folder "+folder.getAbsolutePath()+" could not be created. Not enough rights or something like that?", EMsgType.FAIL); throw new Exception("Folder " + splitFile.getAbsolutePath()
logPrinter.close(); + " could not be created. Not enough rights or something like that?");
return false;
} }
} throw new Exception("Can't create new file.");
logPrinter.print("Save results to: "+folder.getAbsolutePath(), EMsgType.INFO);
break;
} }
try{ private void splitFileToChunks() throws Exception{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream fragmentBos;
long counter; long counter;
long originalFileLen = file.length(); originalFileLen = file.length();
double chunkPercent = (4194240.0 / (originalFileLen / 100.0) / 100.0); double chunkPercent = (4194240.0 / (originalFileLen / 100.0) / 100.0);
long totalSizeCnt = 0; long totalSizeCnt = 0;
@ -90,9 +105,8 @@ public class SplitTask extends Task<Boolean> {
main_loop: main_loop:
for (int i = 0; ; i++){ for (int i = 0; ; i++){
fragmentBos = new BufferedOutputStream( String pathname = splitFile.getAbsolutePath()+File.separator+String.format("%02d", i);
new FileOutputStream(new File(folder.getAbsolutePath()+File.separator+String.format("%02d", i))) BufferedOutputStream fragmentBos = new BufferedOutputStream(new FileOutputStream(new File(pathname)));
);
counter = 0; counter = 0;
@ -101,17 +115,17 @@ public class SplitTask extends Task<Boolean> {
if (this.isCancelled()){ if (this.isCancelled()){
fragmentBos.close(); fragmentBos.close();
bis.close(); bis.close();
boolean isDeleted = folder.delete(); boolean isDeleted = splitFile.delete();
File[] chArrToDel = folder.listFiles(); File[] chunksToDelete = splitFile.listFiles();
if (! isDeleted && chArrToDel != null){ if (! isDeleted && chunksToDelete != null){
isDeleted = true; isDeleted = true;
for (File chunkFile : chArrToDel) for (File chunkFile : chunksToDelete)
isDeleted &= chunkFile.delete(); isDeleted &= chunkFile.delete();
isDeleted &= folder.delete(); isDeleted &= splitFile.delete();
} }
logPrinter.print("Split task interrupted and folder "+(isDeleted?"deleted.":"is not deleted."), EMsgType.PASS);
logPrinter.close(); throw new InterruptedException("Split task interrupted and folder "
return false; + (isDeleted?"deleted.":"is not deleted."));
} }
chunk = new byte[4194240]; chunk = new byte[4194240];
@ -132,20 +146,19 @@ public class SplitTask extends Task<Boolean> {
} }
fragmentBos.close(); fragmentBos.close();
} }
bis.close(); bis.close();
}
//=============== let's check what we have ============== private void validateSplitFile() throws Exception{
logPrinter.print("Original file size: "+originalFileLen, EMsgType.INFO); logPrinter.print("Original file size: "+originalFileLen, EMsgType.INFO);
long totalChunksSize = 0; long totalChunksSize = 0;
File[] chunkFileArr = folder.listFiles(); File[] chunkFileArr = splitFile.listFiles();
if (chunkFileArr == null)
throw new Exception("Unable to check results. It means that something went wrong.");
if (chunkFileArr == null) {
logPrinter.print("Unable to check results. It means that something went wrong.", EMsgType.FAIL);
return false;
}
else {
Arrays.sort(chunkFileArr); Arrays.sort(chunkFileArr);
for (File chunkFile : chunkFileArr) { for (File chunkFile : chunkFileArr) {
logPrinter.print("Chunk " + chunkFile.getName() + " size: " + chunkFile.length(), EMsgType.INFO); logPrinter.print("Chunk " + chunkFile.getName() + " size: " + chunkFile.length(), EMsgType.INFO);
totalChunksSize += chunkFile.length(); totalChunksSize += chunkFile.length();
@ -154,18 +167,9 @@ public class SplitTask extends Task<Boolean> {
logPrinter.print("Total chunks size: " + totalChunksSize, EMsgType.INFO); logPrinter.print("Total chunks size: " + totalChunksSize, EMsgType.INFO);
if (originalFileLen != totalChunksSize) if (originalFileLen != totalChunksSize)
logPrinter.print("Sizes are different! Do NOT use this file for installations!", EMsgType.FAIL); throw new Exception("Sizes are different! Do NOT use this file for installations!");
else
logPrinter.print("Sizes are the same! Split file should be good!", EMsgType.PASS); logPrinter.print("Sizes are the same! Split file should be good!", EMsgType.PASS);
}
}
catch (Exception e){
e.printStackTrace();
logPrinter.print("Error: "+e.getMessage(), EMsgType.FAIL);
}
logPrinter.print("Split task complete!", EMsgType.INFO);
logPrinter.close();
return true;
} }
} }