2012-03-15 5 views
0

J'utilise le décodeur ISO9075 dans mon application. Lorsque je tente de décoder les éléments suivants chaîneStringIndexOutOfBoundsException lors du décodage

ISO9075.decode ("mediaasset_-g9mdob83oozsr5n_xadda")

son donnant l'exception suivante

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) 

Ce qui peut être le problème. Guidez-moi s'il-vous-plaît.

EDIT

Voici mon code

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

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


} 

Merci.

+2

u peut le code source de poster Test1.java pour une meilleure aide –

+0

@Balaswamy, j'ai ajouté mon code. merci – i2ijeya

+1

Cela ressemble à un simple bogue dans le décodeur Alfresco. Comme votre chaîne n'est pas vraiment un échantillon canonique de la norme ISO, je soupçonne une vulnérabilité aux chaînes exotiques. – Guillaume

Répondre

0

Je viens de le tester avec le code here trouvé et n'a pas pu obtenir votre exception.

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(); 
    } 
} 
Questions connexes