NXDT: Add ZLT packet validation check
NXDT: Update BufferedStreams: add try-with-resources practice instead of handling steam.close() calls. (See https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html). Thanks DarkMatterCore :)
This commit is contained in:
parent
dff7063e09
commit
ab1b5b1820
1 changed files with 59 additions and 45 deletions
|
@ -37,7 +37,7 @@ class NxdtUsbAbi1 {
|
||||||
private final String saveToPath;
|
private final String saveToPath;
|
||||||
private final NxdtTask parent;
|
private final NxdtTask parent;
|
||||||
|
|
||||||
private boolean isWindows;
|
private final boolean isWindows;
|
||||||
private boolean isWindows10;
|
private boolean isWindows10;
|
||||||
|
|
||||||
private static final int NXDT_MAX_DIRECTIVE_SIZE = 0x1000;
|
private static final int NXDT_MAX_DIRECTIVE_SIZE = 0x1000;
|
||||||
|
@ -77,6 +77,8 @@ class NxdtUsbAbi1 {
|
||||||
0x00, 0x00, 0x00, 0x00,
|
0x00, 0x00, 0x00, 0x00,
|
||||||
0x00, 0x00, 0x00, 0x00 };
|
0x00, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
|
private static final long W_MAX_PACKET_SIZE = 0x200;
|
||||||
|
|
||||||
public NxdtUsbAbi1(DeviceHandle handler,
|
public NxdtUsbAbi1(DeviceHandle handler,
|
||||||
ILogPrinter logPrinter,
|
ILogPrinter logPrinter,
|
||||||
String saveToPath,
|
String saveToPath,
|
||||||
|
@ -136,7 +138,7 @@ class NxdtUsbAbi1 {
|
||||||
logPrinter.print(e.getMessage(), EMsgType.INFO);
|
logPrinter.print(e.getMessage(), EMsgType.INFO);
|
||||||
logPrinter.print("Terminating now", EMsgType.FAIL);
|
logPrinter.print("Terminating now", EMsgType.FAIL);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
private boolean isInvalidDirective(byte[] message) throws Exception{
|
private boolean isInvalidDirective(byte[] message) throws Exception{
|
||||||
if (message.length < 0x10){
|
if (message.length < 0x10){
|
||||||
|
@ -256,52 +258,66 @@ class NxdtUsbAbi1 {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void dumpFile(File file, long size) throws Exception{
|
private void dumpFile(File file, long size) throws Exception{
|
||||||
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, false));
|
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, false))) {
|
||||||
|
byte[] readBuffer;
|
||||||
|
long received = 0;
|
||||||
|
int bufferSize;
|
||||||
|
|
||||||
byte[] readBuffer;
|
boolean zlt_expected = isAligned(size);
|
||||||
long received = 0;
|
|
||||||
int bufferSize;
|
|
||||||
|
|
||||||
while (received < size){
|
while (received < size) {
|
||||||
readBuffer = readUsbFile();
|
readBuffer = readUsbFile();
|
||||||
bos.write(readBuffer);
|
bos.write(readBuffer);
|
||||||
bufferSize = readBuffer.length;
|
bufferSize = readBuffer.length;
|
||||||
received += bufferSize;
|
received += bufferSize;
|
||||||
logPrinter.updateProgress((received + bufferSize) / (size / 100.0) / 100.0);
|
logPrinter.updateProgress((received + bufferSize) / (size / 100.0) / 100.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (zlt_expected) {
|
||||||
|
logPrinter.print("Finishing with ZLT packet request", EMsgType.INFO);
|
||||||
|
readUsbFile();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
logPrinter.updateProgress(1.0);
|
||||||
}
|
}
|
||||||
logPrinter.updateProgress(1.0);
|
|
||||||
bos.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @see https://bugs.openjdk.java.net/browse/JDK-8146538
|
// @see https://bugs.openjdk.java.net/browse/JDK-8146538
|
||||||
private void dumpFileOnWindowsTen(File file, long size) throws Exception{
|
private void dumpFileOnWindowsTen(File file, long size) throws Exception{
|
||||||
FileOutputStream fos = new FileOutputStream(file, true);
|
FileOutputStream fos = new FileOutputStream(file, true);
|
||||||
BufferedOutputStream bos = new BufferedOutputStream(fos);
|
|
||||||
FileDescriptor fd = fos.getFD();
|
|
||||||
|
|
||||||
byte[] readBuffer;
|
try (BufferedOutputStream bos = new BufferedOutputStream(fos)) {
|
||||||
long received = 0;
|
FileDescriptor fd = fos.getFD();
|
||||||
int bufferSize;
|
byte[] readBuffer;
|
||||||
|
long received = 0;
|
||||||
|
int bufferSize;
|
||||||
|
|
||||||
while (received < size){
|
boolean zlt_expected = isAligned(size);
|
||||||
readBuffer = readUsbFile();
|
|
||||||
bos.write(readBuffer);
|
|
||||||
fd.sync(); // Fixes flushing under Windows (unharmful for other OS)
|
|
||||||
bufferSize = readBuffer.length;
|
|
||||||
received += bufferSize;
|
|
||||||
|
|
||||||
logPrinter.updateProgress((received + bufferSize) / (size / 100.0) / 100.0);
|
while (received < size) {
|
||||||
|
readBuffer = readUsbFile();
|
||||||
|
bos.write(readBuffer);
|
||||||
|
fd.sync(); // Fixes flushing under Windows (unharmful for other OS)
|
||||||
|
bufferSize = readBuffer.length;
|
||||||
|
received += bufferSize;
|
||||||
|
|
||||||
|
logPrinter.updateProgress((received + bufferSize) / (size / 100.0) / 100.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (zlt_expected) {
|
||||||
|
logPrinter.print("Finishing with ZLT packet request", EMsgType.INFO);
|
||||||
|
readUsbFile();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
logPrinter.updateProgress(1.0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
logPrinter.updateProgress(1.0);
|
/** Handle Zero-length terminator **/
|
||||||
bos.close();
|
private boolean isAligned(long size){
|
||||||
|
return ((size & (W_MAX_PACKET_SIZE - 1)) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Sending any byte array to USB device **/
|
||||||
* Sending any byte array to USB device
|
|
||||||
* @return 'false' if no issues
|
|
||||||
* 'true' if errors happened
|
|
||||||
* */
|
|
||||||
private void writeUsb(byte[] message) throws Exception{
|
private void writeUsb(byte[] message) throws Exception{
|
||||||
ByteBuffer writeBuffer = ByteBuffer.allocateDirect(message.length);
|
ByteBuffer writeBuffer = ByteBuffer.allocateDirect(message.length);
|
||||||
writeBuffer.put(message);
|
writeBuffer.put(message);
|
||||||
|
@ -312,18 +328,16 @@ class NxdtUsbAbi1 {
|
||||||
|
|
||||||
int result = LibUsb.bulkTransfer(handlerNS, (byte) 0x01, writeBuffer, writeBufTransferred, 5050);
|
int result = LibUsb.bulkTransfer(handlerNS, (byte) 0x01, writeBuffer, writeBufTransferred, 5050);
|
||||||
|
|
||||||
switch (result){
|
if (result == LibUsb.SUCCESS) {
|
||||||
case LibUsb.SUCCESS:
|
if (writeBufTransferred.get() == message.length)
|
||||||
if (writeBufTransferred.get() == message.length)
|
return;
|
||||||
return;
|
throw new Exception("Data transfer issue [write]" +
|
||||||
throw new Exception("Data transfer issue [write]" +
|
"\n Requested: " + message.length +
|
||||||
"\n Requested: "+message.length+
|
"\n Transferred: " + writeBufTransferred.get());
|
||||||
"\n Transferred: "+writeBufTransferred.get());
|
|
||||||
default:
|
|
||||||
throw new Exception("Data transfer issue [write]" +
|
|
||||||
"\n Returned: "+ UsbErrorCodes.getErrCode(result) +
|
|
||||||
"\n (execution stopped)");
|
|
||||||
}
|
}
|
||||||
|
throw new Exception("Data transfer issue [write]" +
|
||||||
|
"\n Returned: " + UsbErrorCodes.getErrCode(result) +
|
||||||
|
"\n (execution stopped)");
|
||||||
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue