@@ -329,12 +329,11 @@ public long transferTo(long position, long count, WritableByteChannel target)
329
329
330
330
tempPart ++;
331
331
}
332
- ByteArrayOutputStream endWriter =
333
- new ByteArrayOutputStream ();
332
+ ByteArrayOutputStream endWriter = new ByteArrayOutputStream ();
334
333
335
334
Part .sendMessageEnd (endWriter , boundary );
336
335
337
- overallLength += writeToTarget (target , endWriter );
336
+ overallLength += writeToTarget (target , endWriter . toByteArray () );
338
337
339
338
startPart = tempPart ;
340
339
@@ -391,15 +390,15 @@ private long handleByteArrayPart(WritableByteChannel target,
391
390
392
391
final ByteArrayOutputStream output = new ByteArrayOutputStream ();
393
392
Part .sendPart (output , filePart , boundary );
394
- return writeToTarget (target , output );
393
+ return writeToTarget (target , output . toByteArray () );
395
394
}
396
395
397
396
private long handleFileEnd (WritableByteChannel target , FilePart filePart )
398
397
throws IOException {
399
398
400
399
ByteArrayOutputStream endOverhead = generateFileEnd (filePart );
401
400
402
- return this .writeToTarget (target , endOverhead );
401
+ return this .writeToTarget (target , endOverhead . toByteArray () );
403
402
}
404
403
405
404
private ByteArrayOutputStream generateFileEnd (FilePart filePart )
@@ -415,7 +414,7 @@ private long handleFileHeaders(WritableByteChannel target, FilePart filePart) th
415
414
416
415
ByteArrayOutputStream overhead = generateFileStart (filePart );
417
416
418
- return writeToTarget (target , overhead );
417
+ return writeToTarget (target , overhead . toByteArray () );
419
418
}
420
419
421
420
private ByteArrayOutputStream generateFileStart (FilePart filePart )
@@ -525,7 +524,7 @@ private long handlePartSource(WritableByteChannel target, FilePart filePart) thr
525
524
if (nRead > 0 ) {
526
525
ByteArrayOutputStream bos = new ByteArrayOutputStream (nRead );
527
526
bos .write (bytes , 0 , nRead );
528
- writeToTarget (target , bos );
527
+ writeToTarget (target , bos . toByteArray () );
529
528
}
530
529
}
531
530
} finally {
@@ -544,7 +543,7 @@ private long handleStringPart(WritableByteChannel target, StringPart currentPart
544
543
545
544
Part .sendPart (outputStream , currentPart , boundary );
546
545
547
- return writeToTarget (target , outputStream );
546
+ return writeToTarget (target , outputStream . toByteArray () );
548
547
}
549
548
550
549
private long handleMultiPart (WritableByteChannel target , Part currentPart ) throws IOException {
@@ -561,21 +560,21 @@ private long handleMultiPart(WritableByteChannel target, Part currentPart) throw
561
560
return 0 ;
562
561
}
563
562
564
- private long writeToTarget (WritableByteChannel target , ByteArrayOutputStream byteWriter )
563
+ private long writeToTarget (WritableByteChannel target , byte [] bytes )
565
564
throws IOException {
566
565
567
566
int written = 0 ;
568
567
int maxSpin = 0 ;
569
- synchronized (byteWriter ) {
570
- ByteBuffer message = ByteBuffer .wrap (byteWriter . toByteArray () );
568
+ synchronized (bytes ) {
569
+ ByteBuffer message = ByteBuffer .wrap (bytes );
571
570
572
571
if (target instanceof SocketChannel ) {
573
572
final Selector selector = Selector .open ();
574
573
try {
575
574
final SocketChannel channel = (SocketChannel ) target ;
576
575
channel .register (selector , SelectionKey .OP_WRITE );
577
576
578
- while (written < byteWriter . size () ) {
577
+ while (written < bytes . length ) {
579
578
selector .select (1000 );
580
579
maxSpin ++;
581
580
final Set <SelectionKey > selectedKeys = selector .selectedKeys ();
@@ -595,13 +594,13 @@ private long writeToTarget(WritableByteChannel target, ByteArrayOutputStream byt
595
594
selector .close ();
596
595
}
597
596
} else {
598
- while ((target .isOpen ()) && (written < byteWriter . size () )) {
597
+ while ((target .isOpen ()) && (written < bytes . length )) {
599
598
long nWrite = target .write (message );
600
599
written += nWrite ;
601
600
if (nWrite == 0 && maxSpin ++ < 10 ) {
602
601
logger .info ("Waiting for writing..." );
603
602
try {
604
- byteWriter .wait (1000 );
603
+ bytes .wait (1000 );
605
604
} catch (InterruptedException e ) {
606
605
logger .trace (e .getMessage (), e );
607
606
}
@@ -616,5 +615,4 @@ private long writeToTarget(WritableByteChannel target, ByteArrayOutputStream byt
616
615
}
617
616
return written ;
618
617
}
619
-
620
618
}
0 commit comments