@@ -435,11 +435,11 @@ private ByteArrayOutputStream generateFileStart(FilePart filePart)
435
435
}
436
436
437
437
private long handleFilePart (WritableByteChannel target , FilePart filePart ) throws IOException {
438
- FilePartStallHandler handler = new FilePartStallHandler (
439
- filePart .getStalledTime (), filePart );
440
-
441
- handler .start ();
442
-
438
+ FilePartStallHandler handler = new FilePartStallHandler (
439
+ filePart .getStalledTime (), filePart );
440
+
441
+ handler .start ();
442
+
443
443
if (FilePartSource .class .isAssignableFrom (filePart .getSource ().getClass ())) {
444
444
int length = 0 ;
445
445
@@ -458,23 +458,22 @@ private long handleFilePart(WritableByteChannel target, FilePart filePart) throw
458
458
long nWrite = 0 ;
459
459
synchronized (fc ) {
460
460
while (fileLength != l ) {
461
- if (handler .isFailed ()) {
462
- logger .debug ("Stalled error" );
461
+ if (handler .isFailed ()) {
462
+ logger .debug ("Stalled error" );
463
463
throw new FileUploadStalledException ();
464
- }
464
+ }
465
465
try {
466
466
nWrite = fc .transferTo (fileLength , l , target );
467
-
467
+
468
468
if (nWrite == 0 ) {
469
469
logger .info ("Waiting for writing..." );
470
470
try {
471
471
fc .wait (50 );
472
472
} catch (InterruptedException e ) {
473
473
logger .trace (e .getMessage (), e );
474
474
}
475
- }
476
- else {
477
- handler .writeHappened ();
475
+ } else {
476
+ handler .writeHappened ();
478
477
}
479
478
} catch (IOException ex ) {
480
479
String message = ex .getMessage ();
@@ -496,7 +495,7 @@ private long handleFilePart(WritableByteChannel target, FilePart filePart) throw
496
495
}
497
496
}
498
497
handler .completed ();
499
-
498
+
500
499
fc .close ();
501
500
502
501
length += handleFileEnd (target , filePart );
@@ -556,7 +555,7 @@ private long handleMultiPart(WritableByteChannel target, Part currentPart) throw
556
555
return handleStringPart (target , (StringPart ) currentPart );
557
556
} else if (currentPart .getClass ().equals (FilePart .class )) {
558
557
FilePart filePart = (FilePart ) currentPart ;
559
-
558
+
560
559
return handleFilePart (target , filePart );
561
560
}
562
561
return 0 ;
@@ -571,45 +570,45 @@ private long writeToTarget(WritableByteChannel target, ByteArrayOutputStream byt
571
570
ByteBuffer message = ByteBuffer .wrap (byteWriter .toByteArray ());
572
571
573
572
if (target instanceof SocketChannel ) {
574
- final Selector selector = Selector .open ();
575
- try {
576
- final SocketChannel channel = (SocketChannel ) target ;
577
- channel .register (selector , SelectionKey .OP_WRITE );
578
-
579
- while (written < byteWriter .size () && selector .select () != 0 ) {
580
- final Set <SelectionKey > selectedKeys = selector .selectedKeys ();
581
-
582
- for (SelectionKey key : selectedKeys ) {
583
- if (key .isWritable ()) {
584
- written += target .write (message );
585
- }
586
- }
587
- }
588
-
589
- if (written < byteWriter .size ()) {
590
- throw new IOException ("Unable to write on channel " + target );
591
- }
592
- } finally {
593
- selector .close ();
594
- }
573
+ final Selector selector = Selector .open ();
574
+ try {
575
+ final SocketChannel channel = (SocketChannel ) target ;
576
+ channel .register (selector , SelectionKey .OP_WRITE );
577
+
578
+ while (written < byteWriter .size () && selector .select () != 0 ) {
579
+ final Set <SelectionKey > selectedKeys = selector .selectedKeys ();
580
+
581
+ for (SelectionKey key : selectedKeys ) {
582
+ if (key .isWritable ()) {
583
+ written += target .write (message );
584
+ }
585
+ }
586
+ }
587
+
588
+ if (written < byteWriter .size ()) {
589
+ throw new IOException ("Unable to write on channel " + target );
590
+ }
591
+ } finally {
592
+ selector .close ();
593
+ }
595
594
} else {
596
- while ((target .isOpen ()) && (written < byteWriter .size ())) {
597
- long nWrite = target .write (message );
598
- written += nWrite ;
599
- if (nWrite == 0 && maxSpin ++ < 10 ) {
600
- logger .info ("Waiting for writing..." );
601
- try {
602
- byteWriter .wait (1000 );
603
- } catch (InterruptedException e ) {
604
- logger .trace (e .getMessage (), e );
605
- }
606
- } else {
607
- if (maxSpin >= 10 ) {
608
- throw new IOException ("Unable to write on channel " + target );
609
- }
610
- maxSpin = 0 ;
611
- }
612
- }
595
+ while ((target .isOpen ()) && (written < byteWriter .size ())) {
596
+ long nWrite = target .write (message );
597
+ written += nWrite ;
598
+ if (nWrite == 0 && maxSpin ++ < 10 ) {
599
+ logger .info ("Waiting for writing..." );
600
+ try {
601
+ byteWriter .wait (1000 );
602
+ } catch (InterruptedException e ) {
603
+ logger .trace (e .getMessage (), e );
604
+ }
605
+ } else {
606
+ if (maxSpin >= 10 ) {
607
+ throw new IOException ("Unable to write on channel " + target );
608
+ }
609
+ maxSpin = 0 ;
610
+ }
611
+ }
613
612
}
614
613
}
615
614
return written ;
0 commit comments