Split 'SplitMerge' class into 2 normal

Update NETCommunications: installation speed now 2x faster
This commit is contained in:
Dmitry Isaenko 2020-07-11 15:42:17 +03:00
parent 267ffcf5d2
commit 6993b89d52
8 changed files with 167 additions and 145 deletions

View file

@ -307,7 +307,7 @@ public class NETCommunications implements Runnable {
private void handleSplitFile(File file, long start, long end) throws Exception{ private void handleSplitFile(File file, long start, long end) throws Exception{
long count = end - start + 1; long count = end - start + 1;
int readPice = 8388608; int readPice = 1024;// NOTE: keep it small for better speed
byte[] byteBuf; byte[] byteBuf;
long currentOffset = 0; long currentOffset = 0;
@ -333,7 +333,7 @@ public class NETCommunications implements Runnable {
private void handleRegularFile(File file, long start, long end) throws Exception{ private void handleRegularFile(File file, long start, long end) throws Exception{
long count = end - start + 1; long count = end - start + 1;
int readPice = 8388608; int readPice = 1024; // NOTE: keep it small for better speed
byte[] byteBuf; byte[] byteBuf;
long currentOffset = 0; long currentOffset = 0;

View file

@ -33,7 +33,8 @@ import nsusbloader.AppPreferences;
import nsusbloader.MediatorControl; import nsusbloader.MediatorControl;
import nsusbloader.NSLDataTypes.EModule; import nsusbloader.NSLDataTypes.EModule;
import nsusbloader.ServiceWindow; import nsusbloader.ServiceWindow;
import nsusbloader.Utilities.SplitMergeTool; import nsusbloader.Utilities.splitmerge.MergeTask;
import nsusbloader.Utilities.splitmerge.SplitTask;
import java.io.File; import java.io.File;
import java.net.URL; import java.net.URL;
@ -204,9 +205,9 @@ public class SplitMergeController implements Initializable {
} }
if (splitRad.isSelected()) if (splitRad.isSelected())
smTask = SplitMergeTool.splitFile(fileFolderActualPathLbl.getText(), saveToPathLbl.getText()); smTask = new SplitTask(fileFolderActualPathLbl.getText(), saveToPathLbl.getText());
else else
smTask = SplitMergeTool.mergeFile(fileFolderActualPathLbl.getText(), saveToPathLbl.getText()); smTask = new MergeTask(fileFolderActualPathLbl.getText(), saveToPathLbl.getText());
smTask.setOnCancelled(event -> statusLbl.setText(resourceBundle.getString("failure_txt"))); smTask.setOnCancelled(event -> statusLbl.setText(resourceBundle.getString("failure_txt")));
smTask.setOnSucceeded(event -> { smTask.setOnSucceeded(event -> {
if (smTask.getValue()) if (smTask.getValue())

View file

@ -0,0 +1,150 @@
/*
Copyright 2019-2020 Dmitry Isaenko
This file is part of NS-USBloader.
NS-USBloader is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NS-USBloader is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
*/
package nsusbloader.Utilities.splitmerge;
import javafx.concurrent.Task;
import nsusbloader.ModelControllers.ILogPrinter;
import nsusbloader.ModelControllers.Log;
import nsusbloader.NSLDataTypes.EModule;
import nsusbloader.NSLDataTypes.EMsgType;
import java.io.*;
import java.util.Arrays;
public class MergeTask extends Task<Boolean> {
private final ILogPrinter logPrinter;
private final String saveToPath;
private final String filePath;
public MergeTask(String filePath, String saveToPath) {
this.filePath = filePath;
this.saveToPath = saveToPath;
logPrinter = Log.getPrinter(EModule.SPLIT_MERGE_TOOL);
}
@Override
protected Boolean call() {
logPrinter.print("Merge file: "+filePath, EMsgType.INFO);
File folder = new File(filePath);
long cnkTotalSize = 0;
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.close();
return false;
}
Arrays.sort(chunkFiles);
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();
}
double chunkPercent = (4194240.0 / (cnkTotalSize / 100.0) / 100.0);
long totalSizeCnt = 0;
File resultFile = new File(saveToPath+File.separator+"!_"+folder.getName());
//*******
for (int i = 0; ; i++){
if (this.isCancelled()){
logPrinter.print("Split task interrupted!", EMsgType.PASS);
logPrinter.close();
return false;
}
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());
continue;
}
logPrinter.print("Save results to: "+resultFile.getAbsolutePath(), EMsgType.INFO);
break;
}
//*******
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(resultFile));
BufferedInputStream bis;
byte[] chunk;
int readBytesCnt;
for (File cnk : chunkFiles){
bis = new BufferedInputStream(new FileInputStream(cnk));
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;
}
chunk = new byte[4194240];
readBytesCnt = bis.read(chunk);
logPrinter.updateProgress(chunkPercent * totalSizeCnt);
totalSizeCnt++;
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){
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);
}
logPrinter.print("Merge task complete!", EMsgType.INFO);
logPrinter.close();
return true;
}
}

View file

@ -16,7 +16,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>. along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
*/ */
package nsusbloader.Utilities; package nsusbloader.Utilities.splitmerge;
import javafx.concurrent.Task; import javafx.concurrent.Task;
import nsusbloader.ModelControllers.ILogPrinter; import nsusbloader.ModelControllers.ILogPrinter;
@ -27,24 +27,13 @@ import nsusbloader.NSLDataTypes.EMsgType;
import java.io.*; import java.io.*;
import java.util.Arrays; import java.util.Arrays;
public class SplitMergeTool { public class SplitTask extends Task<Boolean> {
public static Task<Boolean> splitFile(String filePath, String saveToPath){ private final ILogPrinter logPrinter;
return new SplitTask(filePath, saveToPath); private final String saveToPath;
}; private final String filePath;
public static Task<Boolean> mergeFile(String filePath, String saveToPath){ public SplitTask(String filePath, String saveToPath){
return new MergeTask(filePath, saveToPath);
}
}
class SplitTask extends Task<Boolean>{
private ILogPrinter logPrinter;
private String saveToPath;
private String filePath;
SplitTask(String filePath, String saveToPath){
this.filePath = filePath; this.filePath = filePath;
this.saveToPath = saveToPath; this.saveToPath = saveToPath;
logPrinter = Log.getPrinter(EModule.SPLIT_MERGE_TOOL); logPrinter = Log.getPrinter(EModule.SPLIT_MERGE_TOOL);
@ -180,125 +169,3 @@ class SplitTask extends Task<Boolean>{
return true; return true;
} }
} }
class MergeTask extends Task<Boolean> {
private ILogPrinter logPrinter;
private String saveToPath;
private String filePath;
MergeTask(String filePath, String saveToPath) {
this.filePath = filePath;
this.saveToPath = saveToPath;
logPrinter = Log.getPrinter(EModule.SPLIT_MERGE_TOOL);
}
@Override
protected Boolean call() {
logPrinter.print("Merge file: "+filePath, EMsgType.INFO);
File folder = new File(filePath);
long cnkTotalSize = 0;
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.close();
return false;
}
Arrays.sort(chunkFiles);
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();
}
double chunkPercent = (4194240.0 / (cnkTotalSize / 100.0) / 100.0);
long totalSizeCnt = 0;
File resultFile = new File(saveToPath+File.separator+"!_"+folder.getName());
//*******
for (int i = 0; ; i++){
if (this.isCancelled()){
logPrinter.print("Split task interrupted!", EMsgType.PASS);
logPrinter.close();
return false;
}
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());
continue;
}
logPrinter.print("Save results to: "+resultFile.getAbsolutePath(), EMsgType.INFO);
break;
}
//*******
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(resultFile));
BufferedInputStream bis;
byte[] chunk;
int readBytesCnt;
for (File cnk : chunkFiles){
bis = new BufferedInputStream(new FileInputStream(cnk));
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;
}
chunk = new byte[4194240];
readBytesCnt = bis.read(chunk);
logPrinter.updateProgress(chunkPercent * totalSizeCnt);
totalSizeCnt++;
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){
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);
}
logPrinter.print("Merge task complete!", EMsgType.INFO);
logPrinter.close();
return true;
}
}

