00001 package de.fub.bytecode.classfile;
00002
00003 import de.fub.bytecode.Constants;
00004 import java.io.*;
00005 import java.util.*;
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 public final class Unknown extends Attribute {
00017 private byte[] bytes;
00018 private String name;
00019
00020 private static Hashtable unknown_attributes = new Hashtable();
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 public Unknown(int name_index, int length, byte[] bytes,
00031 ConstantPool constant_pool)
00032 {
00033 super(ATTR_UNKNOWN, name_index, length, constant_pool);
00034 this.bytes = bytes;
00035
00036 name = ((ConstantUtf8)constant_pool.getConstant(name_index,
00037 CONSTANT_Utf8)).getBytes();
00038 unknown_attributes.put(name, this);
00039 }
00040
00041
00042
00043
00044
00045
00046
00047
00048 Unknown(int name_index, int length, DataInputStream file,
00049 ConstantPool constant_pool)
00050 throws IOException
00051 {
00052 this(name_index, length, (byte [])null, constant_pool);
00053
00054 if(length > 0) {
00055 bytes = new byte[length];
00056 file.readFully(bytes);
00057 }
00058 }
00059
00060
00061
00062
00063 public Unknown(Unknown c) {
00064 this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
00065 }
00066
00067
00068
00069
00070
00071
00072
00073 public void accept(Visitor v) {
00074 v.visitUnknown(this);
00075 }
00076
00077
00078
00079 public Attribute copy(ConstantPool constant_pool) {
00080 Unknown c = (Unknown)clone();
00081
00082 if(bytes != null)
00083 c.bytes = (byte[])bytes.clone();
00084
00085 c.constant_pool = constant_pool;
00086 return c;
00087 }
00088
00089
00090
00091
00092
00093
00094 public final void dump(DataOutputStream file) throws IOException
00095 {
00096 super.dump(file);
00097 if(length > 0)
00098 file.write(bytes, 0, length);
00099 }
00100
00101
00102
00103 public final byte[] getBytes() { return bytes; }
00104
00105
00106
00107 public final String getName() { return name; }
00108
00109
00110 static Unknown[] getUnknownAttributes() {
00111 Unknown[] unknowns = new Unknown[unknown_attributes.size()];
00112 Enumeration entries = unknown_attributes.elements();
00113
00114 for(int i=0; entries.hasMoreElements(); i++)
00115 unknowns[i] = (Unknown)entries.nextElement();
00116
00117 unknown_attributes = new Hashtable();
00118 return unknowns;
00119 }
00120
00121
00122
00123 public final void setBytes(byte[] bytes) {
00124 this.bytes = bytes;
00125 }
00126
00127
00128
00129 public final String toString() {
00130 if(length == 0 || bytes == null)
00131 return "(Unknown attribute " + name + ")";
00132
00133 String hex;
00134 if(length > 10) {
00135 byte[] tmp = new byte[10];
00136 System.arraycopy(bytes, 0, tmp, 0, 10);
00137 hex = Utility.toHexString(tmp) + "... (truncated)";
00138 }
00139 else
00140 hex = Utility.toHexString(bytes);
00141
00142 return "(Unknown attribute " + name + ": " + hex + ")";
00143 }
00144 }