aboutsummaryrefslogtreecommitdiff
path: root/common/src/main/java/at/gv/egovernment/moa/util/URLDecoder.java
diff options
context:
space:
mode:
authorpdanner <pdanner@d688527b-c9ab-4aba-bd8d-4036d912da1d>2007-09-05 10:50:49 +0000
committerpdanner <pdanner@d688527b-c9ab-4aba-bd8d-4036d912da1d>2007-09-05 10:50:49 +0000
commit2006356de97eef6d8ce1f4e03ace9a42079be8d1 (patch)
tree0d418da384681d8e4e5f6c3054a3fb9f68bf8b5d /common/src/main/java/at/gv/egovernment/moa/util/URLDecoder.java
parentf5addfcb66da2761d2d862220766c047de7a1e32 (diff)
downloadmoa-id-spss-2006356de97eef6d8ce1f4e03ace9a42079be8d1.tar.gz
moa-id-spss-2006356de97eef6d8ce1f4e03ace9a42079be8d1.tar.bz2
moa-id-spss-2006356de97eef6d8ce1f4e03ace9a42079be8d1.zip
MOA-SP/SS Release 1.4.1tags/Build-SPSS-1.4.1
git-svn-id: https://joinup.ec.europa.eu/svn/moa-idspss/tags/Build-SPSS-1_4_1@995 d688527b-c9ab-4aba-bd8d-4036d912da1d
Diffstat (limited to 'common/src/main/java/at/gv/egovernment/moa/util/URLDecoder.java')
-rw-r--r--common/src/main/java/at/gv/egovernment/moa/util/URLDecoder.java60
1 files changed, 60 insertions, 0 deletions
diff --git a/common/src/main/java/at/gv/egovernment/moa/util/URLDecoder.java b/common/src/main/java/at/gv/egovernment/moa/util/URLDecoder.java
new file mode 100644
index 000000000..a20820f7e
--- /dev/null
+++ b/common/src/main/java/at/gv/egovernment/moa/util/URLDecoder.java
@@ -0,0 +1,60 @@
+package at.gv.egovernment.moa.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.UnsupportedEncodingException;
+
+/**
+ * Decodes an URL encoded String using a specified character encoding.
+ * Provides a function missing in JDK 1.3.
+ * @author Paul Ivancsics
+ * @version $Id$
+ */
+public class URLDecoder {
+
+ /**
+ * Decodes an <code>application/x-www-form-urlencoded</code> string using a specific encoding scheme.
+ * @param s the string to decode
+ * @param encoding name of character encoding
+ * @return the newly decoded string
+ * @throws UnsupportedEncodingException if the encoding is not supported
+ */
+ public static String decode(String s, String encoding) throws UnsupportedEncodingException {
+ StringReader in = new StringReader(s);
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ for (int b = read(in); b >= 0; b = read(in))
+ bout.write(b);
+ return bout.toString(encoding);
+ }
+ /**
+ * Decodes the next byte from the string reader.
+ * @param in string reader
+ * @return the next byte decoded;
+ * -1 upon end of string, on erroneous data, and on any exception caught
+ * @todo syntax check on string
+ */
+ private static int read(StringReader in) {
+ try {
+ int b = in.read();
+ if (b == '+')
+ return ' ';
+ if (b == '%') {
+ char[] hex = new char[2];
+ if (in.read(hex, 0, 2) >= 0) {
+ String hexString = new String(hex);
+ return Integer.valueOf(hexString, 16).intValue();
+ }
+ else
+ return -1;
+ }
+ return b;
+ }
+ catch (IOException ex) {
+ return -1;
+ }
+ catch (NumberFormatException ex) {
+ return -1;
+ }
+ }
+}