diff options
author | kstranacher <kstranacher@d688527b-c9ab-4aba-bd8d-4036d912da1d> | 2009-08-13 11:43:41 +0000 |
---|---|---|
committer | kstranacher <kstranacher@d688527b-c9ab-4aba-bd8d-4036d912da1d> | 2009-08-13 11:43:41 +0000 |
commit | a5b9107deaed46f7f1bb9e3e29c2ff627ea367cb (patch) | |
tree | 38f828de45559bbba3f8741d4828bb69ae7a5db8 /common/src/main/java/at/gv/egovernment/moa/util/URLDecoder.java | |
parent | 79d13d2a1f058b3962f3d6cd9042944f46614cda (diff) | |
parent | 6e9eb88d441cd0a75bb54858e47f36d01a14cfc8 (diff) | |
download | moa-id-spss-a5b9107deaed46f7f1bb9e3e29c2ff627ea367cb.tar.gz moa-id-spss-a5b9107deaed46f7f1bb9e3e29c2ff627ea367cb.tar.bz2 moa-id-spss-a5b9107deaed46f7f1bb9e3e29c2ff627ea367cb.zip |
git-svn-id: https://joinup.ec.europa.eu/svn/moa-idspss/tags/1.4.5@1131 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.java | 75 |
1 files changed, 75 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..2fc721db9 --- /dev/null +++ b/common/src/main/java/at/gv/egovernment/moa/util/URLDecoder.java @@ -0,0 +1,75 @@ +/* +* Copyright 2003 Federal Chancellery Austria +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +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; + } + } +} |