我在我的应用程序中使用ISO9075解码器。当我尝试解码以下字符串
时ISO9075.decode( “mediaasset_-g9mdob83oozsr5n_xadda”)
给出以下异常
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 22
at java.lang.String.charAt(Unknown Source)
at org.alfresco.util.ISO9075.matchesEncodedPattern(ISO9075.java:128)
at org.alfresco.util.ISO9075.decode(ISO9075.java:176)
at Test1.main(Test1.java:9)
可能是什么问题。请指导我。
修改
这是我的代码
public class Test1 {
public static void main(String args[])
{
String s = "mediaasset_-g9mdob83oozsr5n_xadda";
System.out.println(ISO9075.decode(s));
}
}
感谢。
答案 0 :(得分:0)
我刚用代码here对其进行了测试,无法获得您的例外。
public static void main(String args[]) {
String s = "mediaasset_-g9mdob83oozsr5n_xadda";
System.out.println(ISO9075.decode(s)); //prints mediaasset_-g9mdob83oozsr5n_xadda
}
public static class ISO9075 {
//I have removed the parts not used by your main()
private static boolean matchesEncodedPattern(String string, int position) {
return (string.length() > position + 6)
&& (string.charAt(position) == '_') && (string.charAt(position + 1) == 'x')
&& isHexChar(string.charAt(position + 2)) && isHexChar(string.charAt(position + 3))
&& isHexChar(string.charAt(position + 4)) && isHexChar(string.charAt(position + 5))
&& (string.charAt(position + 6) == '_');
}
private static boolean isHexChar(char c) {
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
return true;
default:
return false;
}
}
public static String decode(String toDecode) {
if ((toDecode == null) || (toDecode.length() < 7) || (toDecode.indexOf("_x") < 0)) {
return toDecode;
}
StringBuffer decoded = new StringBuffer();
for (int i = 0, l = toDecode.length(); i < l; i++) {
if (matchesEncodedPattern(toDecode, i)) {
decoded.append(((char) Integer.parseInt(toDecode.substring(i + 2, i + 6), 16)));
i += 6;// then one added for the loop to mkae the length of 7
} else {
decoded.append(toDecode.charAt(i));
}
}
return decoded.toString();
}
}