You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2007/04/13 16:59:46 UTC

svn commit: r528509 - in /jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/utils: ./ URLUtils.java

Author: olegk
Date: Fri Apr 13 07:59:45 2007
New Revision: 528509

URL: http://svn.apache.org/viewvc?view=rev&rev=528509
Log:
Ported URL encoding utils from Commons HttpClient

Added:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/utils/
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/utils/URLUtils.java
      - copied, changed from r528506, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/util/EncodingUtil.java

Copied: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/utils/URLUtils.java (from r528506, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/util/EncodingUtil.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/utils/URLUtils.java?view=diff&rev=528509&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/util/EncodingUtil.java&r1=528506&p2=jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/utils/URLUtils.java&r2=528509
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/util/EncodingUtil.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/utils/URLUtils.java Fri Apr 13 07:59:45 2007
@@ -1,5 +1,5 @@
 /*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/util/EncodingUtil.java,v 1.8 2004/05/13 04:01:22 mbecke Exp $
+ * $HeadURL$
  * $Revision$
  * $Date$
  *
@@ -27,32 +27,27 @@
  * <http://www.apache.org/>.
  *
  */
-package org.apache.commons.httpclient.util;
+package org.apache.http.client.utils;
 
 import java.io.UnsupportedEncodingException;
 
 import org.apache.commons.codec.net.URLCodec;
-import org.apache.commons.httpclient.HttpClientError;
-import org.apache.commons.httpclient.NameValuePair;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.apache.http.NameValuePair;
+import org.apache.http.util.CharArrayBuffer;
 
 /**
- * The home for utility methods that handle various encoding tasks.
+ * The home for utility methods that handle various URL encoding tasks.
  * 
  * @author Michael Becke
- * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  * 
  * @since 2.0 final
  */
-public class EncodingUtil {
+public class URLUtils {
 
     /** Default content encoding chatset */
     private static final String DEFAULT_CHARSET = "ISO-8859-1";
 
-    /** Log object for this class. */
-    private static final Log LOG = LogFactory.getLog(EncodingUtil.class);
-
     /**
      * Form-urlencoding routine.
      *
@@ -74,19 +69,17 @@
      * 
      * @return the urlencoded pairs
      * 
-     * @since 2.0 final
+     * @since 4.0
      */
-     public static String formUrlEncode(NameValuePair[] pairs, String charset) {
+     public static String formUrlEncode(final NameValuePair[] pairs, final String charset) {
         try {
             return doFormUrlEncode(pairs, charset);
         } catch (UnsupportedEncodingException e) {
-            LOG.error("Encoding not supported: " + charset);
             try {
                 return doFormUrlEncode(pairs, DEFAULT_CHARSET);
             } catch (UnsupportedEncodingException fatal) {
                 // Should never happen. ISO-8859-1 must be supported on all JVMs
-                throw new HttpClientError("Encoding not supported: " + 
-                    DEFAULT_CHARSET);
+                throw new Error("HttpClient requires " + DEFAULT_CHARSET + " support");
             }
         }
     }
@@ -111,10 +104,10 @@
      * 
      * @since 2.0 final
      */
-     private static String doFormUrlEncode(NameValuePair[] pairs, String charset)
-        throws UnsupportedEncodingException 
-     {
-        StringBuffer buf = new StringBuffer();
+     private static String doFormUrlEncode(
+             final NameValuePair[] pairs, 
+             final String charset) throws UnsupportedEncodingException {
+        CharArrayBuffer buf = new CharArrayBuffer(32);
         for (int i = 0; i < pairs.length; i++) {
             URLCodec codec = new URLCodec();
             NameValuePair pair = pairs[i];
@@ -133,156 +126,9 @@
     }
     
     /**
-     * Converts the byte array of HTTP content characters to a string. If
-     * the specified charset is not supported, default system encoding
-     * is used.
-     *
-     * @param data the byte array to be encoded
-     * @param offset the index of the first byte to encode
-     * @param length the number of bytes to encode 
-     * @param charset the desired character encoding
-     * @return The result of the conversion.
-     * 
-     * @since 3.0
-     */
-    public static String getString(
-        final byte[] data, 
-        int offset, 
-        int length, 
-        String charset
-    ) {
-
-        if (data == null) {
-            throw new IllegalArgumentException("Parameter may not be null");
-        }
-
-        if (charset == null || charset.length() == 0) {
-            throw new IllegalArgumentException("charset may not be null or empty");
-        }
-
-        try {
-            return new String(data, offset, length, charset);
-        } catch (UnsupportedEncodingException e) {
-
-            if (LOG.isWarnEnabled()) {
-                LOG.warn("Unsupported encoding: " + charset + ". System encoding used");
-            }
-            return new String(data, offset, length);
-        }
-    }
-
-
-    /**
-     * Converts the byte array of HTTP content characters to a string. If
-     * the specified charset is not supported, default system encoding
-     * is used.
-     *
-     * @param data the byte array to be encoded
-     * @param charset the desired character encoding
-     * @return The result of the conversion.
-     * 
-     * @since 3.0
-     */
-    public static String getString(final byte[] data, String charset) {
-        return getString(data, 0, data.length, charset);
-    }
-
-    /**
-     * Converts the specified string to a byte array.  If the charset is not supported the
-     * default system charset is used.
-     *
-     * @param data the string to be encoded
-     * @param charset the desired character encoding
-     * @return The resulting byte array.
-     * 
-     * @since 3.0
-     */
-    public static byte[] getBytes(final String data, String charset) {
-
-        if (data == null) {
-            throw new IllegalArgumentException("data may not be null");
-        }
-
-        if (charset == null || charset.length() == 0) {
-            throw new IllegalArgumentException("charset may not be null or empty");
-        }
-
-        try {
-            return data.getBytes(charset);
-        } catch (UnsupportedEncodingException e) {
-
-            if (LOG.isWarnEnabled()) {
-                LOG.warn("Unsupported encoding: " + charset + ". System encoding used.");
-            }
-            
-            return data.getBytes();
-        }
-    }    
-    
-    /**
-     * Converts the specified string to byte array of ASCII characters.
-     *
-     * @param data the string to be encoded
-     * @return The string as a byte array.
-     * 
-     * @since 3.0
-     */
-    public static byte[] getAsciiBytes(final String data) {
-
-        if (data == null) {
-            throw new IllegalArgumentException("Parameter may not be null");
-        }
-
-        try {
-            return data.getBytes("US-ASCII");
-        } catch (UnsupportedEncodingException e) {
-            throw new HttpClientError("HttpClient requires ASCII support");
-        }
-    }
-
-    /**
-     * Converts the byte array of ASCII characters to a string. This method is
-     * to be used when decoding content of HTTP elements (such as response
-     * headers)
-     *
-     * @param data the byte array to be encoded
-     * @param offset the index of the first byte to encode
-     * @param length the number of bytes to encode 
-     * @return The string representation of the byte array
-     * 
-     * @since 3.0
-     */
-    public static String getAsciiString(final byte[] data, int offset, int length) {
-
-        if (data == null) {
-            throw new IllegalArgumentException("Parameter may not be null");
-        }
-
-        try {
-            return new String(data, offset, length, "US-ASCII");
-        } catch (UnsupportedEncodingException e) {
-            throw new HttpClientError("HttpClient requires ASCII support");
-        }
-    }
-
-    /**
-     * Converts the byte array of ASCII characters to a string. This method is
-     * to be used when decoding content of HTTP elements (such as response
-     * headers)
-     *
-     * @param data the byte array to be encoded
-     * @return The string representation of the byte array
-     * 
-     * @since 3.0
-     */
-    public static String getAsciiString(final byte[] data) {
-        return getAsciiString(data, 0, data.length);
-    }
-
-    /**
      * This class should not be instantiated.
      */
-    private EncodingUtil() {
+    private URLUtils() {
     }
 
 }