optimize URL encoder

This commit is contained in:
zzz
2010-01-02 02:40:12 +00:00
parent 72db6d1a08
commit afe8394658

View File

@ -370,8 +370,13 @@ public class TrackerClient extends I2PAppThread
} }
/** /**
* Very lazy byte[] to URL encoder. Just encodes everything, even * Very lazy byte[] to URL encoder. Just encodes almost everything, even
* "normal" chars. * some "normal" chars.
* By not encoding about 1/4 of the chars, we make random data like hashes about 16% smaller.
*
* RFC1738: 0-9a-zA-Z$-_.+!*'(),
* Us: 0-9a-zA-Z
*
*/ */
public static String urlencode(byte[] bs) public static String urlencode(byte[] bs)
{ {
@ -379,11 +384,17 @@ public class TrackerClient extends I2PAppThread
for (int i = 0; i < bs.length; i++) for (int i = 0; i < bs.length; i++)
{ {
int c = bs[i] & 0xFF; int c = bs[i] & 0xFF;
if ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z')) {
sb.append((char)c);
} else {
sb.append('%'); sb.append('%');
if (c < 16) if (c < 16)
sb.append('0'); sb.append('0');
sb.append(Integer.toHexString(c)); sb.append(Integer.toHexString(c));
} }
}
return sb.toString(); return sb.toString();
} }