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:
Dmitry Isaenko 2020-08-14 18:33:23 +03:00
parent dff7063e09
commit ab1b5b1820

View file

@ -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,34 +258,43 @@ 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; byte[] readBuffer;
long received = 0; long received = 0;
int bufferSize; int bufferSize;
while (received < size){ boolean zlt_expected = isAligned(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();
try (BufferedOutputStream bos = new BufferedOutputStream(fos)) {
FileDescriptor fd = fos.getFD();
byte[] readBuffer; byte[] readBuffer;
long received = 0; long received = 0;
int bufferSize; int bufferSize;
while (received < size){ boolean zlt_expected = isAligned(size);
while (received < size) {
readBuffer = readUsbFile(); readBuffer = readUsbFile();
bos.write(readBuffer); bos.write(readBuffer);
fd.sync(); // Fixes flushing under Windows (unharmful for other OS) fd.sync(); // Fixes flushing under Windows (unharmful for other OS)
@ -293,15 +304,20 @@ class NxdtUsbAbi1 {
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(); }
}
/** Handle Zero-length terminator **/
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)");
} }
/** /**