32
32
33
33
public class MultipartBody implements RandomAccessBody {
34
34
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 ;
40
35
private 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 ;
48
50
49
51
enum FileLocation {NONE , START , MIDDLE , END }
50
52
51
53
public MultipartBody (List <com .ning .http .client .Part > parts , String contentType , long contentLength ) {
52
54
this .boundary = MultipartEncodingUtil .getAsciiBytes (contentType .substring (contentType .indexOf ("boundary=" ) + "boundary=" .length ()));
53
55
this .parts = parts ;
54
56
this .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 ;
64
57
}
65
58
66
59
public void close () throws IOException {
@@ -180,7 +173,7 @@ public long read(ByteBuffer buffer) throws IOException {
180
173
181
174
Part .sendMessageEnd (endWriter , boundary );
182
175
183
- initializeBuffer (endWriter );
176
+ initializeBuffer (endWriter . toByteArray () );
184
177
}
185
178
186
179
if (currentStreamPosition > -1 ) {
@@ -207,7 +200,7 @@ private void initializeByteArrayBody(FilePart filePart)
207
200
ByteArrayOutputStream output = new ByteArrayOutputStream ();
208
201
filePart .sendData (output );
209
202
210
- initializeBuffer (output );
203
+ initializeBuffer (output . toByteArray () );
211
204
212
205
fileLocation = FileLocation .MIDDLE ;
213
206
}
@@ -217,7 +210,7 @@ private void initializeFileEnd(FilePart currentPart)
217
210
218
211
ByteArrayOutputStream output = generateFileEnd (currentPart );
219
212
220
- initializeBuffer (output );
213
+ initializeBuffer (output . toByteArray () );
221
214
222
215
fileLocation = FileLocation .END ;
223
216
@@ -238,6 +231,7 @@ private void initializeFileBody(FilePart currentPart)
238
231
currentFileChannel = raf .getChannel ();
239
232
240
233
} else {
234
+ // ByteArrayPartSource
241
235
PartSource partSource = currentPart .getSource ();
242
236
243
237
InputStream stream = partSource .createInputStream ();
@@ -261,7 +255,7 @@ private void initializeFilePart(FilePart filePart)
261
255
262
256
ByteArrayOutputStream output = generateFileStart (filePart );
263
257
264
- initializeBuffer (output );
258
+ initializeBuffer (output . toByteArray () );
265
259
266
260
fileLocation = FileLocation .START ;
267
261
}
@@ -274,7 +268,7 @@ private void initializeStringPart(StringPart currentPart)
274
268
275
269
Part .sendPart (outputStream , currentPart , boundary );
276
270
277
- initializeBuffer (outputStream );
271
+ initializeBuffer (outputStream . toByteArray () );
278
272
}
279
273
280
274
private int writeToBuffer (ByteBuffer buffer , int length )
@@ -300,10 +294,10 @@ private int writeToBuffer(ByteBuffer buffer, int length)
300
294
return writeLength ;
301
295
}
302
296
303
- private void initializeBuffer (ByteArrayOutputStream outputStream )
297
+ private void initializeBuffer (byte [] bytes )
304
298
throws IOException {
305
299
306
- currentStream = new ByteArrayInputStream (outputStream . toByteArray () );
300
+ currentStream = new ByteArrayInputStream (bytes );
307
301
308
302
currentStreamPosition = 0 ;
309
303
0 commit comments