Optimized URLEncodedUtils#parse(HttpEntity) method

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1302360 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2012-03-19 10:33:16 +00:00
parent 3f99045e4f
commit 5e0e2efe13

View File

@ -38,6 +38,7 @@
import java.util.Scanner;
import org.apache.http.annotation.Immutable;
import org.apache.http.entity.ContentType;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
@ -98,32 +99,15 @@ public static List <NameValuePair> parse (final URI uri, final String encoding)
*/
public static List <NameValuePair> parse (
final HttpEntity entity) throws IOException {
List <NameValuePair> result = Collections.emptyList();
String contentType = null;
String charset = null;
Header h = entity.getContentType();
if (h != null) {
HeaderElement[] elems = h.getElements();
if (elems.length > 0) {
HeaderElement elem = elems[0];
contentType = elem.getName();
NameValuePair param = elem.getParameterByName("charset");
if (param != null) {
charset = param.getValue();
}
}
}
if (contentType != null && contentType.equalsIgnoreCase(CONTENT_TYPE)) {
final String content = EntityUtils.toString(entity, HTTP.ASCII);
List <NameValuePair> result = null;
ContentType contentType = ContentType.get(entity);
if (contentType != null && contentType.getMimeType().equalsIgnoreCase(CONTENT_TYPE)) {
String content = EntityUtils.toString(entity, HTTP.ASCII);
if (content != null && content.length() > 0) {
result = new ArrayList <NameValuePair>();
parse(result, new Scanner(content), charset);
result = parse(content, contentType.getCharset());
}
}
return result;
return result != null ? result : new ArrayList<NameValuePair>();
}
/**
@ -188,12 +172,12 @@ public static void parse (
*
* @param s
* text to parse.
* @param encoding
* @param charset
* Encoding to use when decoding the parameters.
*
* @since 4.2
*/
public static List<NameValuePair> parse (final String s, final String encoding) {
public static List<NameValuePair> parse (final String s, final String charset) {
if (s == null) {
return Collections.emptyList();
}
@ -206,8 +190,8 @@ public static List<NameValuePair> parse (final String s, final String encoding)
NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, DELIM);
if (nvp.getName().length() > 0) {
list.add(new BasicNameValuePair(
decode(nvp.getName(), encoding),
decode(nvp.getValue(), encoding)));
decode(nvp.getName(), charset),
decode(nvp.getValue(), charset)));
}
}
return list;