1 17 package org.apache.bcel.classfile; 18 19 import java.io.DataInputStream ; 20 import java.io.DataOutputStream ; 21 import java.io.IOException ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import org.apache.bcel.Constants; 26 27 43 public final class Unknown extends Attribute { 44 45 private byte[] bytes; 46 private String name; 47 private static Map unknown_attributes = new HashMap (); 48 49 50 52 static Unknown[] getUnknownAttributes() { 53 Unknown[] unknowns = new Unknown[unknown_attributes.size()]; 54 Iterator entries = unknown_attributes.values().iterator(); 55 for (int i = 0; entries.hasNext(); i++) { 56 unknowns[i] = (Unknown) entries.next(); 57 } 58 unknown_attributes.clear(); 59 return unknowns; 60 } 61 62 63 67 public Unknown(Unknown c) { 68 this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); 69 } 70 71 72 80 public Unknown(int name_index, int length, byte[] bytes, ConstantPool constant_pool) { 81 super(Constants.ATTR_UNKNOWN, name_index, length, constant_pool); 82 this.bytes = bytes; 83 name = ((ConstantUtf8) constant_pool.getConstant(name_index, Constants.CONSTANT_Utf8)) 84 .getBytes(); 85 unknown_attributes.put(name, this); 86 } 87 88 89 97 Unknown(int name_index, int length, DataInputStream file, ConstantPool constant_pool) 98 throws IOException { 99 this(name_index, length, (byte[]) null, constant_pool); 100 if (length > 0) { 101 bytes = new byte[length]; 102 file.readFully(bytes); 103 } 104 } 105 106 107 114 public void accept( Visitor v ) { 115 v.visitUnknown(this); 116 } 117 118 119 125 public final void dump( DataOutputStream file ) throws IOException { 126 super.dump(file); 127 if (length > 0) { 128 file.write(bytes, 0, length); 129 } 130 } 131 132 133 136 public final byte[] getBytes() { 137 return bytes; 138 } 139 140 141 144 public final String getName() { 145 return name; 146 } 147 148 149 152 public final void setBytes( byte[] bytes ) { 153 this.bytes = bytes; 154 } 155 156 157 160 public final String toString() { 161 if (length == 0 || bytes == null) { 162 return "(Unknown attribute " + name + ")"; 163 } 164 String hex; 165 if (length > 10) { 166 byte[] tmp = new byte[10]; 167 System.arraycopy(bytes, 0, tmp, 0, 10); 168 hex = Utility.toHexString(tmp) + "... (truncated)"; 169 } else { 170 hex = Utility.toHexString(bytes); 171 } 172 return "(Unknown attribute " + name + ": " + hex + ")"; 173 } 174 175 176 179 public Attribute copy( ConstantPool _constant_pool ) { 180 Unknown c = (Unknown) clone(); 181 if (bytes != null) { 182 c.bytes = new byte[bytes.length]; 183 System.arraycopy(bytes, 0, c.bytes, 0, bytes.length); 184 } 185 c.constant_pool = _constant_pool; 186 return c; 187 } 188 } 189 | Popular Tags |