Wrap URLEncoder nicely in StringUtil.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@1247 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray
2003-10-08 23:52:50 +00:00
parent 66cfb9ccf6
commit e6dbb1cf5b
5 changed files with 37 additions and 22 deletions
@@ -1,5 +1,5 @@
// //
// $Id: HttpPostUtil.java,v 1.1 2003/09/26 02:36:41 ray Exp $ // $Id: HttpPostUtil.java,v 1.2 2003/10/08 23:52:49 ray Exp $
package com.samskivert.net; package com.samskivert.net;
@@ -10,7 +10,6 @@ import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder;
import com.samskivert.util.ServiceWaiter; import com.samskivert.util.ServiceWaiter;
@@ -1,5 +1,5 @@
// //
// $Id: UserManager.java,v 1.18 2003/10/06 22:50:28 ray Exp $ // $Id: UserManager.java,v 1.19 2003/10/08 23:52:50 ray Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -20,7 +20,6 @@
package com.samskivert.servlet.user; package com.samskivert.servlet.user;
import java.net.URLEncoder;
import java.util.Properties; import java.util.Properties;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@@ -1,5 +1,5 @@
// //
// $Id: RequestUtils.java,v 1.8 2003/10/08 00:40:16 mdb Exp $ // $Id: RequestUtils.java,v 1.9 2003/10/08 23:52:50 ray Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -21,7 +21,6 @@
package com.samskivert.servlet.util; package com.samskivert.servlet.util;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import com.samskivert.util.StringUtil; import com.samskivert.util.StringUtil;
@@ -41,12 +40,7 @@ public class RequestUtils
*/ */
public static String getLocationEncoded (HttpServletRequest req) public static String getLocationEncoded (HttpServletRequest req)
{ {
String location = getLocation(req); return StringUtil.encode(getLocation(req));
try {
return URLEncoder.encode(location, "UTF-8");
} catch (UnsupportedEncodingException uee) {
return location;
}
} }
/** /**
@@ -1,5 +1,5 @@
// //
// $Id: StringUtil.java,v 1.57 2003/08/19 23:37:56 ray Exp $ // $Id: StringUtil.java,v 1.58 2003/10/08 23:52:50 ray Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -24,10 +24,15 @@ import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D; import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.security.MessageDigest; import java.security.MessageDigest;
@@ -635,6 +640,30 @@ public class StringUtil
} }
} }
/**
* URL encodes the specified string using the UTF-8 character encoding.
*/
public static String encode (String s)
{
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("UTF-8 is unknown in this Java.");
}
}
/**
* URL decodes the specified string using the UTF-8 character encoding.
*/
public static String decode (String s)
{
try {
return URLDecoder.decode(s, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("UTF-8 is unknown in this Java.");
}
}
/** /**
* Generates a string from the supplied bytes that is the HEX encoded * Generates a string from the supplied bytes that is the HEX encoded
* representation of those bytes. Returns the empty string for a * representation of those bytes. Returns the empty string for a
@@ -650,8 +679,8 @@ public class StringUtil
return ""; return "";
} }
char[] chars = new char[bytes.length*2];
count = Math.min(count, bytes.length); count = Math.min(count, bytes.length);
char[] chars = new char[count*2];
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
int val = (int)bytes[i]; int val = (int)bytes[i];
@@ -1,5 +1,5 @@
// //
// $Id: StringTool.java,v 1.10 2003/10/08 00:35:57 ray Exp $ // $Id: StringTool.java,v 1.11 2003/10/08 23:52:50 ray Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -20,7 +20,6 @@
package com.samskivert.velocity; package com.samskivert.velocity;
import java.net.URLEncoder;
import java.text.NumberFormat; import java.text.NumberFormat;
import com.samskivert.servlet.util.HTMLUtil; import com.samskivert.servlet.util.HTMLUtil;
@@ -44,12 +43,7 @@ public class StringTool
*/ */
public static String urlEncode (String text) public static String urlEncode (String text)
{ {
try { return StringUtil.encode(text);
return URLEncoder.encode(text, "UTF-8");
} catch (java.io.UnsupportedEncodingException uee) {
// won't happen
return text;
}
} }
/** /**