2012-03-15 3 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) 

문제가 될 수 있습니다 무엇. 나를 안내 해줘.

편집

여기에 내 코드

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