1 18 package org.apache.tools.ant.taskdefs.optional.jlink; 19 20 import java.io.DataInput ; 21 import java.io.DataInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 25 31 class ConstantPool { 32 static final 34 byte UTF8 = 1, UNUSED = 2, INTEGER = 3, FLOAT = 4, LONG = 5, DOUBLE = 6, 35 CLASS = 7, STRING = 8, FIELDREF = 9, METHODREF = 10, 36 INTERFACEMETHODREF = 11, NAMEANDTYPE = 12; 37 38 byte[] types; 39 40 Object [] values; 41 43 48 ConstantPool(DataInput data) throws IOException { 49 super(); 50 51 int count = data.readUnsignedShort(); 52 types = new byte [ count ]; 53 values = new Object [ count ]; 54 for (int i = 1; i < count; i++) { 56 byte type = data.readByte(); 57 types[i] = type; 58 switch (type) { 59 case UTF8 : 60 values[i] = data.readUTF(); 61 break; 62 63 case UNUSED : 64 break; 65 66 case INTEGER : 67 values[i] = new Integer (data.readInt()); 68 break; 69 70 case FLOAT : 71 values[i] = new Float (data.readFloat()); 72 break; 73 74 case LONG : 75 values[i] = new Long (data.readLong()); 76 ++i; 77 break; 78 79 case DOUBLE : 80 values[i] = new Double (data.readDouble()); 81 ++i; 82 break; 83 84 case CLASS : 85 case STRING : 86 values[i] = new Integer (data.readUnsignedShort()); 87 break; 88 89 case FIELDREF : 90 case METHODREF : 91 case INTERFACEMETHODREF : 92 case NAMEANDTYPE : 93 values[i] = new Integer (data.readInt()); 94 break; 95 default: 96 } 98 } 99 } 100 } 101 102 107 public class ClassNameReader extends Object { 108 109 116 public static String getClassName(InputStream input) throws IOException { 117 DataInputStream data = new DataInputStream (input); 118 int cookie = data.readInt(); 120 if (cookie != 0xCAFEBABE) { 121 return null; 122 } 123 data.readInt(); 124 ConstantPool constants = new ConstantPool(data); 126 Object [] values = constants.values; 127 data.readUnsignedShort(); 129 int classIndex = data.readUnsignedShort(); 130 Integer stringIndex = (Integer ) values[classIndex]; 131 String className = (String ) values[stringIndex.intValue()]; 132 return className; 133 } 134 135 136 } 137 138 139 | Popular Tags |