1 6 9 27 28 33 34 package jas; 35 36 import java.util.*; 37 import java.io.*; 38 39 40 67 68 69 class CodeAttributeDecoder 70 { 71 public static byte[] decode(String attr, Hashtable labelToPc) 72 { 73 List attributeHunks = new LinkedList(); 74 int attributeSize = 0; 75 int tableSize = 0; 76 77 StringTokenizer st = new StringTokenizer(attr, "%", true); 78 boolean isLabel = false; 79 80 byte[] pcArray; 81 while(st.hasMoreTokens()) { 82 String token = st.nextToken(); 83 if( token.equals( "%" ) ) { 84 isLabel = !isLabel; 85 continue; 86 } 87 if(isLabel) { 88 Integer pc = (Integer ) labelToPc.get(token); 89 90 if(pc == null) 91 throw new RuntimeException ("PC is null, the token is "+token); 92 93 int pcvalue = pc.intValue(); 94 if(pcvalue > 65535) 95 throw new RuntimeException ("PC great than 65535, the token is "+token+" : " +pcvalue); 96 97 pcArray = new byte[2]; 98 99 pcArray[1] = (byte)(pcvalue&0x0FF); 100 101 pcArray[0] = (byte)((pcvalue>>8)&0x0FF); 102 103 attributeHunks.add(pcArray); 104 attributeSize += 2; 105 106 tableSize++; 107 108 } else { 109 110 byte[] hunk = Base64.decode(token.toCharArray()); 111 attributeSize += hunk.length; 112 113 attributeHunks.add(hunk); 114 } 115 } 116 117 attributeSize += 2; 118 byte[] attributeValue = new byte[attributeSize]; 119 attributeValue[0] = (byte)((tableSize>>8)&0x0FF); 120 attributeValue[1] = (byte)(tableSize&0x0FF); 121 122 int index = 2; 123 124 Iterator it = attributeHunks.iterator(); 125 while(it.hasNext()) { 126 byte[] hunk = (byte[]) it.next(); 127 for(int i = 0; i < hunk.length; i++) { 128 attributeValue[index++] = hunk[i]; 129 } 130 } 131 132 if(index != (attributeSize)) 133 throw new RuntimeException ("Index does not euqal to attrubute size :"+index+" -- "+attributeSize); 134 135 return attributeValue; 136 } 137 } 138 | Popular Tags |