Here you can find the source of encode(String str, boolean fullUri)
private static String encode(String str, boolean fullUri) throws URISyntaxException
//package com.java2s; import java.net.URISyntaxException; public class Main { private static final String URI_DECODE_RESERVED = ";/?:@&=+$,#"; /**/* w w w. ja va 2s . c o m*/ * Code taken from rhino-1.7R1/src/org/mozilla/javascript/NativeGlobal.java * and slightly adapted. It is released under the MPL 1.1 and GPL 2.0. */ private static String encode(String str, boolean fullUri) throws URISyntaxException { byte[] utf8buf = null; StringBuilder sb = null; for (int k = 0, length = str.length(); k != length; ++k) { char c = str.charAt(k); if (encodeUnescaped(c, fullUri)) { if (sb != null) { sb.append(c); } } else { if (sb == null) { sb = new StringBuilder(length + 3); sb.append(str); sb.setLength(k); utf8buf = new byte[6]; } if (0xDC00 <= c && c <= 0xDFFF) { throw new URISyntaxException(str, c + " outside of valid range"); } int value; if (c < 0xD800 || 0xDBFF < c) { value = c; } else { k++; if (k == length) { throw new URISyntaxException(str, "out of chars"); } char c2 = str.charAt(k); if (!(0xDC00 <= c2 && c2 <= 0xDFFF)) { throw new URISyntaxException(str, "outside of valid range"); } value = ((c - 0xD800) << 10) + (c2 - 0xDC00) + 0x10000; } int L = oneUcs4ToUtf8Char(utf8buf, value); assert utf8buf != null; for (int j = 0; j < L; j++) { int d = 0xff & utf8buf[j]; sb.append('%'); sb.append(toHexChar(d >>> 4)); sb.append(toHexChar(d & 0xf)); } } } return (sb == null) ? str : sb.toString(); } private static boolean encodeUnescaped(char c, boolean fullUri) { if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9')) { return true; } if ("-_.!~*'()".indexOf(c) >= 0) return true; if (fullUri) { return URI_DECODE_RESERVED.indexOf(c) >= 0; } return false; } private static int oneUcs4ToUtf8Char(byte[] utf8Buffer, int ucs4Char) { int utf8Length = 1; //JS_ASSERT(ucs4Char <= 0x7FFFFFFF); if ((ucs4Char & ~0x7F) == 0) utf8Buffer[0] = (byte) ucs4Char; else { int i; int a = ucs4Char >>> 11; utf8Length = 2; while (a != 0) { a >>>= 5; utf8Length++; } i = utf8Length; while (--i > 0) { utf8Buffer[i] = (byte) ((ucs4Char & 0x3F) | 0x80); ucs4Char >>>= 6; } utf8Buffer[0] = (byte) (0x100 - (1 << (8 - utf8Length)) + ucs4Char); } return utf8Length; } private static char toHexChar(int i) { if (i >> 4 != 0) throw new RuntimeException(); return (char) ((i < 10) ? i + '0' : i - 10 + 'a'); } }