2012-03-15 19 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

uが良く助け –

+0

@BalaswamyためTest1.javaのソースコードを投稿することが、私は私のコードを追加しています。ありがとう – 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(); 
    } 
} 
関連する問題