@@ -201,100 +201,85 @@ private void performHandshake(byte[] message) throws Exception{
201
201
}
202
202
203
203
private void handleSendFileProperties (byte [] message ) throws Exception {
204
- final long fullSize = getLElong (message , 0x10 );
205
- final int fileNameLen = getLEint (message , 0x18 );
206
- final int headerSize = getLEint (message , 0x1C );
207
-
208
- if (checkFileNameLen (fileNameLen )) // In case of negative value we should better handle it before String constructor throws error
209
- return ;
210
-
211
- String filename = new String (message , 0x20 , fileNameLen , StandardCharsets .UTF_8 );
212
- String absoluteFilePath = getAbsoluteFilePath (filename );
213
- File fileToDump = new File (absoluteFilePath );
214
-
215
- if (checkSizes (fullSize , headerSize ))
216
- return ;
204
+ try {
205
+ final long fullSize = getLElong (message , 0x10 );
206
+ final int fileNameLen = getLEint (message , 0x18 );
207
+ final int headerSize = getLEint (message , 0x1C );
208
+ checkFileNameLen (fileNameLen ); // In case of negative value we should better handle it before String constructor throws error
209
+ String filename = new String (message , 0x20 , fileNameLen , StandardCharsets .UTF_8 );
210
+ String absoluteFilePath = getAbsoluteFilePath (filename );
211
+ File fileToDump = new File (absoluteFilePath );
212
+
213
+ checkSizes (fullSize , headerSize );
214
+ createPath (absoluteFilePath );
215
+ checkFileSystem (fileToDump , fullSize );
216
+
217
+ if (headerSize > 0 ){ // if NSP
218
+ logPrinter .print ("Receiving NSP file: '" +filename +"' (" +formatByteSize (fullSize )+")" , EMsgType .PASS );
219
+ createNewNsp (filename , headerSize , fullSize , fileToDump );
220
+ return ;
221
+ }
222
+ else {
223
+ // TODO: Note, in case of a big amount of small files performance decreases dramatically. It's better to handle this only in case of 1-big-file-transfer
224
+ logPrinter .print ("Receiving: '" +filename +"' (" +fullSize +" b)" , EMsgType .INFO );
225
+ }
217
226
218
- if (createPath (absoluteFilePath ))
219
- return ;
227
+ writeUsb (USBSTATUS_SUCCESS );
220
228
229
+ if (fullSize == 0 )
230
+ return ;
221
231
222
- if (checkFileSystem (fileToDump , fullSize ))
223
- return ;
232
+ if (isNspTransfer ())
233
+ dumpNspFile (fullSize );
234
+ else
235
+ dumpFile (fileToDump , fullSize );
224
236
225
- if (headerSize > 0 ){ // if NSP
226
- logPrinter .print ("Receiving NSP file: '" +filename +"' (" +formatByteSize (fullSize )+")" , EMsgType .PASS );
227
- createNewNsp (filename , headerSize , fullSize , fileToDump );
228
- return ;
237
+ writeUsb (USBSTATUS_SUCCESS );
229
238
}
230
- else {
231
- // TODO: Note, in case of a big amount of small files performance decreases dramatically. It's better to handle this only in case of 1-big-file-transfer
232
- logPrinter .print ("Receiving: '" +filename +"' (" +fullSize +" b)" , EMsgType .INFO );
239
+ catch (NxdtMalformedException malformed ){
240
+ logPrinter .print (malformed .getMessage (), EMsgType .FAIL );
241
+ writeUsb (USBSTATUS_MALFORMED_REQUEST );
242
+ }
243
+ catch (NxdtHostIOException ioException ){
244
+ logPrinter .print (ioException .getMessage (), EMsgType .FAIL );
245
+ writeUsb (USBSTATUS_HOSTIOERROR );
233
246
}
234
-
235
- writeUsb (USBSTATUS_SUCCESS );
236
-
237
- if (fullSize == 0 )
238
- return ;
239
-
240
- if (isNspTransfer ())
241
- dumpNspFile (nspFile , fullSize );
242
- else
243
- dumpFile (fileToDump , fullSize );
244
-
245
- writeUsb (USBSTATUS_SUCCESS );
246
-
247
247
}
248
- private boolean checkFileNameLen (int fileNameLen ) throws Exception {
248
+ private void checkFileNameLen (int fileNameLen ) throws NxdtMalformedException {
249
249
if (fileNameLen <= 0 || fileNameLen > NXDT_FILE_PROPERTIES_MAX_NAME_LENGTH ){
250
- logPrinter .print ("Invalid filename length!" , EMsgType .FAIL );
251
- writeUsb (USBSTATUS_MALFORMED_REQUEST );
252
- return true ;
250
+ throw new NxdtMalformedException ("Invalid filename length!" );
253
251
}
254
- return false ;
255
252
}
256
- private boolean checkSizes (long fileSize , int headerSize ) throws Exception {
253
+ private void checkSizes (long fileSize , int headerSize ) throws Exception {
257
254
if (headerSize >= fileSize ){
258
- logPrinter .print (String .format ("File size (%d) should not be less or equal to header size (%d)!" , fileSize , headerSize ), EMsgType .FAIL );
259
255
resetNsp ();
260
- writeUsb (USBSTATUS_MALFORMED_REQUEST );
261
- return true ;
256
+ throw new NxdtMalformedException (String .format ("File size (%d) should not be less or equal to header size (%d)!" , fileSize , headerSize ));
262
257
}
263
258
if (fileSize < 0 ){ // It's possible to have files of zero-length, so only less is the problem
264
- logPrinter .print ("File size should not be less then zero!" , EMsgType .FAIL );
265
259
resetNsp ();
266
- writeUsb (USBSTATUS_MALFORMED_REQUEST );
267
- return true ;
260
+ throw new NxdtMalformedException ("File size should not be less then zero!" );
268
261
}
269
- return false ;
270
262
}
271
- private boolean checkFileSystem (File fileToDump , long fileSize ) throws Exception {
263
+ private void checkFileSystem (File fileToDump , long fileSize ) throws Exception {
272
264
// Check if enough space
273
265
if (fileToDump .getParentFile ().getFreeSpace () <= fileSize ){
274
- writeUsb (USBSTATUS_HOSTIOERROR );
275
- logPrinter .print ("Not enough space on selected volume. Need: " +fileSize +
276
- " while available: " +fileToDump .getParentFile ().getFreeSpace (), EMsgType .FAIL );
277
- return true ;
266
+ throw new NxdtHostIOException ("Not enough space on selected volume. Need: " +fileSize +
267
+ " while available: " +fileToDump .getParentFile ().getFreeSpace ());
278
268
}
279
269
// Check if FS is NOT read-only
280
270
if (! (fileToDump .canWrite () || fileToDump .createNewFile ()) ){
281
- writeUsb (USBSTATUS_HOSTIOERROR );
282
- logPrinter .print ("Unable to write into selected volume: " +fileToDump .getAbsolutePath (), EMsgType .FAIL );
283
- return true ;
271
+ throw new NxdtHostIOException ("Unable to write into selected volume: " +fileToDump .getAbsolutePath ());
284
272
}
285
- return false ;
286
273
}
287
- private void createNewNsp (String filename , int headerSize , long fileSize , File fileToDump ) throws Exception {
274
+ private void createNewNsp (String filename , int headerSize , long fileSize , File fileToDump ) throws NxdtHostIOException {
288
275
try {
289
276
nspFile = new NxdtNspFile (filename , headerSize , fileSize , fileToDump );
277
+ writeUsb (USBSTATUS_SUCCESS );
290
278
}
291
279
catch (Exception e ){
292
- logPrinter .print ("Unable to create new file for: " +filename +" :" +e .getMessage (), EMsgType .FAIL );
293
280
e .printStackTrace ();
294
- writeUsb (USBSTATUS_HOSTIOERROR );
295
- return ;
281
+ throw new NxdtHostIOException ("Unable to create new file for: " +filename +" :" +e .getMessage ());
296
282
}
297
- writeUsb (USBSTATUS_SUCCESS );
298
283
}
299
284
private int getLEint (byte [] bytes , int fromOffset ){
300
285
return ByteBuffer .wrap (bytes , fromOffset , 0x4 ).order (ByteOrder .LITTLE_ENDIAN ).getInt ();
@@ -316,24 +301,21 @@ private boolean isRomFs(String filename){
316
301
return filename .startsWith ("/" );
317
302
}
318
303
319
- private boolean createPath (String path ) throws Exception {
304
+ private void createPath (String path ) throws Exception {
320
305
try {
321
306
Path folderForTheFile = Paths .get (path ).getParent ();
322
307
Files .createDirectories (folderForTheFile );
323
- return false ;
324
308
}
325
309
catch (Exception e ){
326
- logPrinter .print ("Unable to create dir(s) for file " +path , EMsgType .FAIL );
327
- writeUsb (USBSTATUS_HOSTIOERROR );
328
- return true ;
310
+ throw new NxdtHostIOException ("Unable to create dir(s) for file '" +path +"':" +e .getMessage ());
329
311
}
330
312
}
331
313
332
314
// @see https://bugs.openjdk.java.net/browse/JDK-8146538
333
315
private void dumpFile (File file , long size ) throws Exception {
334
316
FileOutputStream fos = new FileOutputStream (file , true );
335
317
336
- try (BufferedOutputStream bos = new BufferedOutputStream (fos )) {
318
+ try (BufferedOutputStream bos = new BufferedOutputStream (fos )){
337
319
FileDescriptor fd = fos .getFD ();
338
320
byte [] readBuffer ;
339
321
long received = 0 ;
@@ -360,12 +342,12 @@ private void dumpFile(File file, long size) throws Exception{
360
342
}
361
343
}
362
344
363
- private void dumpNspFile (NxdtNspFile nsp , long size ) throws Exception {
364
- FileOutputStream fos = new FileOutputStream (nsp .getFile (), true );
365
- long nspSize = nsp .getFullSize ();
345
+ private void dumpNspFile (long size ) throws Exception {
346
+ FileOutputStream fos = new FileOutputStream (nspFile .getFile (), true );
347
+ long nspSize = nspFile .getFullSize ();
366
348
367
349
try (BufferedOutputStream bos = new BufferedOutputStream (fos )) {
368
- long nspRemainingSize = nsp .getNspRemainingSize ();
350
+ long nspRemainingSize = nspFile .getNspRemainingSize ();
369
351
370
352
FileDescriptor fd = fos .getFD ();
371
353
byte [] readBuffer ;
@@ -390,14 +372,15 @@ private void dumpNspFile(NxdtNspFile nsp, long size) throws Exception{
390
372
if (isWindows10 )
391
373
fd .sync ();
392
374
nspRemainingSize -= (lastChunkSize - 1 );
393
- nsp .setNspRemainingSize (nspRemainingSize );
375
+ nspFile .setNspRemainingSize (nspRemainingSize );
394
376
}
395
377
}
396
378
397
379
private void handleSendNspHeader (byte [] message ) throws Exception {
398
380
final int headerSize = getLEint (message , 0x8 );
399
381
NxdtNspFile nsp = nspFile ;
400
382
resetNsp ();
383
+ logPrinter .updateProgress (1.0 );
401
384
402
385
if (nsp == null ) {
403
386
writeUsb (USBSTATUS_MALFORMED_REQUEST );
@@ -423,7 +406,6 @@ private void handleSendNspHeader(byte[] message) throws Exception{
423
406
raf .write (headerData );
424
407
}
425
408
logPrinter .print ("NSP file: '" +nsp .getName ()+"' successfully received!" , EMsgType .PASS );
426
- logPrinter .updateProgress (1.0 );
427
409
writeUsb (USBSTATUS_SUCCESS );
428
410
}
429
411
private void resetNsp (){
0 commit comments