public static String encodePath(String path,
boolean flag) {
char[] retCC = new char[path.length() * 2 + 16];
int retLen = 0;
char[] pathCC = path.toCharArray();
int n = path.length();
for (int i=0; i< n; i++) {
char c = pathCC[i];
if ((!flag && c == '/') || (flag && c == File.separatorChar))
retCC[retLen++] = '/';
else {
if (c < = 0x007F) {
if (c >= 'a' && c < = 'z' ||
c >= 'A' && c < = 'Z' ||
c >= '0' && c < = '9') {
retCC[retLen++] = c;
} else
if (encodedInPath.get(c))
retLen = escape(retCC, c, retLen);
else
retCC[retLen++] = c;
} else if (c > 0x07FF) {
retLen = escape(retCC, (char)(0xE0 | ((c > > 12) & 0x0F)), retLen);
retLen = escape(retCC, (char)(0x80 | ((c > > 6) & 0x3F)), retLen);
retLen = escape(retCC, (char)(0x80 | ((c > > 0) & 0x3F)), retLen);
} else {
retLen = escape(retCC, (char)(0xC0 | ((c > > 6) & 0x1F)), retLen);
retLen = escape(retCC, (char)(0x80 | ((c > > 0) & 0x3F)), retLen);
}
}
//worst case scenario for character [0x7ff-] every single
//character will be encoded into 9 characters.
if (retLen + 9 > retCC.length) {
int newLen = retCC.length * 2 + 16;
if (newLen < 0) {
newLen = Integer.MAX_VALUE;
}
char[] buf = new char[newLen];
System.arraycopy(retCC, 0, buf, 0, retLen);
retCC = buf;
}
}
return new String(retCC, 0, retLen);
}
|