1515 */
1616package com .ning .http .multipart ;
1717
18- import java .io .ByteArrayOutputStream ;
1918import java .io .IOException ;
2019import java .io .OutputStream ;
2120
@@ -212,6 +211,10 @@ protected void sendStart(OutputStream out) throws IOException {
212211out .write (getPartBoundary ());
213212 }
214213
214+ private int startLength () {
215+ return EXTRA_BYTES .length +getPartBoundary ().length ;
216+ }
217+
215218/**
216219 * Write the content disposition header to the specified output stream
217220 *
@@ -228,6 +231,18 @@ protected void sendDispositionHeader(OutputStream out) throws IOException {
228231 }
229232 }
230233
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+
231246/**
232247 * Write the content type header to the specified output stream
233248 *
@@ -248,6 +263,22 @@ protected void sendContentTypeHeader(OutputStream out) throws IOException {
248263 }
249264 }
250265
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+
251282/**
252283 * Write the content transfer encoding header to the specified output stream
253284 *
@@ -263,6 +294,17 @@ protected void sendTransferEncodingHeader(OutputStream out) throws IOException {
263294 }
264295 }
265296
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+
266308/**
267309 * Write the content ID header to the specified output stream
268310 *
@@ -278,6 +320,17 @@ protected void sendContentIdHeader(OutputStream out) throws IOException {
278320 }
279321 }
280322
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+
281334/**
282335 * Write the end of the header to the output stream
283336 *
@@ -289,6 +342,10 @@ protected void sendEndOfHeader(OutputStream out) throws IOException {
289342out .write (CRLF_BYTES );
290343 }
291344
345+ protected int endOfHeaderLength () {
346+ return CRLF_BYTES .length *2 ;
347+ }
348+
292349/**
293350 * Write the data to the specified output stream
294351 *
@@ -315,6 +372,10 @@ protected void sendEnd(OutputStream out) throws IOException {
315372out .write (CRLF_BYTES );
316373 }
317374
375+ protected int endLength () {
376+ return CRLF_BYTES .length ;
377+ }
378+
318379/**
319380 * Write all the data to the output stream. If you override this method make sure to override #length() as well
320381 *
@@ -339,18 +400,21 @@ public void send(OutputStream out) throws IOException {
339400 * @throws IOException If an IO problem occurs
340401 */
341402public long length ()throws IOException {
342- if (lengthOfData () <0 ) {
403+
404+ long lengthOfData =lengthOfData ();
405+
406+ if (lengthOfData <0 ) {
343407return -1 ;
408+ }else {
409+ return lengthOfData //
410+ +startLength ()//
411+ +dispositionHeaderLength ()//
412+ +contentTypeHeaderLength ()//
413+ +transferEncodingHeaderLength ()//
414+ +contentIdHeaderLength ()//
415+ +endOfHeaderLength ()//
416+ +endLength ();
344417 }
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 ();
354418 }
355419
356420/**