From 5e0e2efe13c6b72b4b9e9accc8283e52c446d1a4 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Mon, 19 Mar 2012 10:33:16 +0000 Subject: [PATCH] Optimized URLEncodedUtils#parse(HttpEntity) method git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1302360 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/client/utils/URLEncodedUtils.java | 38 ++++++------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java b/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java index aa18236d1..423345acf 100644 --- a/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java +++ b/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java @@ -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 parse (final URI uri, final String encoding) */ public static List parse ( final HttpEntity entity) throws IOException { - List 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 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 (); - parse(result, new Scanner(content), charset); + result = parse(content, contentType.getCharset()); } } - return result; + return result != null ? result : new ArrayList(); } /** @@ -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 parse (final String s, final String encoding) { + public static List parse (final String s, final String charset) { if (s == null) { return Collections.emptyList(); } @@ -206,8 +190,8 @@ public static List 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;