/Users/lyon/j4p/src/classUtils/dumper/AttributeInfo.java

1    /* 
2     
3     * @(#)AttributeInfo.java   1.4 95/08/16 Chuck McManis 
4     
5     * 
6     
7     * Copyright (c) 1996 Chuck McManis, All Rights Reserved. 
8     
9     * 
10    
11    * Permission to use, copy, modify, and distribute this software 
12    
13    * and its documentation for NON-COMMERCIAL purposes and without 
14    
15    * fee is hereby granted provided that this copyright notice 
16    
17    * appears in all copies. 
18    
19    * 
20    
21    * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY 
22    
23    * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
24    
25    * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
26    
27    * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS SHALL NOT BE 
28    
29    * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, 
30    
31    * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. 
32    
33    */ 
34    
35    
36   package classUtils.dumper; 
37    
38    
39   import java.io.DataInputStream; 
40   import java.io.DataOutputStream; 
41   import java.io.IOException; 
42    
43    
44   /** 
45    
46    * This class defines the generic Attribute type for Java class files. 
47    
48    * It is a little bit smart in that for some Attributes it can display 
49    
50    * them intelligently if it also has access to the constant pool of the 
51    
52    * current class. 
53    
54    * 
55    
56    * @version     1.4, 16 Aug 1995 
57    
58    * @author  Chuck McManis 
59    
60    * @see     ClassFile 
61    
62    */ 
63    
64   public class AttributeInfo { 
65    
66       private ConstantPoolInfo name;  // attribute name 
67    
68       private byte data[]; // attribute's contents 
69    
70    
71       public AttributeInfo(ConstantPoolInfo newName, byte newData[]) { 
72    
73           setName(getName()); 
74    
75           setData(newData); 
76    
77       } 
78    
79    
80       public AttributeInfo() { 
81    
82       } 
83    
84    
85       public boolean read(DataInputStream di, ConstantPoolInfo pool[]) 
86    
87               throws IOException { 
88    
89           int len; 
90    
91    
92           setName(pool[di.readShort()]); 
93    
94           len = di.readInt(); 
95    
96           setData(new byte[len]); 
97    
98           len = di.read(getData()); 
99    
100          if (len != getData().length) 
101   
102              return (false); 
103   
104          return (true); 
105   
106      } 
107   
108   
109      public void write(DataOutputStream dos, ConstantPoolInfo pool[]) 
110   
111              throws IOException, Exception { 
112   
113          dos.writeShort(ConstantPoolInfo.indexOf(getName(), pool)); 
114   
115          dos.writeInt(getData().length); 
116   
117          dos.write(getData(), 0, getData().length); 
118   
119      } 
120   
121   
122      short indexFromBytes(byte a[]) { 
123   
124          return (short) (((a[0] << 8) & (0xff << 8)) | 
125   
126                  ((a[1] << 0) & (0xff << 0))); 
127   
128      } 
129   
130   
131      public String toString(ConstantPoolInfo pool[]) { 
132   
133          StringBuffer x = new StringBuffer(); 
134   
135          String type = getName().toString(); 
136   
137          ConstantPoolInfo item; 
138   
139   
140          if (type.compareTo("ConstantValue") == 0) { 
141   
142              item = pool[indexFromBytes(getData())]; 
143   
144              return (item.toString()); 
145   
146          } else if (type.compareTo("SourceFile") == 0) { 
147   
148              item = pool[indexFromBytes(getData())]; 
149   
150              return (item.toString()); 
151   
152          } else { 
153   
154              x.append(type + "<" + getData().length + " bytes>"); 
155   
156          } 
157   
158          return (x.toString()); 
159   
160      } 
161   
162   
163      public String toBoolean(ConstantPoolInfo pool[]) { 
164   
165          ConstantPoolInfo item = pool[indexFromBytes(getData())]; 
166   
167   
168          if (item.intValue == 0) 
169   
170              return ("= false"); 
171   
172          return ("= true"); 
173   
174      } 
175   
176   
177      public String toString() { 
178   
179          return (getName().toString() + " <" + getData().length + " bytes>"); 
180   
181      } 
182   
183      ConstantPoolInfo getName() { 
184          return name; 
185      } 
186   
187      void setName(ConstantPoolInfo name) { 
188          this.name = name; 
189      } 
190   
191      byte[] getData() { 
192          return data; 
193      } 
194   
195      void setData(byte[] data) { 
196          this.data = data; 
197      } 
198   
199  } 
200   
201