@@ -329,12 +329,11 @@ public long transferTo(long position, long count, WritableByteChannel target)
329329
330330tempPart ++;
331331 }
332- ByteArrayOutputStream endWriter =
333- new ByteArrayOutputStream ();
332+ ByteArrayOutputStream endWriter =new ByteArrayOutputStream ();
334333
335334Part .sendMessageEnd (endWriter ,boundary );
336335
337- overallLength +=writeToTarget (target ,endWriter );
336+ overallLength +=writeToTarget (target ,endWriter . toByteArray () );
338337
339338startPart =tempPart ;
340339
@@ -391,15 +390,15 @@ private long handleByteArrayPart(WritableByteChannel target,
391390
392391final ByteArrayOutputStream output =new ByteArrayOutputStream ();
393392Part .sendPart (output ,filePart ,boundary );
394- return writeToTarget (target ,output );
393+ return writeToTarget (target ,output . toByteArray () );
395394 }
396395
397396private long handleFileEnd (WritableByteChannel target ,FilePart filePart )
398397throws IOException {
399398
400399ByteArrayOutputStream endOverhead =generateFileEnd (filePart );
401400
402- return this .writeToTarget (target ,endOverhead );
401+ return this .writeToTarget (target ,endOverhead . toByteArray () );
403402 }
404403
405404private ByteArrayOutputStream generateFileEnd (FilePart filePart )
@@ -415,7 +414,7 @@ private long handleFileHeaders(WritableByteChannel target, FilePart filePart) th
415414
416415ByteArrayOutputStream overhead =generateFileStart (filePart );
417416
418- return writeToTarget (target ,overhead );
417+ return writeToTarget (target ,overhead . toByteArray () );
419418 }
420419
421420private ByteArrayOutputStream generateFileStart (FilePart filePart )
@@ -525,7 +524,7 @@ private long handlePartSource(WritableByteChannel target, FilePart filePart) thr
525524if (nRead >0 ) {
526525ByteArrayOutputStream bos =new ByteArrayOutputStream (nRead );
527526bos .write (bytes ,0 ,nRead );
528- writeToTarget (target ,bos );
527+ writeToTarget (target ,bos . toByteArray () );
529528 }
530529 }
531530 }finally {
@@ -544,7 +543,7 @@ private long handleStringPart(WritableByteChannel target, StringPart currentPart
544543
545544Part .sendPart (outputStream ,currentPart ,boundary );
546545
547- return writeToTarget (target ,outputStream );
546+ return writeToTarget (target ,outputStream . toByteArray () );
548547 }
549548
550549private long handleMultiPart (WritableByteChannel target ,Part currentPart )throws IOException {
@@ -561,21 +560,21 @@ private long handleMultiPart(WritableByteChannel target, Part currentPart) throw
561560return 0 ;
562561 }
563562
564- private long writeToTarget (WritableByteChannel target ,ByteArrayOutputStream byteWriter )
563+ private long writeToTarget (WritableByteChannel target ,byte [] bytes )
565564throws IOException {
566565
567566int written =0 ;
568567int maxSpin =0 ;
569- synchronized (byteWriter ) {
570- ByteBuffer message =ByteBuffer .wrap (byteWriter . toByteArray () );
568+ synchronized (bytes ) {
569+ ByteBuffer message =ByteBuffer .wrap (bytes );
571570
572571if (target instanceof SocketChannel ) {
573572final Selector selector =Selector .open ();
574573try {
575574final SocketChannel channel = (SocketChannel )target ;
576575channel .register (selector ,SelectionKey .OP_WRITE );
577576
578- while (written <byteWriter . size () ) {
577+ while (written <bytes . length ) {
579578selector .select (1000 );
580579maxSpin ++;
581580final Set <SelectionKey >selectedKeys =selector .selectedKeys ();
@@ -595,13 +594,13 @@ private long writeToTarget(WritableByteChannel target, ByteArrayOutputStream byt
595594selector .close ();
596595 }
597596 }else {
598- while ((target .isOpen ()) && (written <byteWriter . size () )) {
597+ while ((target .isOpen ()) && (written <bytes . length )) {
599598long nWrite =target .write (message );
600599written +=nWrite ;
601600if (nWrite ==0 &&maxSpin ++ <10 ) {
602601logger .info ("Waiting for writing..." );
603602try {
604- byteWriter .wait (1000 );
603+ bytes .wait (1000 );
605604 }catch (InterruptedException e ) {
606605logger .trace (e .getMessage (),e );
607606 }
@@ -616,5 +615,4 @@ private long writeToTarget(WritableByteChannel target, ByteArrayOutputStream byt
616615 }
617616return written ;
618617 }
619-
620618}