3232
3333public class MultipartBody implements RandomAccessBody {
3434
35- private byte []boundary ;
36- private long contentLength ;
37- private List <com .ning .http .client .Part >parts ;
38- private List <RandomAccessFile >files ;
39- private int startPart ;
4035private final static Logger logger =LoggerFactory .getLogger (MultipartBody .class );
41- ByteArrayInputStream currentStream ;
42- int currentStreamPosition ;
43- boolean endWritten ;
44- boolean doneWritingParts ;
45- FileLocation fileLocation ;
46- FilePart currentFilePart ;
47- FileChannel currentFileChannel ;
36+
37+ private final byte []boundary ;
38+ private final long contentLength ;
39+ private final List <com .ning .http .client .Part >parts ;
40+ private final List <RandomAccessFile >files =new ArrayList <RandomAccessFile >();
41+
42+ private int startPart =0 ;
43+ private ByteArrayInputStream currentStream ;
44+ private int currentStreamPosition = -1 ;
45+ private boolean endWritten =false ;
46+ private boolean doneWritingParts =false ;
47+ private FileLocation fileLocation =FileLocation .NONE ;
48+ private FilePart currentFilePart ;
49+ private FileChannel currentFileChannel ;
4850
4951enum FileLocation {NONE ,START ,MIDDLE ,END }
5052
5153public MultipartBody (List <com .ning .http .client .Part >parts ,String contentType ,long contentLength ) {
5254this .boundary =MultipartEncodingUtil .getAsciiBytes (contentType .substring (contentType .indexOf ("boundary=" ) +"boundary=" .length ()));
5355this .parts =parts ;
5456this .contentLength =contentLength ;
55-
56- files =new ArrayList <RandomAccessFile >();
57-
58- startPart =0 ;
59- currentStreamPosition = -1 ;
60- endWritten =false ;
61- doneWritingParts =false ;
62- fileLocation =FileLocation .NONE ;
63- currentFilePart =null ;
6457 }
6558
6659public void close ()throws IOException {
@@ -180,7 +173,7 @@ public long read(ByteBuffer buffer) throws IOException {
180173
181174Part .sendMessageEnd (endWriter ,boundary );
182175
183- initializeBuffer (endWriter );
176+ initializeBuffer (endWriter . toByteArray () );
184177 }
185178
186179if (currentStreamPosition > -1 ) {
@@ -207,7 +200,7 @@ private void initializeByteArrayBody(FilePart filePart)
207200ByteArrayOutputStream output =new ByteArrayOutputStream ();
208201filePart .sendData (output );
209202
210- initializeBuffer (output );
203+ initializeBuffer (output . toByteArray () );
211204
212205fileLocation =FileLocation .MIDDLE ;
213206 }
@@ -217,7 +210,7 @@ private void initializeFileEnd(FilePart currentPart)
217210
218211ByteArrayOutputStream output =generateFileEnd (currentPart );
219212
220- initializeBuffer (output );
213+ initializeBuffer (output . toByteArray () );
221214
222215fileLocation =FileLocation .END ;
223216
@@ -238,6 +231,7 @@ private void initializeFileBody(FilePart currentPart)
238231currentFileChannel =raf .getChannel ();
239232
240233 }else {
234+ // ByteArrayPartSource
241235PartSource partSource =currentPart .getSource ();
242236
243237InputStream stream =partSource .createInputStream ();
@@ -261,7 +255,7 @@ private void initializeFilePart(FilePart filePart)
261255
262256ByteArrayOutputStream output =generateFileStart (filePart );
263257
264- initializeBuffer (output );
258+ initializeBuffer (output . toByteArray () );
265259
266260fileLocation =FileLocation .START ;
267261 }
@@ -274,7 +268,7 @@ private void initializeStringPart(StringPart currentPart)
274268
275269Part .sendPart (outputStream ,currentPart ,boundary );
276270
277- initializeBuffer (outputStream );
271+ initializeBuffer (outputStream . toByteArray () );
278272 }
279273
280274private int writeToBuffer (ByteBuffer buffer ,int length )
@@ -300,10 +294,10 @@ private int writeToBuffer(ByteBuffer buffer, int length)
300294return writeLength ;
301295 }
302296
303- private void initializeBuffer (ByteArrayOutputStream outputStream )
297+ private void initializeBuffer (byte [] bytes )
304298throws IOException {
305299
306- currentStream =new ByteArrayInputStream (outputStream . toByteArray () );
300+ currentStream =new ByteArrayInputStream (bytes );
307301
308302currentStreamPosition =0 ;
309303