| 1 30 31 45 46 47 package jbet; 48 import java.io.*; 49 import java.util.*; 50 51 class GenericAttribute implements AttributeWriter { 52 String name; int nameIndex; byte [] data; AttributeWriter writer; 57 61 public int size() { 62 return data.length; 63 } 64 65 69 public void write (DataOutputStream dataOut) 70 throws IOException, RuntimeException { 71 dataOut.write (data, 0, data.length); 72 } 73 74 79 void writeAll (DataOutputStream dataOut) throws IOException { 80 dataOut.writeShort( nameIndex ); 81 dataOut.writeInt( writer.size() ); 82 writer.write(dataOut); 83 } 84 85 91 public GenericAttribute (String _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 104 public GenericAttribute nodata() { 105 data = new byte [0]; 106 return this; 107 } 108 109 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 124 public void read (int length) throws IOException { 125 read (length, in); 126 in = null; 127 } 128 129 133 public void read (int length, InputStream in) 134 throws IOException, RuntimeException { 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 ("not enough to read"); 141 off += ret; 142 } 143 } 144 145 149 public GenericAttribute (String _name, int _nameIndex) { 150 name = _name; 151 nameIndex = _nameIndex; 152 data = null; 153 writer = this; 154 } 155 156 160 public GenericAttribute (String _name, ConstantPool constantPool) { 161 name = _name; 162 nameIndex = constantPool.internUtf8( name ); 163 data = null; 164 writer = this; 165 } 166 167 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++) bs.clear(i); 184 185 for (int i = 0; i < nhints; i++) { 186 String 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 |