1 19 20 25 26 27 package soot.coffi; 28 import soot.*; 29 30 import java.io.*; 31 import java.util.Enumeration ; 32 33 38 public class CONSTANT_Utf8_info extends cp_info { 39 private int sHashCode; 41 private String s; 43 44 private byte bytes[]; 45 46 public CONSTANT_Utf8_info(DataInputStream d) throws IOException { 47 int len; 48 len = d.readUnsignedShort(); 49 bytes = new byte[len+2]; 50 bytes[0] = (byte)(len>>8); 51 bytes[1] = (byte)(len & 0xff); 52 if (len>0) { 53 int j; 54 for (j=0; j<len;j++) 55 bytes[j+2] = (byte)d.readUnsignedByte(); 56 } 57 } 58 59 public void writeBytes(DataOutputStream dd) throws IOException { 60 int len; 61 len = bytes.length; 62 dd.writeShort(len-2); 63 dd.write(bytes,2,len-2); 64 } 65 66 public int length() { 67 return (((((int)(bytes[0]))&0xff)<<8) + (((int)(bytes[1]))&0xff)); 68 } 69 73 public int size() { return length()+3; } 74 77 public String convert() { 78 if (s==null) { 79 try { 80 ByteArrayInputStream bs = new ByteArrayInputStream(bytes); 81 DataInputStream d = new DataInputStream(bs); 82 String buf = d.readUTF(); 83 sHashCode = buf.hashCode(); 84 return buf; 85 } catch(IOException e) { 86 return "!!IOException!!"; 87 } 88 } 89 return s; 90 } 91 95 public void fixConversion(String rep) { 96 if (sHashCode != rep.hashCode()) 97 throw new RuntimeException ("bad use of fixConversion!"); 98 99 if (s == null) { 100 s = rep; 101 } 102 } 103 107 public boolean equals(CONSTANT_Utf8_info cu) { 108 int i,j; 109 j = bytes.length; 110 if (j!=cu.bytes.length) return false; 111 for (i=0; i<j; i++) { 112 if (bytes[i]!=cu.bytes[i]) return false; 113 } 114 return true; 115 } 116 126 public int compareTo(cp_info constant_pool[],cp_info cp,cp_info cp_constant_pool[]) { 127 return compareTo(cp); 128 } 129 138 public int compareTo(cp_info cp) { 139 if (tag!=cp.tag) return tag-cp.tag; 140 CONSTANT_Utf8_info cu = (CONSTANT_Utf8_info)cp; 141 G.v().coffi_CONSTANT_Utf8_info_e1.reset(bytes); 142 G.v().coffi_CONSTANT_Utf8_info_e2.reset(cu.bytes); 143 for (;G.v().coffi_CONSTANT_Utf8_info_e1.hasMoreElements() && G.v().coffi_CONSTANT_Utf8_info_e2.hasMoreElements();) { 144 G.v().coffi_CONSTANT_Utf8_info_e1.nextElement(); 145 G.v().coffi_CONSTANT_Utf8_info_e2.nextElement(); 146 if (G.v().coffi_CONSTANT_Utf8_info_e1.c<G.v().coffi_CONSTANT_Utf8_info_e2.c) return -1; 147 if (G.v().coffi_CONSTANT_Utf8_info_e2.c<G.v().coffi_CONSTANT_Utf8_info_e1.c) return 1; 148 } 149 if (G.v().coffi_CONSTANT_Utf8_info_e1.hasMoreElements()) return -1; 150 if (G.v().coffi_CONSTANT_Utf8_info_e2.hasMoreElements()) return 1; 151 return 0; 152 } 153 158 public static byte[] toUtf8(String s) { 159 try { 160 ByteArrayOutputStream bs = new ByteArrayOutputStream(s.length()); 161 DataOutputStream d = new DataOutputStream(bs); 162 d.writeUTF(s); 163 return bs.toByteArray(); 164 } catch(IOException e) { 165 G.v().out.println("Some sort of IO exception in toUtf8 with " + s); 166 } 167 return null; 168 } 169 174 public String toString(cp_info constant_pool[]) { 175 return convert(); 176 } 177 181 public String typeName() { return "utf8"; } 182 } 183 | Popular Tags |