15
15
*/
16
16
package com .ning .http .multipart ;
17
17
18
- import java .io .ByteArrayOutputStream ;
19
18
import java .io .IOException ;
20
19
import java .io .OutputStream ;
21
20
@@ -212,6 +211,10 @@ protected void sendStart(OutputStream out) throws IOException {
212
211
out .write (getPartBoundary ());
213
212
}
214
213
214
+ private int startLength () {
215
+ return EXTRA_BYTES .length + getPartBoundary ().length ;
216
+ }
217
+
215
218
/**
216
219
* Write the content disposition header to the specified output stream
217
220
*
@@ -228,6 +231,18 @@ protected void sendDispositionHeader(OutputStream out) throws IOException {
228
231
}
229
232
}
230
233
234
+ protected int dispositionHeaderLength () {
235
+ int length = 0 ;
236
+ if (getName () != null ) {
237
+ length += CRLF_BYTES .length ;
238
+ length += CONTENT_DISPOSITION_BYTES .length ;
239
+ length += QUOTE_BYTES .length ;
240
+ length += MultipartEncodingUtil .getAsciiBytes (getName ()).length ;
241
+ length += QUOTE_BYTES .length ;
242
+ }
243
+ return length ;
244
+ }
245
+
231
246
/**
232
247
* Write the content type header to the specified output stream
233
248
*
@@ -248,6 +263,22 @@ protected void sendContentTypeHeader(OutputStream out) throws IOException {
248
263
}
249
264
}
250
265
266
+ protected int contentTypeHeaderLength () {
267
+ int length = 0 ;
268
+ String contentType = getContentType ();
269
+ if (contentType != null ) {
270
+ length += CRLF_BYTES .length ;
271
+ length += CONTENT_TYPE_BYTES .length ;
272
+ length += MultipartEncodingUtil .getAsciiBytes (contentType ).length ;
273
+ String charSet = getCharSet ();
274
+ if (charSet != null ) {
275
+ length += CHARSET_BYTES .length ;
276
+ length += MultipartEncodingUtil .getAsciiBytes (charSet ).length ;
277
+ }
278
+ }
279
+ return length ;
280
+ }
281
+
251
282
/**
252
283
* Write the content transfer encoding header to the specified output stream
253
284
*
@@ -263,6 +294,17 @@ protected void sendTransferEncodingHeader(OutputStream out) throws IOException {
263
294
}
264
295
}
265
296
297
+ protected int transferEncodingHeaderLength () {
298
+ int length = 0 ;
299
+ String transferEncoding = getTransferEncoding ();
300
+ if (transferEncoding != null ) {
301
+ length += CRLF_BYTES .length ;
302
+ length += CONTENT_TRANSFER_ENCODING_BYTES .length ;
303
+ length += MultipartEncodingUtil .getAsciiBytes (transferEncoding ).length ;
304
+ }
305
+ return length ;
306
+ }
307
+
266
308
/**
267
309
* Write the content ID header to the specified output stream
268
310
*
@@ -278,6 +320,17 @@ protected void sendContentIdHeader(OutputStream out) throws IOException {
278
320
}
279
321
}
280
322
323
+ protected int contentIdHeaderLength () {
324
+ int length = 0 ;
325
+ String contentId = getContentId ();
326
+ if (contentId != null ) {
327
+ length += CRLF_BYTES .length ;
328
+ length += CONTENT_ID_BYTES .length ;
329
+ length += MultipartEncodingUtil .getAsciiBytes (contentId ).length ;
330
+ }
331
+ return length ;
332
+ }
333
+
281
334
/**
282
335
* Write the end of the header to the output stream
283
336
*
@@ -289,6 +342,10 @@ protected void sendEndOfHeader(OutputStream out) throws IOException {
289
342
out .write (CRLF_BYTES );
290
343
}
291
344
345
+ protected int endOfHeaderLength () {
346
+ return CRLF_BYTES .length * 2 ;
347
+ }
348
+
292
349
/**
293
350
* Write the data to the specified output stream
294
351
*
@@ -315,6 +372,10 @@ protected void sendEnd(OutputStream out) throws IOException {
315
372
out .write (CRLF_BYTES );
316
373
}
317
374
375
+ protected int endLength () {
376
+ return CRLF_BYTES .length ;
377
+ }
378
+
318
379
/**
319
380
* Write all the data to the output stream. If you override this method make sure to override #length() as well
320
381
*
@@ -339,18 +400,21 @@ public void send(OutputStream out) throws IOException {
339
400
* @throws IOException If an IO problem occurs
340
401
*/
341
402
public long length () throws IOException {
342
- if (lengthOfData () < 0 ) {
403
+
404
+ long lengthOfData = lengthOfData ();
405
+
406
+ if (lengthOfData < 0 ) {
343
407
return -1 ;
408
+ } else {
409
+ return lengthOfData //
410
+ + startLength ()//
411
+ + dispositionHeaderLength ()//
412
+ + contentTypeHeaderLength ()//
413
+ + transferEncodingHeaderLength ()//
414
+ + contentIdHeaderLength ()//
415
+ + endOfHeaderLength ()//
416
+ + endLength ();
344
417
}
345
- ByteArrayOutputStream overhead = new ByteArrayOutputStream ();
346
- sendStart (overhead );
347
- sendDispositionHeader (overhead );
348
- sendContentTypeHeader (overhead );
349
- sendTransferEncodingHeader (overhead );
350
- sendContentIdHeader (overhead );
351
- sendEndOfHeader (overhead );
352
- sendEnd (overhead );
353
- return overhead .size () + lengthOfData ();
354
418
}
355
419
356
420
/**
0 commit comments