Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef1372f

Browse files
author
Stephane Landelle
committed
Don't write all the bytes just to compute the length!!!
1 parentfb86b39 commitef1372f

File tree

2 files changed

+87
-11
lines changed

2 files changed

+87
-11
lines changed

‎src/main/java/com/ning/http/multipart/FilePart.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ protected void sendDispositionHeader(OutputStream out) throws IOException {
156156
}
157157
}
158158

159+
protectedintdispositionHeaderLength() {
160+
Stringfilename =this.source.getFileName();
161+
intlength =super.dispositionHeaderLength();
162+
if (filename !=null) {
163+
length +=FILE_NAME_BYTES.length;
164+
length +=QUOTE_BYTES.length;
165+
length +=MultipartEncodingUtil.getAsciiBytes(filename).length;
166+
length +=QUOTE_BYTES.length;
167+
}
168+
returnlength;
169+
}
170+
159171
/**
160172
* Write the data in "source" to the specified stream.
161173
*

‎src/main/java/com/ning/http/multipart/Part.java‎

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
packagecom.ning.http.multipart;
1717

18-
importjava.io.ByteArrayOutputStream;
1918
importjava.io.IOException;
2019
importjava.io.OutputStream;
2120

@@ -212,6 +211,10 @@ protected void sendStart(OutputStream out) throws IOException {
212211
out.write(getPartBoundary());
213212
}
214213

214+
privateintstartLength() {
215+
returnEXTRA_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+
protectedintdispositionHeaderLength() {
235+
intlength =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+
returnlength;
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+
protectedintcontentTypeHeaderLength() {
267+
intlength =0;
268+
StringcontentType =getContentType();
269+
if (contentType !=null) {
270+
length +=CRLF_BYTES.length;
271+
length +=CONTENT_TYPE_BYTES.length;
272+
length +=MultipartEncodingUtil.getAsciiBytes(contentType).length;
273+
StringcharSet =getCharSet();
274+
if (charSet !=null) {
275+
length +=CHARSET_BYTES.length;
276+
length +=MultipartEncodingUtil.getAsciiBytes(charSet).length;
277+
}
278+
}
279+
returnlength;
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+
protectedinttransferEncodingHeaderLength() {
298+
intlength =0;
299+
StringtransferEncoding =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+
returnlength;
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+
protectedintcontentIdHeaderLength() {
324+
intlength =0;
325+
StringcontentId =getContentId();
326+
if (contentId !=null) {
327+
length +=CRLF_BYTES.length;
328+
length +=CONTENT_ID_BYTES.length;
329+
length +=MultipartEncodingUtil.getAsciiBytes(contentId).length;
330+
}
331+
returnlength;
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 {
289342
out.write(CRLF_BYTES);
290343
}
291344

345+
protectedintendOfHeaderLength() {
346+
returnCRLF_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 {
315372
out.write(CRLF_BYTES);
316373
}
317374

375+
protectedintendLength() {
376+
returnCRLF_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
*/
341402
publiclonglength()throwsIOException {
342-
if (lengthOfData() <0) {
403+
404+
longlengthOfData =lengthOfData();
405+
406+
if (lengthOfData <0) {
343407
return -1;
408+
}else {
409+
returnlengthOfData//
410+
+startLength()//
411+
+dispositionHeaderLength()//
412+
+contentTypeHeaderLength()//
413+
+transferEncodingHeaderLength()//
414+
+contentIdHeaderLength()//
415+
+endOfHeaderLength()//
416+
+endLength();
344417
}
345-
ByteArrayOutputStreamoverhead =newByteArrayOutputStream();
346-
sendStart(overhead);
347-
sendDispositionHeader(overhead);
348-
sendContentTypeHeader(overhead);
349-
sendTransferEncodingHeader(overhead);
350-
sendContentIdHeader(overhead);
351-
sendEndOfHeader(overhead);
352-
sendEnd(overhead);
353-
returnoverhead.size() +lengthOfData();
354418
}
355419

356420
/**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp