35
35
import java .nio .file .Path ;
36
36
import java .nio .file .Paths ;
37
37
import java .util .Arrays ;
38
- import java .util .HashMap ;
39
38
40
39
class NxdtUsbAbi1 {
41
40
private final ILogPrinter logPrinter ;
@@ -86,7 +85,7 @@ class NxdtUsbAbi1 {
86
85
87
86
private static final int NXDT_USB_TIMEOUT = 5000 ;
88
87
89
- private HashMap < String , NxdtNspFile > nspFiles ;
88
+ private NxdtNspFile nspFile ;
90
89
91
90
public NxdtUsbAbi1 (DeviceHandle handler ,
92
91
ILogPrinter logPrinter ,
@@ -96,7 +95,6 @@ public NxdtUsbAbi1(DeviceHandle handler,
96
95
this .handlerNS = handler ;
97
96
this .logPrinter = logPrinter ;
98
97
this .parent = parent ;
99
- this .nspFiles = new HashMap <>();
100
98
this .isWindows = System .getProperty ("os.name" ).toLowerCase ().contains ("windows" );
101
99
102
100
if (isWindows )
@@ -213,7 +211,7 @@ private void handleSendFileProperties(byte[] message) throws Exception{
213
211
String filename = new String (message , 0x20 , fileNameLen , StandardCharsets .UTF_8 );
214
212
String absoluteFilePath = getAbsoluteFilePath (filename );
215
213
File fileToDump = new File (absoluteFilePath );
216
- NxdtNspFile nspFile = nspFiles . get ( filename ); // it could be null, but it's not a problem.
214
+
217
215
boolean nspTransferMode = false ;
218
216
219
217
if (checkSizes (fullSize , headerSize ))
@@ -228,7 +226,7 @@ private void handleSendFileProperties(byte[] message) throws Exception{
228
226
229
227
if (headerSize > 0 ){ // if NSP
230
228
logPrinter .print ("Receiving NSP file entry: '" +filename +"' (" +fullSize +" b)" , EMsgType .INFO );
231
- if (nspFiles . containsKey ( filename )){
229
+ if (filename . equals ( nspFile . getName () )){
232
230
nspTransferMode = true ;
233
231
}
234
232
else {
@@ -265,11 +263,13 @@ private boolean checkFileNameLen(int fileNameLen) throws Exception{
265
263
private boolean checkSizes (long fileSize , int headerSize ) throws Exception {
266
264
if (fileSize >= headerSize ){
267
265
logPrinter .print ("File size should not be equal to header size for NSP files!" , EMsgType .FAIL );
266
+ resetNsp ();
268
267
writeUsb (USBSTATUS_MALFORMED_REQUEST );
269
268
return true ;
270
269
}
271
270
if (fileSize < 0 ){ // It's possible to have files of zero-length, so only less is the problem
272
271
logPrinter .print ("File size should not be less then zero!" , EMsgType .FAIL );
272
+ resetNsp ();
273
273
writeUsb (USBSTATUS_MALFORMED_REQUEST );
274
274
return true ;
275
275
}
@@ -294,8 +294,7 @@ private boolean checkFileSystem(File fileToDump, long fileSize) throws Exception
294
294
}
295
295
private void createNewNsp (String filename , int headerSize , long fileSize , File fileToDump ) throws Exception {
296
296
try {
297
- NxdtNspFile nsp = new NxdtNspFile (filename , headerSize , fileSize , fileToDump );
298
- nspFiles .putIfAbsent (filename , nsp );
297
+ nspFile = new NxdtNspFile (filename , headerSize , fileSize , fileToDump );
299
298
}
300
299
catch (Exception e ){
301
300
logPrinter .print (e .getMessage (), EMsgType .FAIL );
@@ -334,8 +333,6 @@ private boolean createPath(String path) throws Exception{
334
333
}
335
334
}
336
335
337
-
338
-
339
336
// @see https://bugs.openjdk.java.net/browse/JDK-8146538
340
337
private void dumpFile (File file , long size ) throws Exception {
341
338
FileOutputStream fos = new FileOutputStream (file , true );
@@ -404,7 +401,8 @@ private void dumpNspFile(NxdtNspFile nsp, long size) throws Exception{
404
401
405
402
private void handleSendNspHeader (byte [] message ) throws Exception {
406
403
final int headerSize = getLEint (message , 0x8 );
407
- NxdtNspFile nsp = nspFiles .remove ( null ); // <---------------------- //TODO: PLEASE PUT FILENAME HERE
404
+ NxdtNspFile nsp = nspFile ;
405
+ resetNsp ();
408
406
409
407
if (nsp == null ) {
410
408
writeUsb (USBSTATUS_MALFORMED_REQUEST );
@@ -420,7 +418,7 @@ private void handleSendNspHeader(byte[] message) throws Exception{
420
418
421
419
if (headerSize != nsp .getHeaderSize ()) {
422
420
writeUsb (USBSTATUS_MALFORMED_REQUEST );
423
- logPrinter .print ("Received NSP header size mismatch! " +headerSize +" != " +nsp .getHeaderSize (), EMsgType .FAIL );
421
+ logPrinter .print ("Received NSP header size mismatch! " +headerSize +" != " + nsp .getHeaderSize (), EMsgType .FAIL );
424
422
return ;
425
423
}
426
424
@@ -432,6 +430,9 @@ private void handleSendNspHeader(byte[] message) throws Exception{
432
430
433
431
writeUsb (USBSTATUS_SUCCESS );
434
432
}
433
+ private void resetNsp (){
434
+ this .nspFile = null ;
435
+ }
435
436
436
437
/** Sending any byte array to USB device **/
437
438
private void writeUsb (byte [] message ) throws Exception {
0 commit comments