URIEncoderDecoderpublic class URIEncoderDecoder extends Object This class is used to encode a string using the format required by {@code
application/x-www-form-urlencoded} MIME content type. It contains helper
methods used by the URI class, and performs encoding and decoding in a
slightly different way than {@code URLEncoder} and {@code URLDecoder}. |
Fields Summary |
---|
static final String | digits | static final String | encoding |
Methods Summary |
---|
static java.lang.String | decode(java.lang.String s)Decodes the string argument which is assumed to be encoded in the {@code
x-www-form-urlencoded} MIME content type using the UTF-8 encoding scheme.
'%' and two following hex digit characters are converted to the
equivalent byte value. All other characters are passed through
unmodified.
e.g. "A%20B%20C %24%25" -> "A B C $%"
Called from URI.getXYZ() methods
StringBuffer result = new StringBuffer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int i = 0; i < s.length();) {
char c = s.charAt(i);
if (c == '%") {
out.reset();
do {
if (i + 2 >= s.length()) {
throw new IllegalArgumentException(Msg.getString(
"K01fe", i)); //$NON-NLS-1$
}
int d1 = Character.digit(s.charAt(i + 1), 16);
int d2 = Character.digit(s.charAt(i + 2), 16);
if (d1 == -1 || d2 == -1) {
throw new IllegalArgumentException(Msg.getString(
"K01ff", s.substring(i, i + 3), //$NON-NLS-1$
String.valueOf(i)));
}
out.write((byte) ((d1 << 4) + d2));
i += 3;
} while (i < s.length() && s.charAt(i) == '%");
result.append(out.toString(encoding));
continue;
}
result.append(c);
i++;
}
return result.toString();
| static java.lang.String | encodeOthers(java.lang.String s)Other characters, which are Unicode chars that are not US-ASCII, and are
not ISO Control or are not ISO Space chars are not preserved. They are
converted into their hexidecimal value prepended by '%'.
For example: Euro currency symbol -> "%E2%82%AC".
Called from URI.toASCIIString()
StringBuffer buf = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch <= 127) {
buf.append(ch);
} else {
byte[] bytes = new String(new char[] { ch }).getBytes(encoding);
for (int j = 0; j < bytes.length; j++) {
buf.append('%");
buf.append(digits.charAt((bytes[j] & 0xf0) >> 4));
buf.append(digits.charAt(bytes[j] & 0xf));
}
}
}
return buf.toString();
| static java.lang.String | quoteIllegal(java.lang.String s, java.lang.String legal)All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9')
and legal characters are converted into their hexidecimal value prepended
by '%'.
For example: '#' -> %23
Other characters, which are unicode chars that are not US-ASCII, and are
not ISO Control or are not ISO Space chars, are preserved.
Called from {@code URI.quoteComponent()} (for multiple argument
constructors)
StringBuffer buf = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if ((ch >= 'a" && ch <= 'z")
|| (ch >= 'A" && ch <= 'Z")
|| (ch >= '0" && ch <= '9")
|| legal.indexOf(ch) > -1
|| (ch > 127 && !Character.isSpaceChar(ch) && !Character
.isISOControl(ch))) {
buf.append(ch);
} else {
byte[] bytes = new String(new char[] { ch }).getBytes(encoding);
for (int j = 0; j < bytes.length; j++) {
buf.append('%");
buf.append(digits.charAt((bytes[j] & 0xf0) >> 4));
buf.append(digits.charAt(bytes[j] & 0xf));
}
}
}
return buf.toString();
| static void | validate(java.lang.String s, java.lang.String legal)Validate a string by checking if it contains any characters other than:
1. letters ('a'..'z', 'A'..'Z') 2. numbers ('0'..'9') 3. characters in
the legalset parameter 4. others (unicode characters that are not in
US-ASCII set, and are not ISO Control or are not ISO Space characters)
called from {@code URI.Helper.parseURI()} to validate each component
//$NON-NLS-1$
for (int i = 0; i < s.length();) {
char ch = s.charAt(i);
if (ch == '%") {
do {
if (i + 2 >= s.length()) {
throw new URISyntaxException(s, Msg.getString("K0313"), //$NON-NLS-1$
i);
}
int d1 = Character.digit(s.charAt(i + 1), 16);
int d2 = Character.digit(s.charAt(i + 2), 16);
if (d1 == -1 || d2 == -1) {
throw new URISyntaxException(s, Msg.getString("K0314", //$NON-NLS-1$
s.substring(i, i + 3)), i);
}
i += 3;
} while (i < s.length() && s.charAt(i) == '%");
continue;
}
if (!((ch >= 'a" && ch <= 'z") || (ch >= 'A" && ch <= 'Z")
|| (ch >= '0" && ch <= '9") || legal.indexOf(ch) > -1 || (ch > 127
&& !Character.isSpaceChar(ch) && !Character
.isISOControl(ch)))) {
throw new URISyntaxException(s, Msg.getString("K00c1"), i); //$NON-NLS-1$
}
i++;
}
| static void | validateSimple(java.lang.String s, java.lang.String legal)
for (int i = 0; i < s.length();) {
char ch = s.charAt(i);
if (!((ch >= 'a" && ch <= 'z") || (ch >= 'A" && ch <= 'Z")
|| (ch >= '0" && ch <= '9") || legal.indexOf(ch) > -1)) {
throw new URISyntaxException(s, Msg.getString("K00c1"), i); //$NON-NLS-1$
}
i++;
}
|
|