View file

@ -128,6 +128,7 @@ public class GoldLeaf {
"GoldLeaf"+goldLeafVersion, "GoldLeaf"+goldLeafVersion,
filterForNsp); filterForNsp);
Thread thread = new Thread(task); Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start(); thread.start();
thread.join(); thread.join();
} }

View file

@ -52,6 +52,7 @@ public class NXDT {
private void runBackend() throws InterruptedException{ private void runBackend() throws InterruptedException{
NxdtTask nxdtTask = new NxdtTask(saveTo); NxdtTask nxdtTask = new NxdtTask(saveTo);
Thread thread = new Thread(nxdtTask); Thread thread = new Thread(nxdtTask);
thread.setDaemon(true);
thread.start(); thread.start();
thread.join(); thread.join();
} }

View file

@ -77,7 +77,7 @@ public class TinfoilNet {
+ "\tns-usbloader -n nsip=<arg1> [hostip=<arg2>] FILE1 ...\n" + "\tns-usbloader -n nsip=<arg1> [hostip=<arg2>] FILE1 ...\n"
+ "\tns-usbloader --tfn nsip=<arg1> [hostip=<arg2>] FILE1 ..." + "\tns-usbloader --tfn nsip=<arg1> [hostip=<arg2>] FILE1 ..."
+ "\n\nOptions:" + "\n\nOptions:"
+ "\n\tnsip=<ip>\t\t\tDefine NS IP address (mandatory)" + "\n\tnsip=<ip>\t\tDefine NS IP address (mandatory)"
+ "\n\thostip=<ip[:port]>\tDefine this host IP address. Will be obtained automatically if not set."); + "\n\thostip=<ip[:port]>\tDefine this host IP address. Will be obtained automatically if not set.");
} }
@ -138,6 +138,7 @@ public class TinfoilNet {
hostPortNum, hostPortNum,
""); "");
Thread netCommThread = new Thread(netCommunications); Thread netCommThread = new Thread(netCommunications);
netCommThread.setDaemon(true);
netCommThread.start(); netCommThread.start();
netCommThread.join(); netCommThread.join();
} }

View file

@ -62,6 +62,7 @@ public class TinfoilUsb {
private void runTinfoilBackend() throws InterruptedException{ private void runTinfoilBackend() throws InterruptedException{
Runnable task = new UsbCommunications(filesList, "TinFoil", false); Runnable task = new UsbCommunications(filesList, "TinFoil", false);
Thread thread = new Thread(task); Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start(); thread.start();
thread.join(); thread.join();
} }