SPR-7107 - RestTemplate/UriTemplate/UriUtils improperly encoding UTF-8

This commit is contained in:
Arjen Poutsma
2010-04-19 08:28:10 +00:00
parent e60389283d
commit b25462ddf9
4 changed files with 28 additions and 16 deletions

View File

@@ -424,22 +424,33 @@ public abstract class UriUtils {
throws UnsupportedEncodingException {
Assert.notNull(source, "'source' must not be null");
Assert.hasLength(encoding, "'encoding' must not be empty");
ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length() * 2);
for (int i = 0; i < source.length(); i++) {
int ch = source.charAt(i);
if (notEncoded.get(ch)) {
bos.write(ch);
byte[] bytes = encode(source.getBytes(encoding), notEncoded);
return new String(bytes, "US-ASCII");
}
private static byte[] encode(byte[] source, BitSet notEncoded) {
Assert.notNull(source, "'source' must not be null");
ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length * 2);
for (int i = 0; i < source.length; i++) {
int b = source[i];
if (b < 0) {
b += 256;
}
if (notEncoded.get(b)) {
bos.write(b);
}
else {
bos.write('%');
char hex1 = Character.toUpperCase(Character.forDigit((ch >> 4) & 0xF, 16));
char hex2 = Character.toUpperCase(Character.forDigit(ch & 0xF, 16));
char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
bos.write(hex1);
bos.write(hex2);
}
}
return new String(bos.toByteArray(), encoding);
return bos.toByteArray();
}
/**