harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r535560 - /harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLDecoder.java
Date Sat, 05 May 2007 20:13:27 GMT
Author: tellison
Date: Sat May  5 13:13:26 2007
New Revision: 535560

URL: http://svn.apache.org/viewvc?view=rev&rev=535560
Log:
Apply patch HARMONY-3793 ([classlib][luni] Performance improvement for java.net.URLDecoder.decode())

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLDecoder.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLDecoder.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLDecoder.java?view=diff&rev=535560&r1=535559&r2=535560
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLDecoder.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLDecoder.java Sat
May  5 13:13:26 2007
@@ -17,11 +17,14 @@
 
 package java.net;
 
-import java.io.ByteArrayOutputStream;
 import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.UnsupportedCharsetException;
 
 import org.apache.harmony.luni.util.Msg;
-import org.apache.harmony.luni.util.Util;
 
 /**
  * This class is used to decode a string which is encoded in the
@@ -29,6 +32,8 @@
  */
 public class URLDecoder {
 
+    static Charset defaultCharset;
+
     /**
      * Decodes the string argument which is assumed to be encoded in the
      * <code>x-www-form-urlencoded</code> MIME content type.
@@ -47,7 +52,22 @@
      */
     @Deprecated
     public static String decode(String s) {
-        return Util.decode(s, true);
+
+        if (defaultCharset == null) {
+            try {
+                defaultCharset = Charset.forName(
+                        System.getProperty("file.encoding")); //$NON-NLS-1$
+            } catch (IllegalCharsetNameException e) {
+                // Ignored
+            } catch (UnsupportedCharsetException e) {
+                // Ignored
+            }
+
+            if (defaultCharset == null) {
+                defaultCharset = Charset.forName("ISO-8859-1"); //$NON-NLS-1$
+            }
+        }
+        return decode(s, defaultCharset);
     }
 
     /**
@@ -77,40 +97,80 @@
 
         // If the given encoding is an empty string throw an exception.
         if (enc.length() == 0) {
-            throw new UnsupportedEncodingException(Msg
-                    .getString("K00a5", "enc")); //$NON-NLS-1$ //$NON-NLS-2$
+            throw new UnsupportedEncodingException(
+                    // K00a5=Invalid parameter - {0}
+                    Msg.getString("K00a5", "enc")); //$NON-NLS-1$ //$NON-NLS-2$
+        }
+
+        if (s.indexOf('%') == -1) {
+            if (s.indexOf('+') == -1)
+                return s;
+            char str[] = s.toCharArray();
+            for (int i = 0; i < str.length; i++) {
+                if (str[i] == '+')
+                    str[i] = ' ';
+            }
+            return new String(str);
         }
 
-        StringBuffer result = new StringBuffer(s.length());
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        Charset charset = null;
+        try {
+            charset = Charset.forName(enc);
+        } catch (IllegalCharsetNameException e) {
+            throw (UnsupportedEncodingException) (new UnsupportedEncodingException(
+                    enc).initCause(e));
+        } catch (UnsupportedCharsetException e) {
+            throw (UnsupportedEncodingException) (new UnsupportedEncodingException(
+                    enc).initCause(e));
+        }
+
+        return decode(s, charset);
+    }
+
+    private static String decode(String s, Charset charset) {
+
+        char str_buf[] = new char[s.length()];
+        byte buf[] = new byte[s.length() / 3];
+        int buf_len = 0;
+
         for (int i = 0; i < s.length();) {
             char c = s.charAt(i);
             if (c == '+') {
-                result.append(' ');
+                str_buf[buf_len] = ' ';
             } else if (c == '%') {
-                out.reset();
+
+                int len = 0;
                 do {
                     if (i + 2 >= s.length()) {
-                        throw new IllegalArgumentException(Msg.getString(
-                                "K01fe", i)); //$NON-NLS-1$
+                        throw new IllegalArgumentException(
+                                // K01fe=Incomplete % sequence at\: {0}
+                                Msg.getString("K01fe", i)); //$NON-NLS-1$
                     }
                     int d1 = Character.digit(s.charAt(i + 1), 16);
                     int d2 = Character.digit(s.charAt(i + 2), 16);
                     if (d1 == -1 || d2 == -1) {
-                        throw new IllegalArgumentException(Msg.getString(
-                                "K01ff", //$NON-NLS-1$
-                                s.substring(i, i + 3), String.valueOf(i)));
+                        throw new IllegalArgumentException(
+                                // K01ff=Invalid % sequence ({0}) at\: {1}
+                                Msg.getString(
+                                        "K01ff", //$NON-NLS-1$
+                                        s.substring(i, i + 3),
+                                        String.valueOf(i)));
                     }
-                    out.write((byte) ((d1 << 4) + d2));
+                    buf[len++] = (byte) ((d1 << 4) + d2);
                     i += 3;
                 } while (i < s.length() && s.charAt(i) == '%');
-                result.append(out.toString(enc));
+
+                CharBuffer cb = charset.decode(ByteBuffer.wrap(buf, 0, len));
+                len = cb.length();
+                System.arraycopy(cb.array(), 0, str_buf, buf_len, len);
+                buf_len += len;
                 continue;
             } else {
-                result.append(c);
+                str_buf[buf_len] = c;
             }
             i++;
+            buf_len++;
         }
-        return result.toString();
+        return new String(str_buf, 0, buf_len);
     }
 }



Mime
View raw message