2012-03-15 8 views
0

Я использую декодер ISO9075 в своем приложении. Когда я пытаюсь расшифровать следующую строкуStringIndexOutOfBoundsException при декодировании

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) 

Что может быть проблема. Пожалуйста, направляйте меня.

EDIT

Вот мой код

public class Test1 { 
public static void main(String args[]) 
{ 
    String s = "mediaasset_-g9mdob83oozsr5n_xadda"; 

    System.out.println(ISO9075.decode(s)); 
} 


} 

Спасибо.

+2

у может опубликовать исходный код Test1.java для лучшей помощи –

+0

@Balaswamy, я добавил свой код. спасибо – i2ijeya

+1

Похоже на простой баг в декодере для альфреско. Поскольку ваша строка не является канонической выборкой стандарта ISO, я подозреваю, что уязвимость к экзотическим строкам. – Guillaume

ответ

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(); 
    } 
} 
Смежные вопросы