Created
February 19, 2013 09:04
-
-
Save don9z/4984212 to your computer and use it in GitHub Desktop.
stringByAddingPercentEscapesUsingEncoding for Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String stringByAddingPercentEscapesUsingEncoding(String input, String encoding) | |
throws UnsupportedEncodingException { | |
byte[] inputBytes = input.getBytes(encoding); | |
StringBuilder stringBuilder = new StringBuilder(inputBytes.length); | |
for(int i = 0; i < inputBytes.length; ++i) { | |
int charByte = inputBytes[i] < 0 ? inputBytes[i] + 256 : inputBytes[i]; | |
if( charByte <= 0x20 || charByte >= 0x7F || | |
(charByte == 0x22 || charByte == 0x25 || charByte == 0x3C || | |
charByte == 0x3E || charByte == 0x20 || charByte == 0x5B || | |
charByte == 0x5C || charByte == 0x5D || charByte == 0x5E || | |
charByte == 0x60 || charByte == 0x7b || charByte == 0x7c || | |
charByte == 0x7d)) { | |
stringBuilder.append(String.format("%%%02X", charByte)); | |
} | |
else { | |
stringBuilder.append((char)charByte); | |
} | |
} | |
return stringBuilder.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment