You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Luiz-Otavio Zorzella <zo...@codaware.com> on 2003/03/02 00:28:34 UTC

Slow encode method -- patch included

This patch fixes an incredibly inefficient, often-used encodeURL method. 
The merits of the patch should be self-evident --

1) it preserves the original goal of using the jdk 1.4's encode if 
available, while

2) it does not call 2 java encode method each time and

3) it does not do so much reflection each time around

Let me know of any concerns,

Zorzella

Re: Slow encode method -- patch included

Posted by Luiz-Otavio Zorzella <zo...@codaware.com>.
Sorry -- I hit a tiny typo there. Correct patch included.

Luiz-Otavio Zorzella wrote:
> This patch fixes an incredibly inefficient, often-used encodeURL method. 
> The merits of the patch should be self-evident --
> 
> 1) it preserves the original goal of using the jdk 1.4's encode if 
> available, while
> 
> 2) it does not call 2 java encode method each time and
> 
> 3) it does not do so much reflection each time around
> 
> Let me know of any concerns,
> 
> Zorzella
> 
> 
> ------------------------------------------------------------------------
> 
> Index: src/share/org/apache/struts/util/RequestUtils.java
> ===================================================================
> RCS file: /home/cvspublic/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v
> retrieving revision 1.90
> diff -u -r1.90 RequestUtils.java
> --- src/share/org/apache/struts/util/RequestUtils.java	26 Feb 2003 04:48:56 -0000	1.90
> +++ src/share/org/apache/struts/util/RequestUtils.java	1 Mar 2003 23:23:00 -0000
> @@ -1896,6 +1896,19 @@
>          return errors;
>      }
>  
> +    private static Method _encode = null;
> +
> +    static {
> +        try {
> +            // get version of encode method with two String args
> +            Class[] args = new Class[] { String.class, String.class };
> +            _encode = URLEncoder.class.getMethod("encode", args);
> +	} catch (Exception e) {
> +            log.debug("Could not find Java 1.4 encode method.  Using deprecated version.", e);
> +	}
> +    }
> +
> +    
>      /**
>       * Use the new URLEncoder.encode() method from java 1.4 if available, else
>       * use the old deprecated version.  This method uses reflection to find the appropriate
> @@ -1904,27 +1917,15 @@
>       * @return String - the encoded url.
>       */
>      public static String encodeURL(String url) {
> -        // default to old version
> -        String encodedURL = URLEncoder.encode(url);
> -        Class encoderClass = URLEncoder.class;
>  
>          try {
> -            // get version of encode method with two String args
> -            Class[] args = new Class[] { String.class, String.class };
> -            Method encode = encoderClass.getMethod("encode", args);
> -
> -            // encode url with new 1.4 method and UTF-8 encoding
> -            encodedURL = (String) encode.invoke(null, new Object[] { url, "UTF-8" });
> -
> -        } catch (IllegalAccessException e) {
> -            log.debug("Could not find Java 1.4 encode method.  Using deprecated version.", e);
> -        } catch (InvocationTargetException e) {
> -            log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
> -        } catch (NoSuchMethodException e) {
> +            if (_encode == null)
> +                return (String) _encode.invoke(null, new Object[] { url, "UTF-8" });
> +        } catch (Exception e) {
>              log.debug("Could not find Java 1.4 encode method.  Using deprecated version.", e);
>          }
> -
> -        return encodedURL;
> +        
> +        return URLEncoder.encode(url);
>      }
>  
>  }
> 
> 
> 
> ------------------------------------------------------------------------
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-dev-help@jakarta.apache.org