public static int percentEncodeString(String s, StringBuffer sb) {
        if (s == null) {
            return 0;
        }
        
        int nbrchars = 0;
        for(int i=0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (isUriUnreserved(c)) {
                sb.append(c);
                nbrchars += 1;
            } else {
                // FIXME: I think proper UTF-8 differs in handling the
                // char \u0000. We should check that, but it's
                // unlikely that it will appear
                if (c >= 0x0001 && c <= 0x007f) {
                    percentEncode((byte) c, sb);
                    nbrchars += 3;
                }
                else if (c <= 0x07ff) { // >= \u0080 or == \u0000
                    percentEncode((byte)(0xc0 | (0x1f & (c >> 6))), sb);
                    percentEncode((byte)(0x80 | (0x3f & c)), sb);
                    nbrchars += 6;
                }
                else {
                    percentEncode((byte)(0xe0 | (0x0f & (c >> 12))), sb);
                    percentEncode((byte)(0x80 | (0x3f & (c >>  6))), sb);
                    percentEncode((byte)(0x80 | (0x3f & c)), sb);
                    nbrchars += 9;
                }
            }
        }
        
        return nbrchars;
    }