Skip to content

Commit 6efae41

Browse files
author
Stephane Landelle
committed
Compute Content-Length in constructor
1 parent e4bec39 commit 6efae41

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

src/main/java/com/ning/http/multipart/MultipartRequestEntity.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
2121

22-
import org.slf4j.Logger;
23-
import org.slf4j.LoggerFactory;
24-
2522
import java.io.IOException;
2623
import java.io.OutputStream;
2724
import java.util.Random;
@@ -57,16 +54,16 @@ public static byte[] generateMultipartBoundary() {
5754
return bytes;
5855
}
5956

60-
private final Logger log = LoggerFactory.getLogger(MultipartRequestEntity.class);
61-
6257
/**
6358
* The MIME parts as set by the constructor
6459
*/
65-
protected Part[] parts;
60+
protected final Part[] parts;
6661

6762
private final byte[] multipartBoundary;
6863

6964
private final String contentType;
65+
66+
private final long contentLength;
7067

7168
/**
7269
* Creates a new multipart entity containing the given parts.
@@ -93,6 +90,8 @@ public MultipartRequestEntity(Part[] parts, FluentCaseInsensitiveStringsMap requ
9390
multipartBoundary = generateMultipartBoundary();
9491
contentType = computeContentType(MULTIPART_FORM_CONTENT_TYPE);
9592
}
93+
94+
contentLength = Part.getLengthOfParts(parts, multipartBoundary);
9695
}
9796

9897
private String computeContentType(String base) {
@@ -128,12 +127,7 @@ public void writeRequest(OutputStream out) throws IOException {
128127
}
129128

130129
public long getContentLength() {
131-
try {
132-
return Part.getLengthOfParts(parts, multipartBoundary);
133-
} catch (Exception e) {
134-
log.error("An exception occurred while getting the length of the parts", e);
135-
return 0L;
136-
}
130+
return contentLength;
137131
}
138132

139133
public String getContentType() {

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818
import java.io.IOException;
1919
import java.io.OutputStream;
2020

21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
2124
/**
2225
* This class is an adaptation of the Apache HttpClient implementation
2326
*
2427
* @link http://hc.apache.org/httpclient-3.x/
2528
*/
2629
public abstract class Part implements com.ning.http.client.Part {
2730

31+
private static final Logger LOGGER = LoggerFactory.getLogger(Part.class);
32+
2833
/**
2934
* Carriage return/linefeed
3035
*/
@@ -443,21 +448,27 @@ public static void sendPart(OutputStream out, Part part, byte[] partBoundary) th
443448
* @since 3.0
444449
*/
445450
public static long getLengthOfParts(Part[] parts, byte[] partBoundary) {
446-
if (parts == null) {
447-
throw new IllegalArgumentException("Parts may not be null");
448-
}
449-
long total = 0;
450-
for (Part part : parts) {
451-
long l = part.length(partBoundary);
452-
if (l < 0) {
453-
return -1;
451+
452+
try {
453+
if (parts == null) {
454+
throw new IllegalArgumentException("Parts may not be null");
455+
}
456+
long total = 0;
457+
for (Part part : parts) {
458+
long l = part.length(partBoundary);
459+
if (l < 0) {
460+
return -1;
461+
}
462+
total += l;
454463
}
455-
total += l;
464+
total += EXTRA_BYTES.length;
465+
total += partBoundary.length;
466+
total += EXTRA_BYTES.length;
467+
total += CRLF_BYTES.length;
468+
return total;
469+
} catch (Exception e) {
470+
LOGGER.error("An exception occurred while getting the length of the parts", e);
471+
return 0L;
456472
}
457-
total += EXTRA_BYTES.length;
458-
total += partBoundary.length;
459-
total += EXTRA_BYTES.length;
460-
total += CRLF_BYTES.length;
461-
return total;
462473
}
463474
}

0 commit comments

Comments
 (0)