More refactoring
This commit is contained in:
parent
dfcc2c9927
commit
0307eaaa90
3 changed files with 62 additions and 49 deletions
|
@ -47,6 +47,18 @@ public class RainbowHexDump {
|
||||||
+ "\n");
|
+ "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void hexDumpUTF8ForWin(byte[] byteArray){
|
||||||
|
for (int i=0; i < byteArray.length; i++)
|
||||||
|
System.out.print(String.format("%02d-", i%100));
|
||||||
|
System.out.println(">"+byteArray.length);
|
||||||
|
for (byte b: byteArray)
|
||||||
|
System.out.print(String.format("%02x ", b));
|
||||||
|
System.out.println();
|
||||||
|
System.out.print("\t\t\t"
|
||||||
|
+ new String(byteArray, StandardCharsets.UTF_8)
|
||||||
|
+ "\n");
|
||||||
|
}
|
||||||
|
|
||||||
public static void hexDumpUTF16LE(byte[] byteArray){
|
public static void hexDumpUTF16LE(byte[] byteArray){
|
||||||
System.out.print(ANSI_BLUE);
|
System.out.print(ANSI_BLUE);
|
||||||
for (int i=0; i < byteArray.length; i++)
|
for (int i=0; i < byteArray.length; i++)
|
||||||
|
|
|
@ -104,7 +104,7 @@ class NxdtUsbAbi1 {
|
||||||
while (true){
|
while (true){
|
||||||
directive = readUsbDirective();
|
directive = readUsbDirective();
|
||||||
|
|
||||||
RainbowHexDump.hexDumpUTF8(directive);
|
RainbowHexDump.hexDumpUTF8ForWin(directive);
|
||||||
|
|
||||||
if (isInvalidDirective(directive))
|
if (isInvalidDirective(directive))
|
||||||
continue;
|
continue;
|
||||||
|
@ -123,6 +123,7 @@ class NxdtUsbAbi1 {
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
writeUsb(USBSTATUS_UNSUPPORTED_CMD);
|
writeUsb(USBSTATUS_UNSUPPORTED_CMD);
|
||||||
|
logPrinter.print("Unsupported command: " + command, EMsgType.FAIL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -142,7 +143,7 @@ class NxdtUsbAbi1 {
|
||||||
logPrinter.print("Invalid 'MAGIC'", EMsgType.INFO);
|
logPrinter.print("Invalid 'MAGIC'", EMsgType.INFO);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (message.length != NXDT_COMMAND_SIZE){
|
if (message.length < 0x10){
|
||||||
writeUsb(USBSTATUS_MALFORMED_REQUEST);
|
writeUsb(USBSTATUS_MALFORMED_REQUEST);
|
||||||
logPrinter.print("Invalid directive size. Expected size is 4096 while received is "+message.length, EMsgType.INFO);
|
logPrinter.print("Invalid directive size. Expected size is 4096 while received is "+message.length, EMsgType.INFO);
|
||||||
return true;
|
return true;
|
||||||
|
@ -160,10 +161,9 @@ class NxdtUsbAbi1 {
|
||||||
|
|
||||||
if (ABI_VERSION != versionABI){
|
if (ABI_VERSION != versionABI){
|
||||||
writeUsb(USBSTATUS_UNSUPPORTED_ABI);
|
writeUsb(USBSTATUS_UNSUPPORTED_ABI);
|
||||||
throw new Exception("ABI v"+message[11]+" is not supported in current version.");
|
throw new Exception("ABI v"+versionABI+" is not supported in current version.");
|
||||||
}
|
}
|
||||||
writeUsb(USBSTATUS_SUCCESS);
|
writeUsb(USBSTATUS_SUCCESS);
|
||||||
// consider refactoring: create sub-classes for various ABI versions and check if it's supported or not
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleSendFileProperties(byte[] message) throws Exception{
|
private void handleSendFileProperties(byte[] message) throws Exception{
|
||||||
|
@ -171,35 +171,25 @@ class NxdtUsbAbi1 {
|
||||||
final int fileNameLen = getLEint(message, 0x18);
|
final int fileNameLen = getLEint(message, 0x18);
|
||||||
String filename = new String(message, 0x20, fileNameLen, StandardCharsets.UTF_8);
|
String filename = new String(message, 0x20, fileNameLen, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
// TODO: In here should be also a field with NSP Header size that would be transmitted after the end of main data transfer; NOTE: Handle with RandomAccessFile.
|
|
||||||
|
|
||||||
logPrinter.print("Write request for: '"+filename+"' ("+fileSize+" bytes)", EMsgType.INFO);
|
logPrinter.print("Write request for: '"+filename+"' ("+fileSize+" bytes)", EMsgType.INFO);
|
||||||
// If RomFs related
|
// If RomFs related
|
||||||
if (filename.startsWith("/")) {
|
if (isRomFs(filename)) {
|
||||||
if (isWindows)
|
if (isWindows)
|
||||||
filename = saveToPath + filename.replaceAll("/", "\\\\");
|
filename = saveToPath + filename.replaceAll("/", "\\\\");
|
||||||
else
|
else
|
||||||
filename = saveToPath + filename;
|
filename = saveToPath + filename;
|
||||||
|
|
||||||
try {
|
|
||||||
createPath(filename);
|
createPath(filename);
|
||||||
}
|
}
|
||||||
catch (Exception e){
|
|
||||||
writeUsb(USBSTATUS_HOSTIOERROR);
|
|
||||||
logPrinter.print("Unable to create dir(s) for file in "+filename+
|
|
||||||
"\n Returned: "+e.getMessage(), EMsgType.FAIL);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
filename = saveToPath + filename;
|
filename = saveToPath + filename;
|
||||||
|
|
||||||
File fileToDump = new File(filename);
|
File fileToDump = new File(filename);
|
||||||
|
|
||||||
// Check if enough space
|
// Check if enough space
|
||||||
if (fileToDump.getFreeSpace() <= fileSize){
|
if (fileToDump.getParentFile().getFreeSpace() <= fileSize){
|
||||||
writeUsb(USBSTATUS_HOSTIOERROR);
|
writeUsb(USBSTATUS_HOSTIOERROR);
|
||||||
logPrinter.print("Not enough space on selected volume. Need: "+fileSize+" while available: "+fileToDump.getFreeSpace(), EMsgType.FAIL);
|
logPrinter.print("Not enough space on selected volume. Need: "+fileSize+" while available: "+fileToDump.getParentFile().getFreeSpace(), EMsgType.FAIL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Check if FS is NOT read-only
|
// Check if FS is NOT read-only
|
||||||
|
@ -209,42 +199,14 @@ class NxdtUsbAbi1 {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
writeUsb(USBSTATUS_SUCCESS);
|
||||||
|
|
||||||
dumpFile(fileToDump, fileSize);
|
dumpFile(fileToDump, fileSize);
|
||||||
|
|
||||||
writeUsb(USBSTATUS_SUCCESS);
|
writeUsb(USBSTATUS_SUCCESS);
|
||||||
|
|
||||||
// TODO: check if NSP_SIZE != 0 then go dump header
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createPath(String path) throws Exception{
|
|
||||||
// QA NOTE: Clarify if exception is even possible on RO FS.
|
|
||||||
|
|
||||||
File resultingFile = new File(path);
|
|
||||||
File folderForTheFile = resultingFile.getParentFile();
|
|
||||||
folderForTheFile.mkdirs();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void dumpFile(File file, long size) throws Exception{
|
|
||||||
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, false));
|
|
||||||
|
|
||||||
byte[] readBuffer;
|
|
||||||
long received = 0;
|
|
||||||
int bufferSize;
|
|
||||||
|
|
||||||
while (received < size){
|
|
||||||
readBuffer = readUsbFile();
|
|
||||||
bos.write(readBuffer);
|
|
||||||
bufferSize = readBuffer.length;
|
|
||||||
received += bufferSize;
|
|
||||||
|
|
||||||
logPrinter.updateProgress((received + bufferSize) / (size / 100.0) / 100.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
logPrinter.updateProgress(1.0);
|
|
||||||
bos.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private int getLEint(byte[] bytes, int fromOffset){
|
private int getLEint(byte[] bytes, int fromOffset){
|
||||||
return ByteBuffer.wrap(bytes, fromOffset, 0x4).order(ByteOrder.LITTLE_ENDIAN).getInt();
|
return ByteBuffer.wrap(bytes, fromOffset, 0x4).order(ByteOrder.LITTLE_ENDIAN).getInt();
|
||||||
}
|
}
|
||||||
|
@ -253,6 +215,45 @@ class NxdtUsbAbi1 {
|
||||||
return ByteBuffer.wrap(bytes, fromOffset, 0x8).order(ByteOrder.LITTLE_ENDIAN).getLong();
|
return ByteBuffer.wrap(bytes, fromOffset, 0x8).order(ByteOrder.LITTLE_ENDIAN).getLong();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isRomFs(String filename){
|
||||||
|
return filename.startsWith("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createPath(String path) throws Exception{
|
||||||
|
File resultingFile = new File(path);
|
||||||
|
File folderForTheFile = resultingFile.getParentFile();
|
||||||
|
|
||||||
|
if (folderForTheFile.exists())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (folderForTheFile.mkdirs())
|
||||||
|
return;
|
||||||
|
|
||||||
|
writeUsb(USBSTATUS_HOSTIOERROR);
|
||||||
|
throw new Exception("Unable to create dir(s) for file in "+folderForTheFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dumpFile(File file, long size) throws Exception{
|
||||||
|
RandomAccessFile raf = new RandomAccessFile(file, "rw");
|
||||||
|
|
||||||
|
byte[] readBuffer;
|
||||||
|
long received = 0;
|
||||||
|
int bufferSize;
|
||||||
|
|
||||||
|
while (received < size){
|
||||||
|
readBuffer = readUsbFile();
|
||||||
|
raf.write(readBuffer);
|
||||||
|
bufferSize = readBuffer.length;
|
||||||
|
received += bufferSize;
|
||||||
|
|
||||||
|
logPrinter.updateProgress((received + bufferSize) / (size / 100.0) / 100.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
logPrinter.updateProgress(1.0);
|
||||||
|
raf.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sending any byte array to USB device
|
* Sending any byte array to USB device
|
||||||
* @return 'false' if no issues
|
* @return 'false' if no issues
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 31 KiB |
Loading…
Reference in a new issue