Added URLDecoder cUnicode support
This commit is contained in:
parent
bf235694eb
commit
930ad2b2f3
3 changed files with 40 additions and 20 deletions
BIN
Zutil.jar
BIN
Zutil.jar
Binary file not shown.
|
|
@ -1,5 +1,9 @@
|
||||||
package zutil.parser;
|
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
|
* This utility class will decode Strings encoded with % sign's to a normal String
|
||||||
*
|
*
|
||||||
|
|
@ -10,7 +14,10 @@ public class URLDecoder {
|
||||||
public static String decode(String url){
|
public static String decode(String url){
|
||||||
if(url == null)
|
if(url == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
try {
|
||||||
StringBuilder out = new StringBuilder();
|
StringBuilder out = new StringBuilder();
|
||||||
|
byte[] buffer = null;
|
||||||
for (int i=0; i<url.length(); ++i) {
|
for (int i=0; i<url.length(); ++i) {
|
||||||
char c = url.charAt(i);
|
char c = url.charAt(i);
|
||||||
switch (c){
|
switch (c){
|
||||||
|
|
@ -19,9 +26,15 @@ public class URLDecoder {
|
||||||
break;
|
break;
|
||||||
case '%':
|
case '%':
|
||||||
if (i+2 < url.length()) {
|
if (i+2 < url.length()) {
|
||||||
char ascii = (char)Integer.parseInt("" + url.charAt(i + 1) + url.charAt(i + 2), 16);
|
if(buffer == null)
|
||||||
out.append(ascii);
|
buffer = new byte[url.length()];
|
||||||
i += 2;
|
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
|
else
|
||||||
out.append(c);
|
out.append(c);
|
||||||
|
|
@ -32,5 +45,9 @@ public class URLDecoder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out.toString();
|
return out.toString();
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
e.printStackTrace(); // Should never happen
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ package zutil.test;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import zutil.parser.URLDecoder;
|
import zutil.parser.URLDecoder;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -29,7 +31,8 @@ public class URLDecoderTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void percentMultibyteTest(){
|
public void percentMultibyteTest() throws UnsupportedEncodingException {
|
||||||
|
assertEquals("Ängen", java.net.URLDecoder.decode("%C3%84ngen", "UTF-8"));
|
||||||
assertEquals("Ängen", URLDecoder.decode("%C3%84ngen"));
|
assertEquals("Ängen", URLDecoder.decode("%C3%84ngen"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue