Added URLDecoder cUnicode support

This commit is contained in:
Ziver Koc 2015-12-11 14:58:30 +01:00
parent bf235694eb
commit 930ad2b2f3
3 changed files with 40 additions and 20 deletions

View file

@ -1,5 +1,9 @@
package zutil.parser;
import zutil.converters.Converter;
import java.io.UnsupportedEncodingException;
/**
* This utility class will decode Strings encoded with % sign's to a normal String
*
@ -10,27 +14,40 @@ public class URLDecoder {
public static String decode(String url){
if(url == null)
return null;
StringBuilder out = new StringBuilder();
for (int i=0; i<url.length(); ++i) {
char c = url.charAt(i);
switch (c){
case '+':
out.append(' ');
break;
case '%':
if (i+2 < url.length()) {
char ascii = (char)Integer.parseInt("" + url.charAt(i + 1) + url.charAt(i + 2), 16);
out.append(ascii);
i += 2;
}
else
try {
StringBuilder out = new StringBuilder();
byte[] buffer = null;
for (int i=0; i<url.length(); ++i) {
char c = url.charAt(i);
switch (c){
case '+':
out.append(' ');
break;
case '%':
if (i+2 < url.length()) {
if(buffer == null)
buffer = new byte[url.length()];
int bufferPos = 0;
while(i<url.length() && url.charAt(i) == '%') {
buffer[bufferPos++] = Converter.hexToByte(url.charAt(i + 1), url.charAt(i + 2));
i += 3;
}
--i; // Go back one step as i will be incremented in the main for loop
out.append(new String(buffer, 0, bufferPos, "UTF-8"));
}
else
out.append(c);
break;
default:
out.append(c);
break;
default:
out.append(c);
break;
break;
}
}
return out.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace(); // Should never happen
}
return out.toString();
return null;
}
}