KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jbet > GenericAttribute


1 /*
2  * JBET - Java Binary Enhancement Tool
3  * Copyright (c) 2003 Networks Associates Technology, Inc.
4  *
5  * This software was developed under DARPA/SPAWAR contract
6  * N66001-00-C-8602 "SPMA" as part of the
7  * DARPA OASIS research program.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */

30
31 /**
32  *
33  * @author Larry D'Anna
34  * @version 0.1
35  * @since JDK 1.1.8
36  *
37  * This is a helper class to assist with reading and writeing
38  * attributes. When attributes are read a hint list is kept so that
39  * they can be written back in the same order, however the hints are
40  * not the authoratative source for information stored in attriubtes.
41  * If a information stored elsewhere disagrees with the hints they
42  * are ignored. classes such as MethodInfo call permute() to
43  * interpret these hints
44  */

45
46
47 package jbet;
48 import java.io.*;
49 import java.util.*;
50
51 class GenericAttribute implements AttributeWriter {
52     String JavaDoc name; // The attribute's name
53
int nameIndex; // The index of the attribute name
54
byte [] data; // Internal data buffer
55
AttributeWriter writer; // Writer for the attribute
56

57     /**
58      * Called from AttributeWriter.
59      * @return the current size of data buffer.
60      */

61     public int size() {
62     return data.length;
63     }
64
65     /**
66      * Called from AttributeWriter to write out the data buffer.
67      * @param dataOut stream to write on.
68      */

69     public void write (DataOutputStream dataOut)
70     throws IOException, RuntimeException JavaDoc {
71     dataOut.write (data, 0, data.length);
72     }
73
74     /*
75      * Write the entire attribute record:
76      * first the name index, then the size of the data, then the data.
77      * @param dataOut stream to write on.
78      */

79     void writeAll (DataOutputStream dataOut) throws IOException {
80     dataOut.writeShort( nameIndex );
81     dataOut.writeInt( writer.size() );
82     writer.write(dataOut);
83     }
84
85     /** Constructor.
86      * @param _name attribute name
87      * @param _nameIndex index of name in constant pool
88      * @param length size of internal data buffer.
89      * @param dataIn Stream to read into data buffer.
90      */

91     public GenericAttribute (String JavaDoc _name, int _nameIndex,
92                  int length, DataInputStream dataIn)
93     throws IOException {
94     name = _name;
95     nameIndex = _nameIndex;
96     data = new byte [length];
97     dataIn.readFully (data, 0, length);
98     writer = this;
99     }
100
101     /** Reset the data buffer.
102      * @return a pointer to this.
103      */

104     public GenericAttribute nodata() {
105     data = new byte [0];
106     return this;
107     }
108
109     /** Return a piped DataOutputStream.
110      * Usage: call getStream(), write to it, and then call read()
111      * @return an output stream.
112      */

113     PipedInputStream in = null;
114     DataOutputStream getStream() throws IOException {
115     in = new PipedInputStream();
116     PipedOutputStream pos = new PipedOutputStream(in);
117     DataOutputStream dataOut = new DataOutputStream(pos);
118     return dataOut;
119     }
120
121     /** Read N bytes into data.
122      * @param length number of bytes to read.
123      */

124     public void read (int length) throws IOException {
125     read (length, in);
126     in = null;
127     }
128
129     /** Read N bytes into data.
130      * @param length number of bytes to read.
131      * @param in stream to read.
132      */

133     public void read (int length, InputStream in)
134     throws IOException, RuntimeException JavaDoc {
135
136     data = new byte [length];
137     for (int off = 0; off < length; ) {
138         int ret = in.read (data, off, length-off);
139         if (ret == -1)
140         throw new RuntimeException JavaDoc ("not enough to read");
141         off += ret;
142     }
143     }
144
145     /** Constructor start with empty data buffer.
146      * @param _name attribute name
147      * @param _nameIndex name index
148      */

149     public GenericAttribute (String JavaDoc _name, int _nameIndex) {
150     name = _name;
151     nameIndex = _nameIndex;
152     data = null;
153     writer = this;
154     }
155
156     /** Constructor given a ConstantPool.
157      * @param _name
158      * @param constantPool a constant pool
159      */

160     public GenericAttribute (String JavaDoc _name, ConstantPool constantPool) {
161     name = _name;
162     nameIndex = constantPool.internUtf8( name );
163     data = null;
164     writer = this;
165     }
166
167     /**
168      * This function will take a vector of attributes to write out and
169      * permute them to match a hint vector. Things in stuff that are not
170      * accounted for in hints are put at the end. Things that are in
171      * hints but not stuff are dropped.
172      *
173      * @param hints input vector of GenericAttributes
174      * @param stuff input vector of GenericAttributes
175      * @param out output vector of GenericAttributes
176      */

177     static void permute (Vector hints, Vector stuff, Vector out) {
178     int nstuff = stuff.size();
179     int nhints = hints.size();
180
181     BitSet bs = new BitSet( nstuff );
182     for (int i = 0; i < nstuff; i++) //not sure this is needed
183
bs.clear(i);
184
185     for (int i = 0; i < nhints; i++) {
186         String JavaDoc hname = ((GenericAttribute)hints.elementAt(i)).name;
187         for (int j = 0; j < nstuff; j++)
188         if (!bs.get(j) &&
189             ((GenericAttribute)stuff.elementAt(j)).name.equals(hname)) {
190             out.addElement( stuff.elementAt(j) );
191             bs.set(j);
192         }
193
194     }
195      
196     for (int i = 0; i < nstuff; i++)
197         if (!bs.get(i)) {
198         out.addElement( stuff.elementAt(i) );
199         }
200
201     }
202
203 }
204
Popular Tags