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
protected String call() {
if (isDriversDownloaded())
return driversInstallerFile.getAbsolutePath();
if (downloadDrivers())
if (isDriversDownloaded() || downloadDrivers())
return driversInstallerFile.getAbsolutePath();
return null;
}

View file

@ -33,6 +33,12 @@ public class MergeTask extends Task<Boolean> {
private final String saveToPath;
private final String filePath;
private File splitFile;
private File[] chunkFiles;
private long chunksTotalSize;
private File resultFile;
public MergeTask(String filePath, String saveToPath) {
this.filePath = filePath;
this.saveToPath = saveToPath;
@ -40,74 +46,94 @@ public class MergeTask extends Task<Boolean> {
}
@Override
protected Boolean call() {
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]$"));
if (chunkFiles == null || chunkFiles.length == 0){
logPrinter.print("Selected folder doesn't have any chunks. Nothing to do here.", EMsgType.FAIL);
logPrinter.print("Merge task complete!", EMsgType.INFO);
logPrinter.close();
return true;
}
catch (Exception e){
logPrinter.print(e.getMessage(), EMsgType.FAIL);
logPrinter.close();
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);
}
private void calculateChunksSizeSum(){
logPrinter.print("Next files will be merged in following order: ", EMsgType.INFO);
for (File cnk : chunkFiles){
logPrinter.print(" "+cnk.getName(), EMsgType.INFO);
cnkTotalSize += cnk.length();
chunksTotalSize += cnk.length();
}
}
double chunkPercent = (4194240.0 / (cnkTotalSize / 100.0) / 100.0);
long totalSizeCnt = 0;
private void createFile() throws Exception{
final String splitFileName = splitFile.getName();
File resultFile = new File(saveToPath+File.separator+"!_"+folder.getName());
//*******
for (int i = 0; ; i++){
resultFile = new File(saveToPath+File.separator+"!_"+splitFileName);
for (int i = 0; i < 50 ; i++){
if (this.isCancelled()){
logPrinter.print("Split task interrupted!", EMsgType.PASS);
logPrinter.close();
return false;
throw new InterruptedException("Split task interrupted!");
}
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);
resultFile = new File(saveToPath+File.separator+"!_"+i+"_"+folder.getName());
resultFile = new File(saveToPath+File.separator+"!_"+i+"_"+splitFileName);
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));
BufferedInputStream bis;
byte[] chunk;
int readBytesCnt;
for (File cnk : chunkFiles){
bis = new BufferedInputStream(new FileInputStream(cnk));
for (File chunkFile : chunkFiles){
bis = new BufferedInputStream(new FileInputStream(chunkFile));
while (true){
if (this.isCancelled()){
bos.close();
bis.close();
boolean isDeleted = resultFile.delete();
logPrinter.print("Split task interrupted and file "+(isDeleted?"deleted.":"is not deleted."), EMsgType.PASS);
logPrinter.close();
return false;
throw new InterruptedException("Merge task interrupted and file "
+ (isDeleted ? "deleted." : "is not deleted."));
}
chunk = new byte[4194240];
@ -127,24 +153,16 @@ public class MergeTask extends Task<Boolean> {
bis.close();
}
bos.close();
//=============== let's check what we have ==============
}
private void validateFile() throws Exception{
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);
if (cnkTotalSize != resultFileSize){
logPrinter.print("Sizes are different! Do NOT use this file for installations!", EMsgType.FAIL);
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);
}
if (chunksTotalSize != resultFileSize)
throw new Exception("Sizes are different! Do NOT use this file for installations!");
logPrinter.print("Merge task complete!", EMsgType.INFO);
logPrinter.close();
return true;
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 filePath;
private File file;
private File splitFile;
private long originalFileLen;
public SplitTask(String filePath, String saveToPath){
this.filePath = filePath;
this.saveToPath = saveToPath;
@ -41,46 +45,57 @@ public class SplitTask extends Task<Boolean> {
@Override
protected Boolean call() {
File file = new File(filePath);
File folder = new File(saveToPath+File.separator+"!_"+file.getName());
try {
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()){
logPrinter.print("Split task interrupted!", EMsgType.PASS);
logPrinter.close();
return false;
throw new InterruptedException("Split task interrupted!");
}
if (! folder.mkdir()){
if (folder.exists()){
if (i >= 50){
logPrinter.print("Can't create new file.", EMsgType.FAIL);
logPrinter.close();
return false;
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);
folder = new File(saveToPath+File.separator+"!_"+i+"_"+file.getName());
splitFile = 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;
throw new Exception("Folder " + splitFile.getAbsolutePath()
+ " could not be created. Not enough rights or something like that?");
}
}
logPrinter.print("Save results to: "+folder.getAbsolutePath(), EMsgType.INFO);
break;
throw new Exception("Can't create new file.");
}
try{
private void splitFileToChunks() throws Exception{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream fragmentBos;
long counter;
long originalFileLen = file.length();
originalFileLen = file.length();
double chunkPercent = (4194240.0 / (originalFileLen / 100.0) / 100.0);
long totalSizeCnt = 0;
@ -90,9 +105,8 @@ public class SplitTask extends Task<Boolean> {
main_loop:
for (int i = 0; ; i++){
fragmentBos = new BufferedOutputStream(
new FileOutputStream(new File(folder.getAbsolutePath()+File.separator+String.format("%02d", i)))
);
String pathname = splitFile.getAbsolutePath()+File.separator+String.format("%02d", i);
BufferedOutputStream fragmentBos = new BufferedOutputStream(new FileOutputStream(new File(pathname)));
counter = 0;
@ -101,17 +115,17 @@ public class SplitTask extends Task<Boolean> {
if (this.isCancelled()){
fragmentBos.close();
bis.close();
boolean isDeleted = folder.delete();
File[] chArrToDel = folder.listFiles();
if (! isDeleted && chArrToDel != null){
boolean isDeleted = splitFile.delete();
File[] chunksToDelete = splitFile.listFiles();
if (! isDeleted && chunksToDelete != null){
isDeleted = true;
for (File chunkFile : chArrToDel)
for (File chunkFile : chunksToDelete)
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();
return false;
throw new InterruptedException("Split task interrupted and folder "
+ (isDeleted?"deleted.":"is not deleted."));
}
chunk = new byte[4194240];
@ -132,20 +146,19 @@ public class SplitTask extends Task<Boolean> {
}
fragmentBos.close();
}
bis.close();
}
//=============== let's check what we have ==============
private void validateSplitFile() throws Exception{
logPrinter.print("Original file size: "+originalFileLen, EMsgType.INFO);
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);
for (File chunkFile : chunkFileArr) {
logPrinter.print("Chunk " + chunkFile.getName() + " size: " + chunkFile.length(), EMsgType.INFO);
totalChunksSize += chunkFile.length();
@ -154,18 +167,9 @@ public class SplitTask extends Task<Boolean> {
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){
e.printStackTrace();
logPrinter.print("Error: "+e.getMessage(), EMsgType.FAIL);
}
logPrinter.print("Split task complete!", EMsgType.INFO);
logPrinter.close();
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;
}
}