Link to home
Start Free TrialLog in
Avatar of alpharvs
alpharvs

asked on

URLDecoder?

Hi there

I need a method for decoding a urlencoded string or bytearray.

Avatar of pyxide
pyxide

It should help :
Download :
ftp://ftp.oreilly.com/published/oreilly/java/java.netprog/javanetexamples.zip

Once deziped :
javanetexamples\05\URLDecoder.java
ASKER CERTIFIED SOLUTION
Avatar of mbormann
mbormann

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
pyxide,
i didn't look at it while answering but the downloaded example is less of a clutter but the Java source is better if u want a name value pair relationship.

regards

  public static String decode(String s) {
 
    ByteArrayOutputStream out = new ByteArrayOutputStream(s.length());
 
    for (int i = 0; i < s.length(); i++) {
      int c = (int) s.charAt(i);
      if (c == '+') {
        out.write(' ');
      }
      else if (c == '%') {
        int c1 = Character.digit(s.charAt(++i), 16);
        int c2 = Character.digit(s.charAt(++i), 16);
        out.write((char) (c1 * 16 + c2));
      }
      else {
        out.write(c);
      }
    } // end for

    return out.toString();
   
  }
to mbormann -

I wrote a class to handle name/value pair for a tiny java HTTP server
which is pretty close to the javax.servlet.http

I added some methods to recover typed values.

Might be usefull if you want a little test server without javax packages.

Regards
-----------------------
import java.util.Hashtable;
import java.util.StringTokenizer;

/**
 * FormRequest.
 *
 *
 */
public class FormRequest
{

    protected Hashtable hashtable;

    public FormRequest(String st_data)
    {
        if( st_data!= null && st_data != ConstantesHTTP.ST_EMPTY)
        {

            hashtable = new Hashtable();
            StringTokenizer STK_data = new StringTokenizer(URLDecoder.decode(st_data),ConstantesHTTP.ST_PARAM_SEPARATOR,false);

            while(STK_data.hasMoreElements())
            {
                String st_key_value = STK_data.nextToken();
                // "="
                if( st_key_value.indexOf(ConstantesHTTP.ST_KEY_VALUE_SEPARATOR)>0 )
                {

                    String st_key = st_key_value.substring(0,st_key_value.indexOf(ConstantesHTTP.ST_KEY_VALUE_SEPARATOR));

                    if(!hashtable.containsKey(st_key))
                        hashtable.put(st_key,
                                        st_key_value.substring(st_key_value.indexOf(ConstantesHTTP.ST_KEY_VALUE_SEPARATOR)+1, st_key_value.length() ) );
                    else
                        hashtable.put(st_key,
                                        (String)hashtable.get(st_key) + "," + st_key_value.substring(st_key_value.indexOf(ConstantesHTTP.ST_KEY_VALUE_SEPARATOR)+1, st_key_value.length() ) );
                }
                else //
                {
                    if(!hashtable.containsKey(st_key_value))
                        hashtable.put(st_key_value,ConstantesHTTP.ST_EMPTY );
                    else
                        hashtable.put(st_key_value,
                                        (String)hashtable.get(st_key_value) + "," + ConstantesHTTP.ST_EMPTY );

                }
            }
        }
    }

    public String get(String st_key)
    {
        return get(st_key, ConstantesHTTP.ST_EMPTY);
    }

    public String get(String st_key, String st_replace)
    {
        if(isValidRetrieval(st_key))
            return (String) hashtable.get(st_key);
        else
            return st_replace;
    }

    /**
     * Return getInt(String st_key, 0);
     *
     * @param st_key name of the parameter.
     * @return an int ( 0 by default or if an exception is raised).
     */
    public int getIntValue(String st_key)
    {
        return getIntValue(st_key, 0);
    }

