Skip to content

Commit d1a4182

Browse files
author
Stephane Landelle
committed
Fix NPE when passing a null charset, close AsyncHttpClient#651
1 parent d536b47 commit d1a4182

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/main/java/com/ning/http/client/multipart/StringPart.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package com.ning.http.client.multipart;
1414

15+
import com.ning.http.util.StandardCharsets;
16+
1517
import java.io.ByteArrayOutputStream;
1618
import java.io.IOException;
1719
import java.io.OutputStream;
@@ -28,7 +30,7 @@ public class StringPart extends PartBase {
2830
/**
2931
* Default charset of string parameters
3032
*/
31-
public static final String DEFAULT_CHARSET = "US-ASCII";
33+
public static final Charset DEFAULT_CHARSET = StandardCharsets.US_ASCII;
3234

3335
/**
3436
* Default transfer encoding of string parameters
@@ -45,6 +47,10 @@ public StringPart(String name, String value, String charset) {
4547
this(name, value, charset, null);
4648
}
4749

50+
private static Charset charsetOrDefault(String charset) {
51+
return charset == null ? DEFAULT_CHARSET : Charset.forName(charset);
52+
}
53+
4854
/**
4955
* Constructor.
5056
*
@@ -58,15 +64,15 @@ public StringPart(String name, String value, String charset) {
5864
* the content id
5965
*/
6066
public StringPart(String name, String value, String charset, String contentId) {
61-
super(name, DEFAULT_CONTENT_TYPE, charset == null ? DEFAULT_CHARSET : charset, contentId, DEFAULT_TRANSFER_ENCODING);
62-
if (value == null) {
67+
super(name, DEFAULT_CONTENT_TYPE, charsetOrDefault(charset).name(), contentId, DEFAULT_TRANSFER_ENCODING);
68+
if (value == null)
6369
throw new NullPointerException("value");
64-
}
65-
if (value.indexOf(0) != -1) {
70+
71+
if (value.indexOf(0) != -1)
6672
// See RFC 2048, 2.8. "8bit Data"
6773
throw new IllegalArgumentException("NULs may not be present in string parts");
68-
}
69-
content = value.getBytes(Charset.forName(charset));
74+
75+
content = value.getBytes(charsetOrDefault(charset));
7076
this.value = value;
7177
}
7278

0 commit comments

Comments
 (0)