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,111 +46,123 @@ 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); logPrinter.print("Save results to: "+resultFile.getAbsolutePath(), EMsgType.INFO);
break; return;
} }
//******* throw new Exception("Can't create new file.");
}
try { private void mergeChunksToFile() throws Exception{
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(resultFile)); double chunkPercent = (4194240.0 / (chunksTotalSize / 100.0) / 100.0);
long totalSizeCnt = 0;
BufferedInputStream bis; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(resultFile));
byte[] chunk;
int readBytesCnt;
for (File cnk : chunkFiles){ BufferedInputStream bis;
bis = new BufferedInputStream(new FileInputStream(cnk)); byte[] chunk;
while (true){ int readBytesCnt;
if (this.isCancelled()){ for (File chunkFile : chunkFiles){
bos.close(); bis = new BufferedInputStream(new FileInputStream(chunkFile));
bis.close(); while (true){
boolean isDeleted = resultFile.delete();
logPrinter.print("Split task interrupted and file "+(isDeleted?"deleted.":"is not deleted."), EMsgType.PASS);
logPrinter.close();
return false;
}
chunk = new byte[4194240]; if (this.isCancelled()){
readBytesCnt = bis.read(chunk); bos.close();
bis.close();
logPrinter.updateProgress(chunkPercent * totalSizeCnt); boolean isDeleted = resultFile.delete();
totalSizeCnt++; throw new InterruptedException("Merge task interrupted and file "
+ (isDeleted ? "deleted." : "is not deleted."));
if (readBytesCnt < 4194240){
if (readBytesCnt > 0)
bos.write(chunk, 0, readBytesCnt);
break;
}
bos.write(chunk);
} }
bis.close();
}
bos.close();
//=============== let's check what we have ==============
long resultFileSize = resultFile.length();
logPrinter.print("Total chunks size: " + cnkTotalSize, EMsgType.INFO);
logPrinter.print("Merged file size: " + resultFileSize, EMsgType.INFO);
if (cnkTotalSize != resultFileSize){ chunk = new byte[4194240];
logPrinter.print("Sizes are different! Do NOT use this file for installations!", EMsgType.FAIL); readBytesCnt = bis.read(chunk);
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.updateProgress(chunkPercent * totalSizeCnt);
logPrinter.close(); totalSizeCnt++;
return true;
if (readBytesCnt < 4194240){
if (readBytesCnt > 0)
bos.write(chunk, 0, readBytesCnt);
break;
}
bos.write(chunk);
}
bis.close();
}
bos.close();
}
private void validateFile() throws Exception{
long resultFileSize = resultFile.length();
logPrinter.print("Total chunks size: " + chunksTotalSize, EMsgType.INFO);
logPrinter.print("Merged file size: " + resultFileSize, EMsgType.INFO);
if (chunksTotalSize != resultFileSize)
throw new Exception("Sizes are different! Do NOT use this file for installations!");
logPrinter.print("Sizes are the same! Resulting file should be good!", EMsgType.PASS);
} }
} }

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,131 +45,131 @@ 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);
this.file = new File(filePath);
logPrinter.print("Split file: "+filePath, EMsgType.INFO); createSplitFile();
splitFileToChunks();
validateSplitFile();
for (int i = 0; ; i++){ logPrinter.print("Split task complete!", EMsgType.INFO);
if (this.isCancelled()){ logPrinter.close();
logPrinter.print("Split task interrupted!", EMsgType.PASS); return true;
logPrinter.close();
return false;
}
if (! folder.mkdir()){
if (folder.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 folder...", EMsgType.WARNING);
folder = new File(saveToPath+File.separator+"!_"+i+"_"+file.getName());
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);
logPrinter.close();
return false;
}
}
logPrinter.print("Save results to: "+folder.getAbsolutePath(), EMsgType.INFO);
break;
}
try{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream fragmentBos;
long counter;
long originalFileLen = file.length();
double chunkPercent = (4194240.0 / (originalFileLen / 100.0) / 100.0);
long totalSizeCnt = 0;
byte[] chunk;
int readBytesCnt;
main_loop:
for (int i = 0; ; i++){
fragmentBos = new BufferedOutputStream(
new FileOutputStream(new File(folder.getAbsolutePath()+File.separator+String.format("%02d", i)))
);
counter = 0;
while (counter < 1024){ // 0xffff0000 total
if (this.isCancelled()){
fragmentBos.close();
bis.close();
boolean isDeleted = folder.delete();
File[] chArrToDel = folder.listFiles();
if (! isDeleted && chArrToDel != null){
isDeleted = true;
for (File chunkFile : chArrToDel)
isDeleted &= chunkFile.delete();
isDeleted &= folder.delete();
}
logPrinter.print("Split task interrupted and folder "+(isDeleted?"deleted.":"is not deleted."), EMsgType.PASS);
logPrinter.close();
return false;
}
chunk = new byte[4194240];
if ((readBytesCnt = bis.read(chunk)) < 4194240){
if (readBytesCnt > 0)
fragmentBos.write(chunk, 0, readBytesCnt);
fragmentBos.close();
logPrinter.updateProgress(1.0);
break main_loop;
}
fragmentBos.write(chunk);
logPrinter.updateProgress(chunkPercent * totalSizeCnt);
counter++; // NOTE: here we have some redundancy of variables. It has to be fixed one day.
totalSizeCnt++;
}
fragmentBos.close();
}
bis.close();
//=============== let's check what we have ==============
logPrinter.print("Original file size: "+originalFileLen, EMsgType.INFO);
long totalChunksSize = 0;
File[] chunkFileArr = folder.listFiles();
if (chunkFileArr == null) {
logPrinter.print("Unable to check results. It means that something went wrong.", EMsgType.FAIL);
return false;
}
else {
Arrays.sort(chunkFileArr);
for (File chunkFile : chunkFileArr) {
logPrinter.print("Chunk " + chunkFile.getName() + " size: " + chunkFile.length(), EMsgType.INFO);
totalChunksSize += chunkFile.length();
}
logPrinter.print("Total chunks size: " + totalChunksSize, EMsgType.INFO);
if (originalFileLen != totalChunksSize)
logPrinter.print("Sizes are different! Do NOT use this file for installations!", EMsgType.FAIL);
else
logPrinter.print("Sizes are the same! Split file should be good!", EMsgType.PASS);
}
} }
catch (Exception e){ catch (Exception e){
e.printStackTrace(); logPrinter.print(e.getMessage(), EMsgType.FAIL);
logPrinter.print("Error: "+e.getMessage(), EMsgType.FAIL); logPrinter.close();
return false;
} }
logPrinter.print("Split task complete!", EMsgType.INFO); }
logPrinter.close();
private void createSplitFile() throws Exception{
splitFile = new File(saveToPath+File.separator+"!_"+file.getName());
for (int i = 0; i < 50 ; i++){
if (this.isCancelled()){
throw new InterruptedException("Split task interrupted!");
}
if (splitFile.mkdir()){
logPrinter.print("Save results to: "+splitFile.getAbsolutePath(), EMsgType.INFO);
return;
}
if (splitFile.exists()){
logPrinter.print("Trying to create a good new folder...", EMsgType.WARNING);
splitFile = new File(saveToPath+File.separator+"!_"+i+"_"+file.getName());
continue;
}
throw new Exception("Folder " + splitFile.getAbsolutePath()
+ " could not be created. Not enough rights or something like that?");
}
throw new Exception("Can't create new file.");
}
private void splitFileToChunks() throws Exception{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
long counter;
originalFileLen = file.length();
double chunkPercent = (4194240.0 / (originalFileLen / 100.0) / 100.0);
long totalSizeCnt = 0;
byte[] chunk;
int readBytesCnt;
main_loop:
for (int i = 0; ; i++){
String pathname = splitFile.getAbsolutePath()+File.separator+String.format("%02d", i);
BufferedOutputStream fragmentBos = new BufferedOutputStream(new FileOutputStream(new File(pathname)));
counter = 0;
while (counter < 1024){ // 0xffff0000 total
if (this.isCancelled()){
fragmentBos.close();
bis.close();
boolean isDeleted = splitFile.delete();
File[] chunksToDelete = splitFile.listFiles();
if (! isDeleted && chunksToDelete != null){
isDeleted = true;
for (File chunkFile : chunksToDelete)
isDeleted &= chunkFile.delete();
isDeleted &= splitFile.delete();
}
throw new InterruptedException("Split task interrupted and folder "
+ (isDeleted?"deleted.":"is not deleted."));
}
chunk = new byte[4194240];
if ((readBytesCnt = bis.read(chunk)) < 4194240){
if (readBytesCnt > 0)
fragmentBos.write(chunk, 0, readBytesCnt);
fragmentBos.close();
logPrinter.updateProgress(1.0);
break main_loop;
}
fragmentBos.write(chunk);
logPrinter.updateProgress(chunkPercent * totalSizeCnt);
counter++; // NOTE: here we have some redundancy of variables. It has to be fixed one day.
totalSizeCnt++;
}
fragmentBos.close();
}
bis.close();
}
private void validateSplitFile() throws Exception{
logPrinter.print("Original file size: "+originalFileLen, EMsgType.INFO);
long totalChunksSize = 0;
File[] chunkFileArr = splitFile.listFiles();
if (chunkFileArr == null)
throw new Exception("Unable to check results. It means that something went wrong.");
Arrays.sort(chunkFileArr);
for (File chunkFile : chunkFileArr) {
logPrinter.print("Chunk " + chunkFile.getName() + " size: " + chunkFile.length(), EMsgType.INFO);
totalChunksSize += chunkFile.length();
}
logPrinter.print("Total chunks size: " + totalChunksSize, EMsgType.INFO);
if (originalFileLen != totalChunksSize)
throw new Exception("Sizes are different! Do NOT use this file for installations!");
logPrinter.print("Sizes are the same! Split file should be good!", EMsgType.PASS);
return true;
} }
} }