    /**
     * Recover a int value.
     *
     * @param st_key name of the parameter.
     * @param i_replace default return value.
     * @return an int ( i_replace by default or if an exception is raised).
     */
    public int getIntValue(String st_key, int i_replace)
    {
        if(isValidRetrieval(st_key))
        {
            try
            {
                return Integer.parseInt(((String)hashtable.get(st_key)).trim(),10);
            }
            catch(Exception e)
            {
                return i_replace;
            }
        }
        else
            return i_replace;
    }

    /**
     * Return getBooleanValue(String st_key, false);
     *
     * @param st_key name of the parameter.
     * @return a boolean ( false by default or if an exception is raised).
     */
    public boolean getBooleanValue(String st_key)
    {
        return getBooleanValue(st_key, false);
    }

    /**
     * Recover a boolean value.
     *
     * @param st_key name of the parameter.
     * @param b_replace default value.
     * @return a boolean ( b_replace by default or if an exception is raised).
     */
    public boolean getBooleanValue(String st_key, boolean b_replace)
    {
        if(isValidRetrieval(st_key))
        {
            return ConstantesHTTP.isTrue( (String)hashtable.get(st_key));
        }
        else
            return b_replace;
    }

    public boolean isKey(String st_key)
    {
        return isValidRetrieval(st_key);
    }

    public boolean isValidRetrieval(String st_key)
    {
        return ( (hashtable != null) && hashtable.containsKey(st_key) );
    }
}
------------------------------------
  /**
   * Constantes.
   *
   */
public final class ConstantesHTTP
{
    static public final String ST_PARAM_SEPARATOR = "&";
    static public final String ST_KEY_VALUE_SEPARATOR = "=";

    static public final String ST_EMPTY = "";
    static public final String ST_TRUE = "true";
    static public final String ST_FALSE = "false";
    static public final String ST_ON = "on";
    static public final String ST_OFF = "off";
    static public final String ST_0 = "0";


    /**
     * Return true
     * if st_boolean!="false"(case insensitive)
     * and st_boolean!="0"
     * and st_boolean!=""
     * and st_boolean!="off"(case insensitive)
     *
     * @return a boolean parse from the string.
     */
    public final static boolean isTrue(String st_boolean)
    {
        return (!st_boolean.trim().toLowerCase().equals(ST_FALSE) && !st_boolean.trim().toLowerCase().equals(ST_0) && !st_boolean.equals(ST_EMPTY) && !st_boolean.trim().toLowerCase().equals(ST_OFF));
    }
}
thanks very much
I have also to thank you for all the comments and sites in the "garbage collection" question from Jdoit. I'll read article on Train algorithm this week-end.
Hi,
try
java.net.URLEncoder.encode(line)

Micha
pyxide,

it's very good ,isn't it? I LOVE anything connected with Gc() or speeding up Java.
here's more from TiJ

Web sites

[5] The premiere online references for optimizing Java code are Jonathan Hardwick’s Java Optimization site at http://www.cs.cmu.edu/~jch/java/optimization.html 
“Tools for Optimizing Java” at
http://www.cs.cmu.edu/~jch/java/tools.html
, and “Java Microbenchmarks” (with a quick 45 second measurement benchmark) at
http://www.cs.cmu.edu/~jch/java/benchmarks.html

Articles

[6] Make Java fast: Optimize! How to get the greatest performance out of your code through low-level optimizations in Java by Doug Bell
http://www.javaworld.com/javaworld/jw-04-1997/jw-04-optimize.html, complete with an extensive annotated measurement Benchmark applet.

[7] Java Optimization Resources http://www.cs.cmu.edu/~jch/java/resources.html

[8] Optimizing Java for Speed
http://www.cs.cmu.edu/~jch/java/speed.html

[9] An Empirical Study of FORTRAN Programs by Donald Knuth, 1971, Software – Practice
and Experience, Volume 1 p. 105-33.

[10] Building High-Performance Applications and Servers in Java: An Experiential Study , by Jimmy Nguyen, Michael Fraenkel, Richard Redpath, Binh Q. Nguyen, and Sandeep K. Singhal;
IBM Software Solutions, IBM T.J. Watson Research Center.
http://www.ibm.com/java/education/javahipr.html