2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.java.util.jar.pack;
|
|
27 |
|
|
28 |
import java.io.*;
|
|
29 |
import java.util.*;
|
|
30 |
import java.util.jar.*;
|
|
31 |
import com.sun.java.util.jar.pack.Package.Class;
|
|
32 |
import com.sun.java.util.jar.pack.Package.InnerClass;
|
|
33 |
import com.sun.java.util.jar.pack.ConstantPool.*;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Define the structure and ordering of "bands" in a packed file.
|
|
37 |
* @author John Rose
|
|
38 |
*/
|
|
39 |
abstract
|
|
40 |
class BandStructure implements Constants {
|
|
41 |
static final int MAX_EFFORT = 9;
|
|
42 |
static final int MIN_EFFORT = 1;
|
|
43 |
static final int DEFAULT_EFFORT = 5;
|
|
44 |
|
|
45 |
// Inherit options from Pack200:
|
|
46 |
PropMap p200 = Utils.currentPropMap();
|
|
47 |
|
|
48 |
int verbose = p200.getInteger(Utils.DEBUG_VERBOSE);
|
|
49 |
int effort = p200.getInteger(Pack200.Packer.EFFORT);
|
|
50 |
{ if (effort == 0) effort = DEFAULT_EFFORT; }
|
|
51 |
boolean optDumpBands = p200.getBoolean(Utils.COM_PREFIX+"dump.bands");
|
|
52 |
boolean optDebugBands = p200.getBoolean(Utils.COM_PREFIX+"debug.bands");
|
|
53 |
|
|
54 |
// Various heuristic options.
|
|
55 |
boolean optVaryCodings = !p200.getBoolean(Utils.COM_PREFIX+"no.vary.codings");
|
|
56 |
boolean optBigStrings = !p200.getBoolean(Utils.COM_PREFIX+"no.big.strings");
|
|
57 |
|
|
58 |
abstract protected Index getCPIndex(byte tag);
|
|
59 |
|
|
60 |
// Local copy of package version.
|
|
61 |
private int packageMajver = -1;
|
|
62 |
|
|
63 |
/** Call this exactly once, early, to specify the archive major version. */
|
|
64 |
public void initPackageMajver(int packageMajver) throws IOException {
|
|
65 |
assert(packageMajver > 0 && packageMajver < 0x10000);
|
|
66 |
if (this.packageMajver > 0) {
|
|
67 |
throw new IOException(
|
|
68 |
"Package majver is already initialized to " + this.packageMajver+
|
|
69 |
"; new setting is " + packageMajver);
|
|
70 |
}
|
|
71 |
this.packageMajver = packageMajver;
|
|
72 |
adjustToMajver();
|
|
73 |
}
|
|
74 |
public int getPackageMajver() {
|
|
75 |
if (packageMajver < 0) {
|
|
76 |
throw new RuntimeException("Package majver not yet initialized");
|
|
77 |
}
|
|
78 |
return packageMajver;
|
|
79 |
}
|
|
80 |
|
|
81 |
private final boolean isReader = this instanceof PackageReader;
|
|
82 |
protected BandStructure() {
|
|
83 |
}
|
|
84 |
|
|
85 |
final static Coding BYTE1 = Coding.of(1,256);
|
|
86 |
|
|
87 |
final static Coding CHAR3 = Coding.of(3,128);
|
|
88 |
// Note: Tried sharper (3,16) with no post-zip benefit.
|
|
89 |
|
|
90 |
// This is best used with BCI values:
|
|
91 |
final static Coding BCI5 = Coding.of(5,4); // mostly 1-byte offsets
|
|
92 |
final static Coding BRANCH5 = Coding.of(5,4,2); // mostly forward branches
|
|
93 |
|
|
94 |
final static Coding UNSIGNED5 = Coding.of(5,64);
|
|
95 |
final static Coding UDELTA5 = UNSIGNED5.getDeltaCoding();
|
|
96 |
// "sharp" (5,64) zips 0.4% better than "medium" (5,128)
|
|
97 |
// It zips 1.1% better than "flat" (5,192)
|
|
98 |
|
|
99 |
final static Coding SIGNED5 = Coding.of(5,64,1); //sharp
|
|
100 |
final static Coding DELTA5 = SIGNED5.getDeltaCoding();
|
|
101 |
// Note: Tried (5,128,2) and (5,192,2) with no benefit.
|
|
102 |
|
|
103 |
final static Coding MDELTA5 = Coding.of(5,64,2).getDeltaCoding();
|
|
104 |
|
|
105 |
final private static Coding[] basicCodings = {
|
|
106 |
// Table of "Canonical BHSD Codings" from Pack200 spec.
|
|
107 |
null, // _meta_default
|
|
108 |
|
|
109 |
// Fixed-length codings:
|
|
110 |
Coding.of(1,256,0),
|
|
111 |
Coding.of(1,256,1),
|
|
112 |
Coding.of(1,256,0).getDeltaCoding(),
|
|
113 |
Coding.of(1,256,1).getDeltaCoding(),
|
|
114 |
Coding.of(2,256,0),
|
|
115 |
Coding.of(2,256,1),
|
|
116 |
Coding.of(2,256,0).getDeltaCoding(),
|
|
117 |
Coding.of(2,256,1).getDeltaCoding(),
|
|
118 |
Coding.of(3,256,0),
|
|
119 |
Coding.of(3,256,1),
|
|
120 |
Coding.of(3,256,0).getDeltaCoding(),
|
|
121 |
Coding.of(3,256,1).getDeltaCoding(),
|
|
122 |
Coding.of(4,256,0),
|
|
123 |
Coding.of(4,256,1),
|
|
124 |
Coding.of(4,256,0).getDeltaCoding(),
|
|
125 |
Coding.of(4,256,1).getDeltaCoding(),
|
|
126 |
|
|
127 |
// Full-range variable-length codings:
|
|
128 |
Coding.of(5, 4,0),
|
|
129 |
Coding.of(5, 4,1),
|
|
130 |
Coding.of(5, 4,2),
|
|
131 |
Coding.of(5, 16,0),
|
|
132 |
Coding.of(5, 16,1),
|
|
133 |
Coding.of(5, 16,2),
|
|
134 |
Coding.of(5, 32,0),
|
|
135 |
Coding.of(5, 32,1),
|
|
136 |
Coding.of(5, 32,2),
|
|
137 |
Coding.of(5, 64,0),
|
|
138 |
Coding.of(5, 64,1),
|
|
139 |
Coding.of(5, 64,2),
|
|
140 |
Coding.of(5,128,0),
|
|
141 |
Coding.of(5,128,1),
|
|
142 |
Coding.of(5,128,2),
|
|
143 |
|
|
144 |
Coding.of(5, 4,0).getDeltaCoding(),
|
|
145 |
Coding.of(5, 4,1).getDeltaCoding(),
|
|
146 |
Coding.of(5, 4,2).getDeltaCoding(),
|
|
147 |
Coding.of(5, 16,0).getDeltaCoding(),
|
|
148 |
Coding.of(5, 16,1).getDeltaCoding(),
|
|
149 |
Coding.of(5, 16,2).getDeltaCoding(),
|
|
150 |
Coding.of(5, 32,0).getDeltaCoding(),
|
|
151 |
Coding.of(5, 32,1).getDeltaCoding(),
|
|
152 |
Coding.of(5, 32,2).getDeltaCoding(),
|
|
153 |
Coding.of(5, 64,0).getDeltaCoding(),
|
|
154 |
Coding.of(5, 64,1).getDeltaCoding(),
|
|
155 |
Coding.of(5, 64,2).getDeltaCoding(),
|
|
156 |
Coding.of(5,128,0).getDeltaCoding(),
|
|
157 |
Coding.of(5,128,1).getDeltaCoding(),
|
|
158 |
Coding.of(5,128,2).getDeltaCoding(),
|
|
159 |
|
|
160 |
// Variable length subrange codings:
|
|
161 |
Coding.of(2,192,0),
|
|
162 |
Coding.of(2,224,0),
|
|
163 |
Coding.of(2,240,0),
|
|
164 |
Coding.of(2,248,0),
|
|
165 |
Coding.of(2,252,0),
|
|
166 |
|
|
167 |
Coding.of(2, 8,0).getDeltaCoding(),
|
|
168 |
Coding.of(2, 8,1).getDeltaCoding(),
|
|
169 |
Coding.of(2, 16,0).getDeltaCoding(),
|
|
170 |
Coding.of(2, 16,1).getDeltaCoding(),
|
|
171 |
Coding.of(2, 32,0).getDeltaCoding(),
|
|
172 |
Coding.of(2, 32,1).getDeltaCoding(),
|
|
173 |
Coding.of(2, 64,0).getDeltaCoding(),
|
|
174 |
Coding.of(2, 64,1).getDeltaCoding(),
|
|
175 |
Coding.of(2,128,0).getDeltaCoding(),
|
|
176 |
Coding.of(2,128,1).getDeltaCoding(),
|
|
177 |
Coding.of(2,192,0).getDeltaCoding(),
|
|
178 |
Coding.of(2,192,1).getDeltaCoding(),
|
|
179 |
Coding.of(2,224,0).getDeltaCoding(),
|
|
180 |
Coding.of(2,224,1).getDeltaCoding(),
|
|
181 |
Coding.of(2,240,0).getDeltaCoding(),
|
|
182 |
Coding.of(2,240,1).getDeltaCoding(),
|
|
183 |
Coding.of(2,248,0).getDeltaCoding(),
|
|
184 |
Coding.of(2,248,1).getDeltaCoding(),
|
|
185 |
|
|
186 |
Coding.of(3,192,0),
|
|
187 |
Coding.of(3,224,0),
|
|
188 |
Coding.of(3,240,0),
|
|
189 |
Coding.of(3,248,0),
|
|
190 |
Coding.of(3,252,0),
|
|
191 |
|
|
192 |
Coding.of(3, 8,0).getDeltaCoding(),
|
|
193 |
Coding.of(3, 8,1).getDeltaCoding(),
|
|
194 |
Coding.of(3, 16,0).getDeltaCoding(),
|
|
195 |
Coding.of(3, 16,1).getDeltaCoding(),
|
|
196 |
Coding.of(3, 32,0).getDeltaCoding(),
|
|
197 |
Coding.of(3, 32,1).getDeltaCoding(),
|
|
198 |
Coding.of(3, 64,0).getDeltaCoding(),
|
|
199 |
Coding.of(3, 64,1).getDeltaCoding(),
|
|
200 |
Coding.of(3,128,0).getDeltaCoding(),
|
|
201 |
Coding.of(3,128,1).getDeltaCoding(),
|
|
202 |
Coding.of(3,192,0).getDeltaCoding(),
|
|
203 |
Coding.of(3,192,1).getDeltaCoding(),
|
|
204 |
Coding.of(3,224,0).getDeltaCoding(),
|
|
205 |
Coding.of(3,224,1).getDeltaCoding(),
|
|
206 |
Coding.of(3,240,0).getDeltaCoding(),
|
|
207 |
Coding.of(3,240,1).getDeltaCoding(),
|
|
208 |
Coding.of(3,248,0).getDeltaCoding(),
|
|
209 |
Coding.of(3,248,1).getDeltaCoding(),
|
|
210 |
|
|
211 |
Coding.of(4,192,0),
|
|
212 |
Coding.of(4,224,0),
|
|
213 |
Coding.of(4,240,0),
|
|
214 |
Coding.of(4,248,0),
|
|
215 |
Coding.of(4,252,0),
|
|
216 |
|
|
217 |
Coding.of(4, 8,0).getDeltaCoding(),
|
|
218 |
Coding.of(4, 8,1).getDeltaCoding(),
|
|
219 |
Coding.of(4, 16,0).getDeltaCoding(),
|
|
220 |
Coding.of(4, 16,1).getDeltaCoding(),
|
|
221 |
Coding.of(4, 32,0).getDeltaCoding(),
|
|
222 |
Coding.of(4, 32,1).getDeltaCoding(),
|
|
223 |
Coding.of(4, 64,0).getDeltaCoding(),
|
|
224 |
Coding.of(4, 64,1).getDeltaCoding(),
|
|
225 |
Coding.of(4,128,0).getDeltaCoding(),
|
|
226 |
Coding.of(4,128,1).getDeltaCoding(),
|
|
227 |
Coding.of(4,192,0).getDeltaCoding(),
|
|
228 |
Coding.of(4,192,1).getDeltaCoding(),
|
|
229 |
Coding.of(4,224,0).getDeltaCoding(),
|
|
230 |
Coding.of(4,224,1).getDeltaCoding(),
|
|
231 |
Coding.of(4,240,0).getDeltaCoding(),
|
|
232 |
Coding.of(4,240,1).getDeltaCoding(),
|
|
233 |
Coding.of(4,248,0).getDeltaCoding(),
|
|
234 |
Coding.of(4,248,1).getDeltaCoding(),
|
|
235 |
|
|
236 |
null
|
|
237 |
};
|
|
238 |
final private static HashMap basicCodingIndexes;
|
|
239 |
static {
|
|
240 |
assert(basicCodings[_meta_default] == null);
|
|
241 |
assert(basicCodings[_meta_canon_min] != null);
|
|
242 |
assert(basicCodings[_meta_canon_max] != null);
|
|
243 |
HashMap map = new HashMap();
|
|
244 |
for (int i = 0; i < basicCodings.length; i++) {
|
|
245 |
Coding c = basicCodings[i];
|
|
246 |
if (c == null) continue;
|
|
247 |
assert(i >= _meta_canon_min);
|
|
248 |
assert(i <= _meta_canon_max);
|
|
249 |
map.put(c, new Integer(i));
|
|
250 |
}
|
|
251 |
basicCodingIndexes = map;
|
|
252 |
}
|
|
253 |
public static Coding codingForIndex(int i) {
|
|
254 |
return i < basicCodings.length ? basicCodings[i] : null;
|
|
255 |
}
|
|
256 |
public static int indexOf(Coding c) {
|
|
257 |
Integer i = (Integer) basicCodingIndexes.get(c);
|
|
258 |
if (i == null) return 0;
|
|
259 |
return i.intValue();
|
|
260 |
}
|
|
261 |
public static Coding[] getBasicCodings() {
|
|
262 |
return (Coding[]) basicCodings.clone();
|
|
263 |
}
|
|
264 |
|
|
265 |
protected byte[] bandHeaderBytes; // used for input only
|
|
266 |
protected int bandHeaderBytePos; // BHB read pointer, for input only
|
|
267 |
protected int bandHeaderBytePos0; // for debug
|
|
268 |
|
|
269 |
protected CodingMethod getBandHeader(int XB, Coding regularCoding) {
|
|
270 |
CodingMethod[] res = {null};
|
|
271 |
// push back XB onto the band header bytes
|
|
272 |
bandHeaderBytes[--bandHeaderBytePos] = (byte) XB;
|
|
273 |
bandHeaderBytePos0 = bandHeaderBytePos;
|
|
274 |
// scan forward through XB and any additional band header bytes
|
|
275 |
bandHeaderBytePos = parseMetaCoding(bandHeaderBytes,
|
|
276 |
bandHeaderBytePos,
|
|
277 |
regularCoding,
|
|
278 |
res);
|
|
279 |
return res[0];
|
|
280 |
}
|
|
281 |
|
|
282 |
public static int parseMetaCoding(byte[] bytes, int pos, Coding dflt, CodingMethod[] res) {
|
|
283 |
if ((bytes[pos] & 0xFF) == _meta_default) {
|
|
284 |
res[0] = dflt;
|
|
285 |
return pos+1;
|
|
286 |
}
|
|
287 |
int pos2;
|
|
288 |
pos2 = Coding.parseMetaCoding(bytes, pos, dflt, res);
|
|
289 |
if (pos2 > pos) return pos2;
|
|
290 |
pos2 = PopulationCoding.parseMetaCoding(bytes, pos, dflt, res);
|
|
291 |
if (pos2 > pos) return pos2;
|
|
292 |
pos2 = AdaptiveCoding.parseMetaCoding(bytes, pos, dflt, res);
|
|
293 |
if (pos2 > pos) return pos2;
|
|
294 |
throw new RuntimeException("Bad meta-coding op "+(bytes[pos]&0xFF));
|
|
295 |
}
|
|
296 |
|
|
297 |
static final int SHORT_BAND_HEURISTIC = 100;
|
|
298 |
|
|
299 |
public static final int NO_PHASE = 0;
|
|
300 |
|
|
301 |
// package writing phases:
|
|
302 |
public static final int COLLECT_PHASE = 1; // collect data before write
|
|
303 |
public static final int FROZEN_PHASE = 3; // no longer collecting
|
|
304 |
public static final int WRITE_PHASE = 5; // ready to write bytes
|
|
305 |
|
|
306 |
// package reading phases:
|
|
307 |
public static final int EXPECT_PHASE = 2; // gather expected counts
|
|
308 |
public static final int READ_PHASE = 4; // ready to read bytes
|
|
309 |
public static final int DISBURSE_PHASE = 6; // pass out data after read
|
|
310 |
|
|
311 |
public static final int DONE_PHASE = 8; // done writing or reading
|
|
312 |
|
|
313 |
static boolean phaseIsRead(int p) {
|
|
314 |
return (p % 2) == 0;
|
|
315 |
}
|
|
316 |
static int phaseCmp(int p0, int p1) {
|
|
317 |
assert((p0 % 2) == (p1 % 2) || (p0 % 8) == 0 || (p1 % 8) == 0);
|
|
318 |
return p0 - p1;
|
|
319 |
}
|
|
320 |
|
|
321 |
/** The packed file is divided up into a number of segments.
|
|
322 |
* Most segments are typed as ValueBand, strongly-typed sequences
|
|
323 |
* of integer values, all interpreted in a single way.
|
|
324 |
* A few segments are ByteBands, which hetergeneous sequences
|
|
325 |
* of bytes.
|
|
326 |
*
|
|
327 |
* The two phases for writing a packed file are COLLECT and WRITE.
|
|
328 |
* 1. When writing a packed file, each band collects
|
|
329 |
* data in an ad-hoc order.
|
|
330 |
* 2. At the end, each band is assigned a coding scheme,
|
|
331 |
* and then all the bands are written in their global order.
|
|
332 |
*
|
|
333 |
* The three phases for reading a packed file are EXPECT, READ,
|
|
334 |
* and DISBURSE.
|
|
335 |
* 1. For each band, the expected number of integers is determined.
|
|
336 |
* 2. The data is actually read from the file into the band.
|
|
337 |
* 3. The band pays out its values as requested, in an ad hoc order.
|
|
338 |
*
|
|
339 |
* When the last phase of a band is done, it is marked so (DONE).
|
|
340 |
* Clearly, these phases must be properly ordered WRT each other.
|
|
341 |
*/
|
|
342 |
abstract class Band {
|
|
343 |
private int phase = NO_PHASE;
|
|
344 |
private final String name;
|
|
345 |
|
|
346 |
private int valuesExpected;
|
|
347 |
|
|
348 |
protected long outputSize = -1; // cache
|
|
349 |
|
|
350 |
final public Coding regularCoding;
|
|
351 |
|
|
352 |
final public int seqForDebug;
|
|
353 |
public int elementCountForDebug;
|
|
354 |
|
|
355 |
|
|
356 |
protected Band(String name, Coding regularCoding) {
|
|
357 |
this.name = name;
|
|
358 |
this.regularCoding = regularCoding;
|
|
359 |
this.seqForDebug = ++nextSeqForDebug;
|
|
360 |
if (verbose > 2)
|
|
361 |
Utils.log.fine("Band "+seqForDebug+" is "+name);
|
|
362 |
// caller must call init
|
|
363 |
}
|
|
364 |
|
|
365 |
public Band init() {
|
|
366 |
// Cannot due this from the constructor, because constructor
|
|
367 |
// may wish to initialize some subclass variables.
|
|
368 |
// Set initial phase for reading or writing:
|
|
369 |
if (isReader)
|
|
370 |
readyToExpect();
|
|
371 |
else
|
|
372 |
readyToCollect();
|
|
373 |
return this;
|
|
374 |
}
|
|
375 |
|
|
376 |
// common operations
|
|
377 |
boolean isReader() { return isReader; }
|
|
378 |
int phase() { return phase; }
|
|
379 |
String name() { return name; }
|
|
380 |
|
|
381 |
/** Return -1 if data buffer not allocated, else max length. */
|
|
382 |
public abstract int capacity();
|
|
383 |
|
|
384 |
/** Allocate data buffer to specified length. */
|
|
385 |
protected abstract void setCapacity(int cap);
|
|
386 |
|
|
387 |
/** Return current number of values in buffer, which must exist. */
|
|
388 |
public abstract int length();
|
|
389 |
|
|
390 |
protected abstract int valuesRemainingForDebug();
|
|
391 |
|
|
392 |
public final int valuesExpected() {
|
|
393 |
return valuesExpected;
|
|
394 |
}
|
|
395 |
|
|
396 |
/** Write out bytes, encoding the values. */
|
|
397 |
public final void writeTo(OutputStream out) throws IOException {
|
|
398 |
assert(assertReadyToWriteTo(this, out));
|
|
399 |
setPhase(WRITE_PHASE);
|
|
400 |
// subclasses continue by writing their contents to output
|
|
401 |
writeDataTo(out);
|
|
402 |
doneWriting();
|
|
403 |
}
|
|
404 |
|
|
405 |
abstract void chooseBandCodings() throws IOException;
|
|
406 |
|
|
407 |
public final long outputSize() {
|
|
408 |
if (outputSize >= 0) {
|
|
409 |
long size = outputSize;
|
|
410 |
assert(size == computeOutputSize());
|
|
411 |
return size;
|
|
412 |
}
|
|
413 |
return computeOutputSize();
|
|
414 |
}
|
|
415 |
|
|
416 |
protected abstract long computeOutputSize();
|
|
417 |
|
|
418 |
abstract protected void writeDataTo(OutputStream out) throws IOException;
|
|
419 |
|
|
420 |
/** Expect a certain number of values. */
|
|
421 |
void expectLength(int l) {
|
|
422 |
assert(assertPhase(this, EXPECT_PHASE));
|
|
423 |
assert(valuesExpected == 0); // all at once
|
|
424 |
assert(l >= 0);
|
|
425 |
valuesExpected = l;
|
|
426 |
}
|
|
427 |
/** Expect more values. (Multiple calls accumulate.) */
|
|
428 |
void expectMoreLength(int l) {
|
|
429 |
assert(assertPhase(this, EXPECT_PHASE));
|
|
430 |
valuesExpected += l;
|
|
431 |
}
|
|
432 |
|
|
433 |
|
|
434 |
/// Phase change markers.
|
|
435 |
|
|
436 |
private void readyToCollect() { // called implicitly by constructor
|
|
437 |
setCapacity(1);
|
|
438 |
setPhase(COLLECT_PHASE);
|
|
439 |
}
|
|
440 |
protected void doneWriting() {
|
|
441 |
assert(assertPhase(this, WRITE_PHASE));
|
|
442 |
setPhase(DONE_PHASE);
|
|
443 |
}
|
|
444 |
private void readyToExpect() { // called implicitly by constructor
|
|
445 |
setPhase(EXPECT_PHASE);
|
|
446 |
}
|
|
447 |
/** Read in bytes, decoding the values. */
|
|
448 |
public final void readFrom(InputStream in) throws IOException {
|
|
449 |
assert(assertReadyToReadFrom(this, in));
|
|
450 |
setCapacity(valuesExpected());
|
|
451 |
setPhase(READ_PHASE);
|
|
452 |
// subclasses continue by reading their contents from input:
|
|
453 |
readDataFrom(in);
|
|
454 |
readyToDisburse();
|
|
455 |
}
|
|
456 |
abstract protected void readDataFrom(InputStream in) throws IOException;
|
|
457 |
protected void readyToDisburse() {
|
|
458 |
if (verbose > 1) Utils.log.fine("readyToDisburse "+this);
|
|
459 |
setPhase(DISBURSE_PHASE);
|
|
460 |
}
|
|
461 |
public void doneDisbursing() {
|
|
462 |
assert(assertPhase(this, DISBURSE_PHASE));
|
|
463 |
setPhase(DONE_PHASE);
|
|
464 |
}
|
|
465 |
public final void doneWithUnusedBand() {
|
|
466 |
if (isReader) {
|
|
467 |
assert(assertPhase(this, EXPECT_PHASE));
|
|
468 |
assert(valuesExpected() == 0);
|
|
469 |
// Fast forward:
|
|
470 |
setPhase(READ_PHASE);
|
|
471 |
setPhase(DISBURSE_PHASE);
|
|
472 |
setPhase(DONE_PHASE);
|
|
473 |
} else {
|
|
474 |
setPhase(FROZEN_PHASE);
|
|
475 |
}
|
|
476 |
}
|
|
477 |
|
|
478 |
protected void setPhase(int newPhase) {
|
|
479 |
assert(assertPhaseChangeOK(this, phase, newPhase));
|
|
480 |
this.phase = newPhase;
|
|
481 |
}
|
|
482 |
|
|
483 |
protected int lengthForDebug = -1; // DEBUG ONLY
|
|
484 |
public String toString() { // DEBUG ONLY
|
|
485 |
int length = (lengthForDebug != -1 ? lengthForDebug : length());
|
|
486 |
String str = name;
|
|
487 |
if (length != 0)
|
|
488 |
str += "[" + length + "]";
|
|
489 |
if (elementCountForDebug != 0)
|
|
490 |
str += "(" + elementCountForDebug + ")";
|
|
491 |
return str;
|
|
492 |
}
|
|
493 |
}
|
|
494 |
|
|
495 |
class ValueBand extends Band {
|
|
496 |
private int[] values; // must be null in EXPECT phase
|
|
497 |
private int length;
|
|
498 |
private int valuesDisbursed;
|
|
499 |
|
|
500 |
private CodingMethod bandCoding;
|
|
501 |
private byte[] metaCoding;
|
|
502 |
|
|
503 |
protected ValueBand(String name, Coding regularCoding) {
|
|
504 |
super(name, regularCoding);
|
|
505 |
}
|
|
506 |
|
|
507 |
public int capacity() {
|
|
508 |
return values == null ? -1 : values.length;
|
|
509 |
}
|
|
510 |
|
|
511 |
/** Declare predicted or needed capacity. */
|
|
512 |
protected void setCapacity(int cap) {
|
|
513 |
assert(length <= cap);
|
|
514 |
if (cap == -1) { values = null; return; }
|
|
515 |
values = realloc(values, cap);
|
|
516 |
}
|
|
517 |
|
|
518 |
public int length() {
|
|
519 |
return length;
|
|
520 |
}
|
|
521 |
protected int valuesRemainingForDebug() {
|
|
522 |
return length - valuesDisbursed;
|
|
523 |
}
|
|
524 |
protected int valueAtForDebug(int i) {
|
|
525 |
return values[i];
|
|
526 |
}
|
|
527 |
|
|
528 |
void patchValue(int i, int value) {
|
|
529 |
// Only one use for this.
|
|
530 |
assert(this == archive_header_S);
|
|
531 |
assert(i == AH_ARCHIVE_SIZE_HI || i == AH_ARCHIVE_SIZE_LO);
|
|
532 |
assert(i < length); // must have already output a dummy
|
|
533 |
values[i] = value;
|
|
534 |
outputSize = -1; // decache
|
|
535 |
}
|
|
536 |
|
|
537 |
protected void initializeValues(int[] values) {
|
|
538 |
assert(assertCanChangeLength(this));
|
|
539 |
assert(length == 0);
|
|
540 |
this.values = values;
|
|
541 |
this.length = values.length;
|
|
542 |
}
|
|
543 |
|
|
544 |
/** Collect one value, or store one decoded value. */
|
|
545 |
protected void addValue(int x) {
|
|
546 |
assert(assertCanChangeLength(this));
|
|
547 |
if (length == values.length)
|
|
548 |
setCapacity(length < 1000 ? length * 10 : length * 2);
|
|
549 |
values[length++] = x;
|
|
550 |
}
|
|
551 |
|
|
552 |
private boolean canVaryCoding() {
|
|
553 |
if (!optVaryCodings) return false;
|
|
554 |
if (length == 0) return false;
|
|
555 |
// Can't read band_headers w/o the archive header:
|
|
556 |
if (this == archive_header_0) return false;
|
|
557 |
if (this == archive_header_S) return false;
|
|
558 |
if (this == archive_header_1) return false;
|
|
559 |
// BYTE1 bands can't vary codings, but the others can.
|
|
560 |
// All that's needed for the initial escape is at least
|
|
561 |
// 256 negative values or more than 256 non-negative values
|
|
562 |
return (regularCoding.min() <= -256 || regularCoding.max() >= 256);
|
|
563 |
}
|
|
564 |
|
|
565 |
private boolean shouldVaryCoding() {
|
|
566 |
assert(canVaryCoding());
|
|
567 |
if (effort < MAX_EFFORT && length < SHORT_BAND_HEURISTIC)
|
|
568 |
return false;
|
|
569 |
return true;
|
|
570 |
}
|
|
571 |
|
|
572 |
protected void chooseBandCodings() throws IOException {
|
|
573 |
boolean canVary = canVaryCoding();
|
|
574 |
if (!canVary || !shouldVaryCoding()) {
|
|
575 |
if (regularCoding.canRepresent(values, 0, length)) {
|
|
576 |
bandCoding = regularCoding;
|
|
577 |
} else {
|
|
578 |
assert(canVary);
|
|
579 |
if (verbose > 1)
|
|
580 |
Utils.log.fine("regular coding fails in band "+name());
|
|
581 |
bandCoding = UNSIGNED5;
|
|
582 |
}
|
|
583 |
outputSize = -1;
|
|
584 |
} else {
|
|
585 |
int[] sizes = {0,0};
|
|
586 |
bandCoding = chooseCoding(values, 0, length,
|
|
587 |
regularCoding, name(),
|
|
588 |
sizes);
|
|
589 |
outputSize = sizes[CodingChooser.BYTE_SIZE];
|
|
590 |
if (outputSize == 0) // CodingChooser failed to size it.
|
|
591 |
outputSize = -1;
|
|
592 |
}
|
|
593 |
|
|
594 |
// Compute and save the meta-coding bytes also.
|
|
595 |
if (bandCoding != regularCoding) {
|
|
596 |
metaCoding = bandCoding.getMetaCoding(regularCoding);
|
|
597 |
if (verbose > 1) {
|
|
598 |
Utils.log.fine("alternate coding "+this+" "+bandCoding);
|
|
599 |
}
|
|
600 |
} else if (canVary &&
|
|
601 |
decodeEscapeValue(values[0], regularCoding) >= 0) {
|
|
602 |
// Need an explicit default.
|
|
603 |
metaCoding = defaultMetaCoding;
|
|
604 |
} else {
|
|
605 |
// Common case: Zero bytes of meta coding.
|
|
606 |
metaCoding = noMetaCoding;
|
|
607 |
}
|
|
608 |
if (metaCoding.length > 0
|
|
609 |
&& (verbose > 2 || verbose > 1 && metaCoding.length > 1)) {
|
|
610 |
StringBuffer sb = new StringBuffer();
|
|
611 |
for (int i = 0; i < metaCoding.length; i++) {
|
|
612 |
if (i == 1) sb.append(" /");
|
|
613 |
sb.append(" ").append(metaCoding[i] & 0xFF);
|
|
614 |
}
|
|
615 |
Utils.log.fine(" meta-coding "+sb);
|
|
616 |
}
|
|
617 |
|
|
618 |
assert((outputSize < 0) ||
|
|
619 |
!(bandCoding instanceof Coding) ||
|
|
620 |
(outputSize == ((Coding)bandCoding)
|
|
621 |
.getLength(values, 0, length)))
|
|
622 |
: (bandCoding+" : "+
|
|
623 |
outputSize+" != "+
|
|
624 |
((Coding)bandCoding).getLength(values, 0, length)
|
|
625 |
+" ?= "+getCodingChooser().computeByteSize(bandCoding,values,0,length)
|
|
626 |
);
|
|
627 |
|
|
628 |
// Compute outputSize of the escape value X, if any.
|
|
629 |
if (metaCoding.length > 0) {
|
|
630 |
// First byte XB of meta-coding is treated specially,
|
|
631 |
// but any other bytes go into the band headers band.
|
|
632 |
// This must be done before any other output happens.
|
|
633 |
if (outputSize >= 0)
|
|
634 |
outputSize += computeEscapeSize(); // good cache
|
|
635 |
// Other bytes go into band_headers.
|
|
636 |
for (int i = 1; i < metaCoding.length; i++) {
|
|
637 |
band_headers.putByte(metaCoding[i] & 0xFF);
|
|
638 |
}
|
|
639 |
}
|
|
640 |
}
|
|
641 |
|
|
642 |
protected long computeOutputSize() {
|
|
643 |
outputSize = getCodingChooser().computeByteSize(bandCoding,
|
|
644 |
values, 0, length);
|
|
645 |
assert(outputSize < Integer.MAX_VALUE);
|
|
646 |
outputSize += computeEscapeSize();
|
|
647 |
return outputSize;
|
|
648 |
}
|
|
649 |
|
|
650 |
protected int computeEscapeSize() {
|
|
651 |
if (metaCoding.length == 0) return 0;
|
|
652 |
int XB = metaCoding[0] & 0xFF;
|
|
653 |
int X = encodeEscapeValue(XB, regularCoding);
|
|
654 |
return regularCoding.setD(0).getLength(X);
|
|
655 |
}
|
|
656 |
|
|
657 |
protected void writeDataTo(OutputStream out) throws IOException {
|
|
658 |
if (length == 0) return; // nothing to write
|
|
659 |
long len0 = 0;
|
|
660 |
if (out == outputCounter) {
|
|
661 |
len0 = outputCounter.getCount();
|
|
662 |
}
|
|
663 |
if (metaCoding.length > 0) {
|
|
664 |
int XB = metaCoding[0] & 0xFF;
|
|
665 |
// We need an explicit band header, either because
|
|
666 |
// there is a non-default coding method, or because
|
|
667 |
// the first value would be parsed as an escape value.
|
|
668 |
int X = encodeEscapeValue(XB, regularCoding);
|
|
669 |
//System.out.println("X="+X+" XB="+XB+" in "+this);
|
|
670 |
regularCoding.setD(0).writeTo(out, X);
|
|
671 |
}
|
|
672 |
bandCoding.writeArrayTo(out, values, 0, length);
|
|
673 |
if (out == outputCounter) {
|
|
674 |
long len1 = outputCounter.getCount();
|
|
675 |
assert(outputSize == outputCounter.getCount() - len0)
|
|
676 |
: (outputSize+" != "+outputCounter.getCount()+"-"+len0);
|
|
677 |
}
|
|
678 |
if (optDumpBands) dumpBand();
|
|
679 |
}
|
|
680 |
|
|
681 |
protected void readDataFrom(InputStream in) throws IOException {
|
|
682 |
length = valuesExpected();
|
|
683 |
if (length == 0) return; // nothing to read
|
|
684 |
if (verbose > 1)
|
|
685 |
Utils.log.fine("Reading band "+this);
|
|
686 |
if (!canVaryCoding()) {
|
|
687 |
bandCoding = regularCoding;
|
|
688 |
metaCoding = noMetaCoding;
|
|
689 |
} else {
|
|
690 |
assert(in.markSupported()); // input must be buffered
|
|
691 |
in.mark(Coding.B_MAX);
|
|
692 |
int X = regularCoding.setD(0).readFrom(in);
|
|
693 |
int XB = decodeEscapeValue(X, regularCoding);
|
|
694 |
if (XB < 0) {
|
|
695 |
// Do not consume this value. No alternate coding.
|
|
696 |
in.reset();
|
|
697 |
XB = _meta_default;
|
|
698 |
bandCoding = regularCoding;
|
|
699 |
metaCoding = noMetaCoding;
|
|
700 |
} else if (XB == _meta_default) {
|
|
701 |
bandCoding = regularCoding;
|
|
702 |
metaCoding = defaultMetaCoding;
|
|
703 |
} else {
|
|
704 |
if (verbose > 2)
|
|
705 |
Utils.log.fine("found X="+X+" => XB="+XB);
|
|
706 |
bandCoding = getBandHeader(XB, regularCoding);
|
|
707 |
// This is really used only by dumpBands.
|
|
708 |
int p0 = bandHeaderBytePos0;
|
|
709 |
int p1 = bandHeaderBytePos;
|
|
710 |
metaCoding = new byte[p1-p0];
|
|
711 |
System.arraycopy(bandHeaderBytes, p0,
|
|
712 |
metaCoding, 0, metaCoding.length);
|
|
713 |
}
|
|
714 |
}
|
|
715 |
if (bandCoding != regularCoding) {
|
|
716 |
if (verbose > 1)
|
|
717 |
Utils.log.fine(name()+": irregular coding "+bandCoding);
|
|
718 |
}
|
|
719 |
bandCoding.readArrayFrom(in, values, 0, length);
|
|
720 |
if (optDumpBands) dumpBand();
|
|
721 |
}
|
|
722 |
|
|
723 |
public void doneDisbursing() {
|
|
724 |
super.doneDisbursing();
|
|
725 |
values = null; // for GC
|
|
726 |
}
|
|
727 |
|
|
728 |
private void dumpBand() throws IOException {
|
|
729 |
assert(optDumpBands);
|
|
730 |
PrintStream ps = new PrintStream(getDumpStream(this, ".txt"));
|
|
731 |
String irr = (bandCoding == regularCoding) ? "" : " irregular";
|
|
732 |
ps.print("# length="+length+
|
|
733 |
" size="+outputSize()+
|
|
734 |
irr+" coding="+bandCoding);
|
|
735 |
if (metaCoding != noMetaCoding) {
|
|
736 |
StringBuffer sb = new StringBuffer();
|
|
737 |
for (int i = 0; i < metaCoding.length; i++) {
|
|
738 |
if (i == 1) sb.append(" /");
|
|
739 |
sb.append(" ").append(metaCoding[i] & 0xFF);
|
|
740 |
}
|
|
741 |
ps.print(" //header: "+sb);
|
|
742 |
}
|
|
743 |
printArrayTo(ps, values, 0, length);
|
|
744 |
ps.close();
|
|
745 |
OutputStream ds = getDumpStream(this, ".bnd");
|
|
746 |
bandCoding.writeArrayTo(ds, values, 0, length);
|
|
747 |
ds.close();
|
|
748 |
}
|
|
749 |
|
|
750 |
/** Disburse one value. */
|
|
751 |
protected int getValue() {
|
|
752 |
assert(phase() == DISBURSE_PHASE);
|
|
753 |
assert(valuesDisbursed < length);
|
|
754 |
return values[valuesDisbursed++];
|
|
755 |
}
|
|
756 |
|
|
757 |
/** Reset for another pass over the same value set. */
|
|
758 |
public void resetForSecondPass() {
|
|
759 |
assert(phase() == DISBURSE_PHASE);
|
|
760 |
assert(valuesDisbursed == length()); // 1st pass is complete
|
|
761 |
valuesDisbursed = 0;
|
|
762 |
}
|
|
763 |
}
|
|
764 |
|
|
765 |
class ByteBand extends Band {
|
|
766 |
private ByteArrayOutputStream bytes; // input buffer
|
|
767 |
private ByteArrayOutputStream bytesForDump;
|
|
768 |
private InputStream in;
|
|
769 |
|
|
770 |
public ByteBand(String name) {
|
|
771 |
super(name, BYTE1);
|
|
772 |
}
|
|
773 |
|
|
774 |
public int capacity() {
|
|
775 |
return bytes == null ? -1 : Integer.MAX_VALUE;
|
|
776 |
}
|
|
777 |
protected void setCapacity(int cap) {
|
|
778 |
assert(bytes == null); // do this just once
|
|
779 |
bytes = new ByteArrayOutputStream(cap);
|
|
780 |
}
|
|
781 |
public void destroy() {
|
|
782 |
lengthForDebug = length();
|
|
783 |
bytes = null;
|
|
784 |
}
|
|
785 |
|
|
786 |
public int length() {
|
|
787 |
return bytes == null ? -1 : bytes.size();
|
|
788 |
}
|
|
789 |
public void reset() {
|
|
790 |
bytes.reset();
|
|
791 |
}
|
|
792 |
protected int valuesRemainingForDebug() {
|
|
793 |
return (bytes == null) ? -1 : ((ByteArrayInputStream)in).available();
|
|
794 |
}
|
|
795 |
|
|
796 |
protected void chooseBandCodings() throws IOException {
|
|
797 |
// No-op.
|
|
798 |
assert(decodeEscapeValue(regularCoding.min(), regularCoding) < 0);
|
|
799 |
assert(decodeEscapeValue(regularCoding.max(), regularCoding) < 0);
|
|
800 |
}
|
|
801 |
|
|
802 |
protected long computeOutputSize() {
|
|
803 |
// do not cache
|
|
804 |
return bytes.size();
|
|
805 |
}
|
|
806 |
|
|
807 |
public void writeDataTo(OutputStream out) throws IOException {
|
|
808 |
if (length() == 0) return;
|
|
809 |
bytes.writeTo(out);
|
|
810 |
if (optDumpBands) dumpBand();
|
|
811 |
destroy(); // done with the bits!
|
|
812 |
}
|
|
813 |
|
|
814 |
private void dumpBand() throws IOException {
|
|
815 |
assert(optDumpBands);
|
|
816 |
OutputStream ds = getDumpStream(this, ".bnd");
|
|
817 |
if (bytesForDump != null)
|
|
818 |
bytesForDump.writeTo(ds);
|
|
819 |
else
|
|
820 |
bytes.writeTo(ds);
|
|
821 |
ds.close();
|
|
822 |
}
|
|
823 |
|
|
824 |
public void readDataFrom(InputStream in) throws IOException {
|
|
825 |
int vex = valuesExpected();
|
|
826 |
if (vex == 0) return;
|
|
827 |
if (verbose > 1) {
|
|
828 |
lengthForDebug = vex;
|
|
829 |
Utils.log.fine("Reading band "+this);
|
|
830 |
lengthForDebug = -1;
|
|
831 |
}
|
|
832 |
byte[] buf = new byte[Math.min(vex, 1<<14)];
|
|
833 |
while (vex > 0) {
|
|
834 |
int nr = in.read(buf, 0, Math.min(vex, buf.length));
|
|
835 |
if (nr < 0) throw new EOFException();
|
|
836 |
bytes.write(buf, 0, nr);
|
|
837 |
vex -= nr;
|
|
838 |
}
|
|
839 |
if (optDumpBands) dumpBand();
|
|
840 |
}
|
|
841 |
|
|
842 |
public void readyToDisburse() {
|
|
843 |
in = new ByteArrayInputStream(bytes.toByteArray());
|
|
844 |
super.readyToDisburse();
|
|
845 |
}
|
|
846 |
|
|
847 |
public void doneDisbursing() {
|
|
848 |
super.doneDisbursing();
|
|
849 |
if (optDumpBands
|
|
850 |
&& bytesForDump != null && bytesForDump.size() > 0) {
|
|
851 |
try {
|
|
852 |
dumpBand();
|
|
853 |
} catch (IOException ee) {
|
|
854 |
throw new RuntimeException(ee);
|
|
855 |
}
|
|
856 |
}
|
|
857 |
in = null; // GC
|
|
858 |
bytes = null; // GC
|
|
859 |
bytesForDump = null; // GC
|
|
860 |
}
|
|
861 |
|
|
862 |
// alternative to readFrom:
|
|
863 |
public void setInputStreamFrom(InputStream in) throws IOException {
|
|
864 |
assert(bytes == null);
|
|
865 |
assert(assertReadyToReadFrom(this, in));
|
|
866 |
setPhase(READ_PHASE);
|
|
867 |
this.in = in;
|
|
868 |
if (optDumpBands) {
|
|
869 |
// Tap the stream.
|
|
870 |
bytesForDump = new ByteArrayOutputStream();
|
|
871 |
this.in = new FilterInputStream(in) {
|
|
872 |
public int read() throws IOException {
|
|
873 |
int ch = in.read();
|
|
874 |
if (ch >= 0) bytesForDump.write(ch);
|
|
875 |
return ch;
|
|
876 |
}
|
|
877 |
public int read(byte b[], int off, int len) throws IOException {
|
|
878 |
int nr = in.read(b, off, len);
|
|
879 |
if (nr >= 0) bytesForDump.write(b, off, nr);
|
|
880 |
return nr;
|
|
881 |
}
|
|
882 |
};
|
|
883 |
}
|
|
884 |
super.readyToDisburse();
|
|
885 |
}
|
|
886 |
|
|
887 |
public OutputStream collectorStream() {
|
|
888 |
assert(phase() == COLLECT_PHASE);
|
|
889 |
assert(bytes != null);
|
|
890 |
return bytes;
|
|
891 |
}
|
|
892 |
|
|
893 |
public InputStream getInputStream() {
|
|
894 |
assert(phase() == DISBURSE_PHASE);
|
|
895 |
assert(in != null);
|
|
896 |
return in;
|
|
897 |
}
|
|
898 |
public int getByte() throws IOException {
|
|
899 |
int b = getInputStream().read();
|
|
900 |
if (b < 0) throw new EOFException();
|
|
901 |
return b;
|
|
902 |
}
|
|
903 |
public void putByte(int b) throws IOException {
|
|
904 |
assert(b == (b & 0xFF));
|
|
905 |
collectorStream().write(b);
|
|
906 |
}
|
|
907 |
public String toString() {
|
|
908 |
return "byte "+super.toString();
|
|
909 |
}
|
|
910 |
}
|
|
911 |
|
|
912 |
class IntBand extends ValueBand {
|
|
913 |
// The usual coding for bands is 7bit/5byte/delta.
|
|
914 |
public IntBand(String name, Coding regularCoding) {
|
|
915 |
super(name, regularCoding);
|
|
916 |
}
|
|
917 |
|
|
918 |
public void putInt(int x) {
|
|
919 |
assert(phase() == COLLECT_PHASE);
|
|
920 |
addValue(x);
|
|
921 |
}
|
|
922 |
|
|
923 |
public int getInt() {
|
|
924 |
return getValue();
|
|
925 |
}
|
|
926 |
/** Return the sum of all values in this band. */
|
|
927 |
public int getIntTotal() {
|
|
928 |
assert(phase() == DISBURSE_PHASE);
|
|
929 |
// assert that this is the whole pass; no other reads allowed
|
|
930 |
assert(valuesRemainingForDebug() == length());
|
|
931 |
int total = 0;
|
|
932 |
for (int k = length(); k > 0; k--) {
|
|
933 |
total += getInt();
|
|
934 |
}
|
|
935 |
resetForSecondPass();
|
|
936 |
return total;
|
|
937 |
}
|
|
938 |
/** Return the occurrence count of a specific value in this band. */
|
|
939 |
public int getIntCount(int value) {
|
|
940 |
assert(phase() == DISBURSE_PHASE);
|
|
941 |
// assert that this is the whole pass; no other reads allowed
|
|
942 |
assert(valuesRemainingForDebug() == length());
|
|
943 |
int total = 0;
|
|
944 |
for (int k = length(); k > 0; k--) {
|
|
945 |
if (getInt() == value) {
|
|
946 |
total += 1;
|
|
947 |
}
|
|
948 |
}
|
|
949 |
resetForSecondPass();
|
|
950 |
return total;
|
|
951 |
}
|
|
952 |
}
|
|
953 |
|
|
954 |
static int getIntTotal(int[] values) {
|
|
955 |
int total = 0;
|
|
956 |
for (int i = 0; i < values.length; i++) {
|
|
957 |
total += values[i];
|
|
958 |
}
|
|
959 |
return total;
|
|
960 |
}
|
|
961 |
|
|
962 |
class CPRefBand extends ValueBand {
|
|
963 |
Index index;
|
|
964 |
boolean nullOK;
|
|
965 |
|
|
966 |
public CPRefBand(String name, Coding regularCoding, byte cpTag, boolean nullOK) {
|
|
967 |
super(name, regularCoding);
|
|
968 |
this.nullOK = nullOK;
|
|
969 |
if (cpTag != CONSTANT_None)
|
|
970 |
setBandIndex(this, cpTag);
|
|
971 |
}
|
|
972 |
public CPRefBand(String name, Coding regularCoding, byte cpTag) {
|
|
973 |
this(name, regularCoding, cpTag, false);
|
|
974 |
}
|
|
975 |
public CPRefBand(String name, Coding regularCoding, Object undef) {
|
|
976 |
this(name, regularCoding, CONSTANT_None, false);
|
|
977 |
}
|
|
978 |
|
|
979 |
public void setIndex(Index index) {
|
|
980 |
this.index = index;
|
|
981 |
}
|
|
982 |
|
|
983 |
protected void readDataFrom(InputStream in) throws IOException {
|
|
984 |
super.readDataFrom(in);
|
|
985 |
assert(assertValidCPRefs(this));
|
|
986 |
}
|
|
987 |
|
|
988 |
/** Write a constant pool reference. */
|
|
989 |
public void putRef(Entry e) {
|
|
990 |
assert(index != null);
|
|
991 |
addValue(encodeRefOrNull(e, index));
|
|
992 |
}
|
|
993 |
public void putRef(Entry e, Index index) {
|
|
994 |
assert(this.index == null);
|
|
995 |
addValue(encodeRefOrNull(e, index));
|
|
996 |
}
|
|
997 |
public void putRef(Entry e, byte cptag) {
|
|
998 |
putRef(e, getCPIndex(cptag));
|
|
999 |
}
|
|
1000 |
|
|
1001 |
public Entry getRef() {
|
|
1002 |
if (index == null) Utils.log.warning("No index for "+this);
|
|
1003 |
assert(index != null);
|
|
1004 |
return decodeRefOrNull(getValue(), index);
|
|
1005 |
}
|
|
1006 |
public Entry getRef(Index index) {
|
|
1007 |
assert(this.index == null);
|
|
1008 |
return decodeRefOrNull(getValue(), index);
|
|
1009 |
}
|
|
1010 |
public Entry getRef(byte cptag) {
|
|
1011 |
return getRef(getCPIndex(cptag));
|
|
1012 |
}
|
|
1013 |
|
|
1014 |
private int encodeRefOrNull(Entry e, Index index) {
|
|
1015 |
int nonNullCode; // NNC is the coding which assumes nulls are rare
|
|
1016 |
if (e == null) {
|
|
1017 |
nonNullCode = -1; // negative values are rare
|
|
1018 |
} else {
|
|
1019 |
nonNullCode = encodeRef(e, index);
|
|
1020 |
}
|
|
1021 |
// If nulls are expected, increment, to make -1 code turn to 0.
|
|
1022 |
return (nullOK ? 1 : 0) + nonNullCode;
|
|
1023 |
}
|
|
1024 |
private Entry decodeRefOrNull(int code, Index index) {
|
|
1025 |
// Inverse to encodeRefOrNull...
|
|
1026 |
int nonNullCode = code - (nullOK ? 1 : 0);
|
|
1027 |
if (nonNullCode == -1) {
|
|
1028 |
return null;
|
|
1029 |
} else {
|
|
1030 |
return decodeRef(nonNullCode, index);
|
|
1031 |
}
|
|
1032 |
}
|
|
1033 |
}
|
|
1034 |
|
|
1035 |
// Bootstrap support for CPRefBands. These are needed to record
|
|
1036 |
// intended CP indexes, before the CP has been created.
|
|
1037 |
private ArrayList allKQBands = new ArrayList();
|
|
1038 |
private ArrayList needPredefIndex = new ArrayList();
|
|
1039 |
|
|
1040 |
|
|
1041 |
int encodeRef(Entry e, Index ix) {
|
|
1042 |
int coding = ix.indexOf(e);
|
|
1043 |
if (verbose > 2)
|
|
1044 |
Utils.log.fine("putRef "+coding+" => "+e);
|
|
1045 |
return coding;
|
|
1046 |
}
|
|
1047 |
|
|
1048 |
Entry decodeRef(int n, Index ix) {
|
|
1049 |
if (n < 0 || n >= ix.size())
|
|
1050 |
Utils.log.warning("decoding bad ref "+n+" in "+ix);
|
|
1051 |
Entry e = ix.getEntry(n);
|
|
1052 |
if (verbose > 2)
|
|
1053 |
Utils.log.fine("getRef "+n+" => "+e);
|
|
1054 |
return e;
|
|
1055 |
}
|
|
1056 |
|
|
1057 |
private CodingChooser codingChooser;
|
|
1058 |
protected CodingChooser getCodingChooser() {
|
|
1059 |
if (codingChooser == null) {
|
|
1060 |
codingChooser = new CodingChooser(effort, basicCodings);
|
|
1061 |
if (codingChooser.stress != null
|
|
1062 |
&& this instanceof PackageWriter) {
|
|
1063 |
// Twist the random state based on my first file.
|
|
1064 |
// This sends each segment off in a different direction.
|
|
1065 |
List classes = ((PackageWriter)this).pkg.classes;
|
|
1066 |
if (!classes.isEmpty()) {
|
|
1067 |
Package.Class cls = (Package.Class) classes.get(0);
|
|
1068 |
codingChooser.addStressSeed(cls.getName().hashCode());
|
|
1069 |
}
|
|
1070 |
}
|
|
1071 |
}
|
|
1072 |
return codingChooser;
|
|
1073 |
}
|
|
1074 |
|
|
1075 |
public CodingMethod chooseCoding(int[] values, int start, int end,
|
|
1076 |
Coding regular, String bandName,
|
|
1077 |
int[] sizes) {
|
|
1078 |
assert(optVaryCodings);
|
|
1079 |
if (effort <= MIN_EFFORT) {
|
|
1080 |
return regular;
|
|
1081 |
}
|
|
1082 |
CodingChooser cc = getCodingChooser();
|
|
1083 |
if (verbose > 1 || cc.verbose > 1) {
|
|
1084 |
Utils.log.fine("--- chooseCoding "+bandName);
|
|
1085 |
}
|
|
1086 |
return cc.choose(values, start, end, regular, sizes);
|
|
1087 |
}
|
|
1088 |
|
|
1089 |
static final byte[] defaultMetaCoding = { _meta_default };
|
|
1090 |
static final byte[] noMetaCoding = {};
|
|
1091 |
|
|
1092 |
// The first value in a band is always coded with the default coding D.
|
|
1093 |
// If this first value X is an escape value, it actually represents the
|
|
1094 |
// first (and perhaps only) byte of a meta-coding.
|
|
1095 |
//
|
|
1096 |
// If D.S != 0 and D includes the range [-256..-1],
|
|
1097 |
// the escape values are in that range,
|
|
1098 |
// and the first byte XB is -1-X.
|
|
1099 |
//
|
|
1100 |
// If D.S == 0 and D includes the range [(D.L)..(D.L)+255],
|
|
1101 |
// the escape values are in that range,
|
|
1102 |
// and XB is X-(D.L).
|
|
1103 |
//
|
|
1104 |
// This representation is designed so that a band header is unlikely
|
|
1105 |
// to be confused with the initial value of a headerless band,
|
|
1106 |
// and yet so that a band header is likely to occupy only a byte or two.
|
|
1107 |
//
|
|
1108 |
// Result is in [0..255] if XB was successfully extracted, else -1.
|
|
1109 |
// See section "Coding Specifier Meta-Encoding" in the JSR 200 spec.
|
|
1110 |
protected static int decodeEscapeValue(int X, Coding regularCoding) {
|
|
1111 |
// The first value in a band is always coded with the default coding D.
|
|
1112 |
// If this first value X is an escape value, it actually represents the
|
|
1113 |
// first (and perhaps only) byte of a meta-coding.
|
|
1114 |
// Result is in [0..255] if XB was successfully extracted, else -1.
|
|
1115 |
if (regularCoding.B() == 1 || regularCoding.L() == 0)
|
|
1116 |
return -1; // degenerate regular coding (BYTE1)
|
|
1117 |
if (regularCoding.S() != 0) {
|
|
1118 |
if (-256 <= X && X <= -1 && regularCoding.min() <= -256) {
|
|
1119 |
int XB = -1-X;
|
|
1120 |
assert(XB >= 0 && XB < 256);
|
|
1121 |
return XB;
|
|
1122 |
}
|
|
1123 |
} else {
|
|
1124 |
int L = regularCoding.L();
|
|
1125 |
if (L <= X && X <= L+255 && regularCoding.max() >= L+255) {
|
|
1126 |
int XB = X-L;
|
|
1127 |
assert(XB >= 0 && XB < 256);
|
|
1128 |
return XB;
|
|
1129 |
}
|
|
1130 |
}
|
|
1131 |
return -1; // negative value for failure
|
|
1132 |
}
|
|
1133 |
// Inverse to decodeEscapeValue().
|
|
1134 |
protected static int encodeEscapeValue(int XB, Coding regularCoding) {
|
|
1135 |
assert(XB >= 0 && XB < 256);
|
|
1136 |
assert(regularCoding.B() > 1 && regularCoding.L() > 0);
|
|
1137 |
int X;
|
|
1138 |
if (regularCoding.S() != 0) {
|
|
1139 |
assert(regularCoding.min() <= -256);
|
|
1140 |
X = -1-XB;
|
|
1141 |
} else {
|
|
1142 |
int L = regularCoding.L();
|
|
1143 |
assert(regularCoding.max() >= L+255);
|
|
1144 |
X = XB+L;
|
|
1145 |
}
|
|
1146 |
assert(decodeEscapeValue(X, regularCoding) == XB)
|
|
1147 |
: (regularCoding+" XB="+XB+" X="+X);
|
|
1148 |
return X;
|
|
1149 |
}
|
|
1150 |
|
|
1151 |
static {
|
|
1152 |
boolean checkXB = false;
|
|
1153 |
assert(checkXB = true);
|
|
1154 |
if (checkXB) {
|
|
1155 |
for (int i = 0; i < basicCodings.length; i++) {
|
|
1156 |
Coding D = basicCodings[i];
|
|
1157 |
if (D == null) continue;
|
|
1158 |
if (D.B() == 1) continue;
|
|
1159 |
if (D.L() == 0) continue;
|
|
1160 |
for (int XB = 0; XB <= 255; XB++) {
|
|
1161 |
// The following exercises decodeEscapeValue also:
|
|
1162 |
encodeEscapeValue(XB, D);
|
|
1163 |
}
|
|
1164 |
}
|
|
1165 |
}
|
|
1166 |
}
|
|
1167 |
|
|
1168 |
class MultiBand extends Band {
|
|
1169 |
MultiBand(String name, Coding regularCoding) {
|
|
1170 |
super(name, regularCoding);
|
|
1171 |
}
|
|
1172 |
|
|
1173 |
public Band init() {
|
|
1174 |
super.init();
|
|
1175 |
// This is all just to keep the asserts happy:
|
|
1176 |
setCapacity(0);
|
|
1177 |
if (phase() == EXPECT_PHASE) {
|
|
1178 |
// Fast forward:
|
|
1179 |
setPhase(READ_PHASE);
|
|
1180 |
setPhase(DISBURSE_PHASE);
|
|
1181 |
}
|
|
1182 |
return this;
|
|
1183 |
}
|
|
1184 |
|
|
1185 |
Band[] bands = new Band[10];
|
|
1186 |
int bandCount = 0;
|
|
1187 |
|
|
1188 |
int size() {
|
|
1189 |
return bandCount;
|
|
1190 |
}
|
|
1191 |
Band get(int i) {
|
|
1192 |
assert(i < bandCount);
|
|
1193 |
return bands[i];
|
|
1194 |
}
|
|
1195 |
Band[] toArray() {
|
|
1196 |
return (Band[]) realloc(bands, bandCount);
|
|
1197 |
}
|
|
1198 |
|
|
1199 |
void add(Band b) {
|
|
1200 |
assert(bandCount == 0 || notePrevForAssert(b, bands[bandCount-1]));
|
|
1201 |
if (bandCount == bands.length) {
|
|
1202 |
bands = (Band[]) realloc(bands);
|
|
1203 |
}
|
|
1204 |
bands[bandCount++] = b;
|
|
1205 |
}
|
|
1206 |
|
|
1207 |
ByteBand newByteBand(String name) {
|
|
1208 |
ByteBand b = new ByteBand(name);
|
|
1209 |
b.init(); add(b);
|
|
1210 |
return b;
|
|
1211 |
}
|
|
1212 |
IntBand newIntBand(String name) {
|
|
1213 |
IntBand b = new IntBand(name, regularCoding);
|
|
1214 |
b.init(); add(b);
|
|
1215 |
return b;
|
|
1216 |
}
|
|
1217 |
IntBand newIntBand(String name, Coding regularCoding) {
|
|
1218 |
IntBand b = new IntBand(name, regularCoding);
|
|
1219 |
b.init(); add(b);
|
|
1220 |
return b;
|
|
1221 |
}
|
|
1222 |
MultiBand newMultiBand(String name, Coding regularCoding) {
|
|
1223 |
MultiBand b = new MultiBand(name, regularCoding);
|
|
1224 |
b.init(); add(b);
|
|
1225 |
return b;
|
|
1226 |
}
|
|
1227 |
CPRefBand newCPRefBand(String name, byte cpTag) {
|
|
1228 |
CPRefBand b = new CPRefBand(name, regularCoding, cpTag);
|
|
1229 |
b.init(); add(b);
|
|
1230 |
return b;
|
|
1231 |
}
|
|
1232 |
CPRefBand newCPRefBand(String name, Coding regularCoding,
|
|
1233 |
byte cpTag) {
|
|
1234 |
CPRefBand b = new CPRefBand(name, regularCoding, cpTag);
|
|
1235 |
b.init(); add(b);
|
|
1236 |
return b;
|
|
1237 |
}
|
|
1238 |
CPRefBand newCPRefBand(String name, Coding regularCoding,
|
|
1239 |
byte cpTag, boolean nullOK) {
|
|
1240 |
CPRefBand b = new CPRefBand(name, regularCoding, cpTag, nullOK);
|
|
1241 |
b.init(); add(b);
|
|
1242 |
return b;
|
|
1243 |
}
|
|
1244 |
|
|
1245 |
int bandCount() { return bandCount; }
|
|
1246 |
|
|
1247 |
private int cap = -1;
|
|
1248 |
public int capacity() { return cap; }
|
|
1249 |
public void setCapacity(int cap) { this.cap = cap; }
|
|
1250 |
|
|
1251 |
public int length() { return 0; }
|
|
1252 |
public int valuesRemainingForDebug() { return 0; }
|
|
1253 |
|
|
1254 |
protected void chooseBandCodings() throws IOException {
|
|
1255 |
// coding decision pass
|
|
1256 |
for (int i = 0; i < bandCount; i++) {
|
|
1257 |
Band b = bands[i];
|
|
1258 |
b.chooseBandCodings();
|
|
1259 |
}
|
|
1260 |
}
|
|
1261 |
|
|
1262 |
protected long computeOutputSize() {
|
|
1263 |
// coding decision pass
|
|
1264 |
long sum = 0;
|
|
1265 |
for (int i = 0; i < bandCount; i++) {
|
|
1266 |
Band b = bands[i];
|
|
1267 |
long bsize = b.outputSize();
|
|
1268 |
assert(bsize >= 0) : b;
|
|
1269 |
sum += bsize;
|
|
1270 |
}
|
|
1271 |
// do not cache
|
|
1272 |
return sum;
|
|
1273 |
}
|
|
1274 |
|
|
1275 |
protected void writeDataTo(OutputStream out) throws IOException {
|
|
1276 |
long preCount = 0;
|
|
1277 |
if (outputCounter != null) preCount = outputCounter.getCount();
|
|
1278 |
for (int i = 0; i < bandCount; i++) {
|
|
1279 |
Band b = bands[i];
|
|
1280 |
b.writeTo(out);
|
|
1281 |
if (outputCounter != null) {
|
|
1282 |
long postCount = outputCounter.getCount();
|
|
1283 |
long len = postCount - preCount;
|
|
1284 |
preCount = postCount;
|
|
1285 |
if ((verbose > 0 && len > 0) || verbose > 1) {
|
|
1286 |
Utils.log.info(" ...wrote "+len+" bytes from "+b);
|
|
1287 |
}
|
|
1288 |
}
|
|
1289 |
}
|
|
1290 |
}
|
|
1291 |
|
|
1292 |
protected void readDataFrom(InputStream in) throws IOException {
|
|
1293 |
assert(false); // not called?
|
|
1294 |
for (int i = 0; i < bandCount; i++) {
|
|
1295 |
Band b = bands[i];
|
|
1296 |
b.readFrom(in);
|
|
1297 |
if ((verbose > 0 && b.length() > 0) || verbose > 1) {
|
|
1298 |
Utils.log.info(" ...read "+b);
|
|
1299 |
}
|
|
1300 |
}
|
|
1301 |
}
|
|
1302 |
|
|
1303 |
public String toString() {
|
|
1304 |
return "{"+bandCount()+" bands: "+super.toString()+"}";
|
|
1305 |
}
|
|
1306 |
}
|
|
1307 |
|
|
1308 |
/**
|
|
1309 |
* An output stream which counts the number of bytes written.
|
|
1310 |
*/
|
|
1311 |
private static
|
|
1312 |
class ByteCounter extends FilterOutputStream {
|
|
1313 |
// (should go public under the name CountingOutputStream?)
|
|
1314 |
|
|
1315 |
private long count;
|
|
1316 |
|
|
1317 |
public ByteCounter(OutputStream out) {
|
|
1318 |
super(out);
|
|
1319 |
}
|
|
1320 |
|
|
1321 |
public long getCount() { return count; }
|
|
1322 |
public void setCount(long c) { count = c; }
|
|
1323 |
|
|
1324 |
public void write(int b) throws IOException {
|
|
1325 |
count++;
|
|
1326 |
if (out != null) out.write(b);
|
|
1327 |
}
|
|
1328 |
public void write(byte b[], int off, int len) throws IOException {
|
|
1329 |
count += len;
|
|
1330 |
if (out != null) out.write(b, off, len);
|
|
1331 |
}
|
|
1332 |
public String toString() {
|
|
1333 |
return String.valueOf(getCount());
|
|
1334 |
}
|
|
1335 |
}
|
|
1336 |
ByteCounter outputCounter;
|
|
1337 |
|
|
1338 |
void writeAllBandsTo(OutputStream out) throws IOException {
|
|
1339 |
// Wrap a byte-counter around the output stream.
|
|
1340 |
outputCounter = new ByteCounter(out);
|
|
1341 |
out = outputCounter;
|
|
1342 |
all_bands.writeTo(out);
|
|
1343 |
if (verbose > 0) {
|
|
1344 |
long nbytes = outputCounter.getCount();
|
|
1345 |
Utils.log.info("Wrote total of "+nbytes+" bytes.");
|
|
1346 |
assert(nbytes == archiveSize0+archiveSize1);
|
|
1347 |
}
|
|
1348 |
outputCounter = null;
|
|
1349 |
}
|
|
1350 |
|
|
1351 |
// random AO_XXX bits, decoded from the archive header
|
|
1352 |
protected int archiveOptions;
|
|
1353 |
|
|
1354 |
// archiveSize1 sizes most of the archive [archive_options..file_bits).
|
|
1355 |
protected long archiveSize0; // size through archive_size_lo
|
|
1356 |
protected long archiveSize1; // size reported in archive_header
|
|
1357 |
protected int archiveNextCount; // reported in archive_header
|
|
1358 |
|
|
1359 |
static final int AH_LENGTH_0 = 3; //minver, majver, options
|
|
1360 |
static final int AH_ARCHIVE_SIZE_HI = 0;
|
|
1361 |
static final int AH_ARCHIVE_SIZE_LO = 1;
|
|
1362 |
static final int AH_LENGTH_S = 2; //optional size hi/lo
|
|
1363 |
static final int AH_LENGTH = 26; // mentioned in spec
|
|
1364 |
// Length contributions from optional header fields:
|
|
1365 |
static final int AH_FILE_HEADER_LEN = 5; // sizehi/lo/next/modtime/files
|
|
1366 |
static final int AH_SPECIAL_FORMAT_LEN = 2; // layouts/band-headers
|
|
1367 |
static final int AH_CP_NUMBER_LEN = 4; // int/float/long/double
|
|
1368 |
static final int AH_LENGTH_MIN = AH_LENGTH
|
|
1369 |
-(AH_SPECIAL_FORMAT_LEN+AH_FILE_HEADER_LEN+AH_CP_NUMBER_LEN);
|
|
1370 |
|
|
1371 |
// Common structure of attribute band groups:
|
|
1372 |
static final int AB_FLAGS_HI = 0;
|
|
1373 |
static final int AB_FLAGS_LO = 1;
|
|
1374 |
static final int AB_ATTR_COUNT = 2;
|
|
1375 |
static final int AB_ATTR_INDEXES = 3;
|
|
1376 |
static final int AB_ATTR_CALLS = 4;
|
|
1377 |
|
|
1378 |
static IntBand getAttrBand(MultiBand xxx_attr_bands, int which) {
|
|
1379 |
IntBand b = (IntBand) xxx_attr_bands.get(which);
|
|
1380 |
switch (which) {
|
|
1381 |
case AB_FLAGS_HI:
|
|
1382 |
assert(b.name().endsWith("_flags_hi")); break;
|
|
1383 |
case AB_FLAGS_LO:
|
|
1384 |
assert(b.name().endsWith("_flags_lo")); break;
|
|
1385 |
case AB_ATTR_COUNT:
|
|
1386 |
assert(b.name().endsWith("_attr_count")); break;
|
|
1387 |
case AB_ATTR_INDEXES:
|
|
1388 |
assert(b.name().endsWith("_attr_indexes")); break;
|
|
1389 |
case AB_ATTR_CALLS:
|
|
1390 |
assert(b.name().endsWith("_attr_calls")); break;
|
|
1391 |
default:
|
|
1392 |
assert(false); break;
|
|
1393 |
}
|
|
1394 |
return b;
|
|
1395 |
}
|
|
1396 |
|
|
1397 |
static private final boolean NULL_IS_OK = true;
|
|
1398 |
|
|
1399 |
MultiBand all_bands = (MultiBand) new MultiBand("(package)", UNSIGNED5).init();
|
|
1400 |
|
|
1401 |
// file header (various random bytes)
|
|
1402 |
ByteBand archive_magic = all_bands.newByteBand("archive_magic");
|
|
1403 |
IntBand archive_header_0 = all_bands.newIntBand("archive_header_0", UNSIGNED5);
|
|
1404 |
IntBand archive_header_S = all_bands.newIntBand("archive_header_S", UNSIGNED5);
|
|
1405 |
IntBand archive_header_1 = all_bands.newIntBand("archive_header_1", UNSIGNED5);
|
|
1406 |
ByteBand band_headers = all_bands.newByteBand("band_headers");
|
|
1407 |
|
|
1408 |
// constant pool contents
|
|
1409 |
MultiBand cp_bands = all_bands.newMultiBand("(constant_pool)", DELTA5);
|
|
1410 |
IntBand cp_Utf8_prefix = cp_bands.newIntBand("cp_Utf8_prefix");
|
|
1411 |
IntBand cp_Utf8_suffix = cp_bands.newIntBand("cp_Utf8_suffix", UNSIGNED5);
|
|
1412 |
IntBand cp_Utf8_chars = cp_bands.newIntBand("cp_Utf8_chars", CHAR3);
|
|
1413 |
IntBand cp_Utf8_big_suffix = cp_bands.newIntBand("cp_Utf8_big_suffix");
|
|
1414 |
MultiBand cp_Utf8_big_chars = cp_bands.newMultiBand("(cp_Utf8_big_chars)", DELTA5);
|
|
1415 |
IntBand cp_Int = cp_bands.newIntBand("cp_Int", UDELTA5);
|
|
1416 |
IntBand cp_Float = cp_bands.newIntBand("cp_Float", UDELTA5);
|
|
1417 |
IntBand cp_Long_hi = cp_bands.newIntBand("cp_Long_hi", UDELTA5);
|
|
1418 |
IntBand cp_Long_lo = cp_bands.newIntBand("cp_Long_lo");
|
|
1419 |
IntBand cp_Double_hi = cp_bands.newIntBand("cp_Double_hi", UDELTA5);
|
|
1420 |
IntBand cp_Double_lo = cp_bands.newIntBand("cp_Double_lo");
|
|
1421 |
CPRefBand cp_String = cp_bands.newCPRefBand("cp_String", UDELTA5, CONSTANT_Utf8);
|
|
1422 |
CPRefBand cp_Class = cp_bands.newCPRefBand("cp_Class", UDELTA5, CONSTANT_Utf8);
|
|
1423 |
CPRefBand cp_Signature_form = cp_bands.newCPRefBand("cp_Signature_form", CONSTANT_Utf8);
|
|
1424 |
CPRefBand cp_Signature_classes = cp_bands.newCPRefBand("cp_Signature_classes", UDELTA5, CONSTANT_Class);
|
|
1425 |
CPRefBand cp_Descr_name = cp_bands.newCPRefBand("cp_Descr_name", CONSTANT_Utf8);
|
|
1426 |
CPRefBand cp_Descr_type = cp_bands.newCPRefBand("cp_Descr_type", UDELTA5, CONSTANT_Signature);
|
|
1427 |
CPRefBand cp_Field_class = cp_bands.newCPRefBand("cp_Field_class", CONSTANT_Class);
|
|
1428 |
CPRefBand cp_Field_desc = cp_bands.newCPRefBand("cp_Field_desc", UDELTA5, CONSTANT_NameandType);
|
|
1429 |
CPRefBand cp_Method_class = cp_bands.newCPRefBand("cp_Method_class", CONSTANT_Class);
|
|
1430 |
CPRefBand cp_Method_desc = cp_bands.newCPRefBand("cp_Method_desc", UDELTA5, CONSTANT_NameandType);
|
|
1431 |
CPRefBand cp_Imethod_class = cp_bands.newCPRefBand("cp_Imethod_class", CONSTANT_Class);
|
|
1432 |
CPRefBand cp_Imethod_desc = cp_bands.newCPRefBand("cp_Imethod_desc", UDELTA5, CONSTANT_NameandType);
|
|
1433 |
|
|
1434 |
// bands for carrying attribute definitions:
|
|
1435 |
MultiBand attr_definition_bands = all_bands.newMultiBand("(attr_definition_bands)", UNSIGNED5);
|
|
1436 |
ByteBand attr_definition_headers = attr_definition_bands.newByteBand("attr_definition_headers");
|
|
1437 |
CPRefBand attr_definition_name = attr_definition_bands.newCPRefBand("attr_definition_name", CONSTANT_Utf8);
|
|
1438 |
CPRefBand attr_definition_layout = attr_definition_bands.newCPRefBand("attr_definition_layout", CONSTANT_Utf8);
|
|
1439 |
|
|
1440 |
// bands for hardwired InnerClasses attribute (shared across the package)
|
|
1441 |
MultiBand ic_bands = all_bands.newMultiBand("(ic_bands)", DELTA5);
|
|
1442 |
CPRefBand ic_this_class = ic_bands.newCPRefBand("ic_this_class", UDELTA5, CONSTANT_Class);
|
|
1443 |
IntBand ic_flags = ic_bands.newIntBand("ic_flags", UNSIGNED5);
|
|
1444 |
// These bands contain data only where flags sets ACC_IC_LONG_FORM:
|
|
1445 |
CPRefBand ic_outer_class = ic_bands.newCPRefBand("ic_outer_class", DELTA5, CONSTANT_Class, NULL_IS_OK);
|
|
1446 |
CPRefBand ic_name = ic_bands.newCPRefBand("ic_name", DELTA5, CONSTANT_Utf8, NULL_IS_OK);
|
|
1447 |
|
|
1448 |
// bands for carrying class schema information:
|
|
1449 |
MultiBand class_bands = all_bands.newMultiBand("(class_bands)", DELTA5);
|
|
1450 |
CPRefBand class_this = class_bands.newCPRefBand("class_this", CONSTANT_Class);
|
|
1451 |
CPRefBand class_super = class_bands.newCPRefBand("class_super", CONSTANT_Class);
|
|
1452 |
IntBand class_interface_count = class_bands.newIntBand("class_interface_count");
|
|
1453 |
CPRefBand class_interface = class_bands.newCPRefBand("class_interface", CONSTANT_Class);
|
|
1454 |
|
|
1455 |
// bands for class members
|
|
1456 |
IntBand class_field_count = class_bands.newIntBand("class_field_count");
|
|
1457 |
IntBand class_method_count = class_bands.newIntBand("class_method_count");
|
|
1458 |
|
|
1459 |
CPRefBand field_descr = class_bands.newCPRefBand("field_descr", CONSTANT_NameandType);
|
|
1460 |
MultiBand field_attr_bands = class_bands.newMultiBand("(field_attr_bands)", UNSIGNED5);
|
|
1461 |
IntBand field_flags_hi = field_attr_bands.newIntBand("field_flags_hi");
|
|
1462 |
IntBand field_flags_lo = field_attr_bands.newIntBand("field_flags_lo");
|
|
1463 |
IntBand field_attr_count = field_attr_bands.newIntBand("field_attr_count");
|
|
1464 |
IntBand field_attr_indexes = field_attr_bands.newIntBand("field_attr_indexes");
|
|
1465 |
IntBand field_attr_calls = field_attr_bands.newIntBand("field_attr_calls");
|
|
1466 |
|
|
1467 |
// bands for predefined field attributes
|
|
1468 |
CPRefBand field_ConstantValue_KQ = field_attr_bands.newCPRefBand("field_ConstantValue_KQ", CONSTANT_Literal);
|
|
1469 |
CPRefBand field_Signature_RS = field_attr_bands.newCPRefBand("field_Signature_RS", CONSTANT_Signature);
|
|
1470 |
MultiBand field_metadata_bands = field_attr_bands.newMultiBand("(field_metadata_bands)", UNSIGNED5);
|
|
1471 |
|
|
1472 |
CPRefBand method_descr = class_bands.newCPRefBand("method_descr", MDELTA5, CONSTANT_NameandType);
|
|
1473 |
MultiBand method_attr_bands = class_bands.newMultiBand("(method_attr_bands)", UNSIGNED5);
|
|
1474 |
IntBand method_flags_hi = method_attr_bands.newIntBand("method_flags_hi");
|
|
1475 |
IntBand method_flags_lo = method_attr_bands.newIntBand("method_flags_lo");
|
|
1476 |
IntBand method_attr_count = method_attr_bands.newIntBand("method_attr_count");
|
|
1477 |
IntBand method_attr_indexes = method_attr_bands.newIntBand("method_attr_indexes");
|
|
1478 |
IntBand method_attr_calls = method_attr_bands.newIntBand("method_attr_calls");
|
|
1479 |
// band for predefined method attributes
|
|
1480 |
IntBand method_Exceptions_N = method_attr_bands.newIntBand("method_Exceptions_N");
|
|
1481 |
CPRefBand method_Exceptions_RC = method_attr_bands.newCPRefBand("method_Exceptions_RC", CONSTANT_Class);
|
|
1482 |
CPRefBand method_Signature_RS = method_attr_bands.newCPRefBand("method_Signature_RS", CONSTANT_Signature);
|
|
1483 |
MultiBand method_metadata_bands = method_attr_bands.newMultiBand("(method_metadata_bands)", UNSIGNED5);
|
|
1484 |
|
|
1485 |
MultiBand class_attr_bands = class_bands.newMultiBand("(class_attr_bands)", UNSIGNED5);
|
|
1486 |
IntBand class_flags_hi = class_attr_bands.newIntBand("class_flags_hi");
|
|
1487 |
IntBand class_flags_lo = class_attr_bands.newIntBand("class_flags_lo");
|
|
1488 |
IntBand class_attr_count = class_attr_bands.newIntBand("class_attr_count");
|
|
1489 |
IntBand class_attr_indexes = class_attr_bands.newIntBand("class_attr_indexes");
|
|
1490 |
IntBand class_attr_calls = class_attr_bands.newIntBand("class_attr_calls");
|
|
1491 |
// band for predefined SourceFile and other class attributes
|
|
1492 |
CPRefBand class_SourceFile_RUN = class_attr_bands.newCPRefBand("class_SourceFile_RUN", UNSIGNED5, CONSTANT_Utf8, NULL_IS_OK);
|
|
1493 |
CPRefBand class_EnclosingMethod_RC = class_attr_bands.newCPRefBand("class_EnclosingMethod_RC", CONSTANT_Class);
|
|
1494 |
CPRefBand class_EnclosingMethod_RDN = class_attr_bands.newCPRefBand("class_EnclosingMethod_RDN", UNSIGNED5, CONSTANT_NameandType, NULL_IS_OK);
|
|
1495 |
CPRefBand class_Signature_RS = class_attr_bands.newCPRefBand("class_Signature_RS", CONSTANT_Signature);
|
|
1496 |
MultiBand class_metadata_bands = class_attr_bands.newMultiBand("(class_metadata_bands)", UNSIGNED5);
|
|
1497 |
IntBand class_InnerClasses_N = class_attr_bands.newIntBand("class_InnerClasses_N");
|
|
1498 |
CPRefBand class_InnerClasses_RC = class_attr_bands.newCPRefBand("class_InnerClasses_RC", CONSTANT_Class);
|
|
1499 |
IntBand class_InnerClasses_F = class_attr_bands.newIntBand("class_InnerClasses_F");
|
|
1500 |
CPRefBand class_InnerClasses_outer_RCN = class_attr_bands.newCPRefBand("class_InnerClasses_outer_RCN", UNSIGNED5, CONSTANT_Class, NULL_IS_OK);
|
|
1501 |
CPRefBand class_InnerClasses_name_RUN = class_attr_bands.newCPRefBand("class_InnerClasses_name_RUN", UNSIGNED5, CONSTANT_Utf8, NULL_IS_OK);
|
|
1502 |
IntBand class_ClassFile_version_minor_H = class_attr_bands.newIntBand("class_ClassFile_version_minor_H");
|
|
1503 |
IntBand class_ClassFile_version_major_H = class_attr_bands.newIntBand("class_ClassFile_version_major_H");
|
|
1504 |
|
|
1505 |
MultiBand code_bands = class_bands.newMultiBand("(code_bands)", UNSIGNED5);
|
|
1506 |
ByteBand code_headers = code_bands.newByteBand("code_headers"); //BYTE1
|
|
1507 |
IntBand code_max_stack = code_bands.newIntBand("code_max_stack", UNSIGNED5);
|
|
1508 |
IntBand code_max_na_locals = code_bands.newIntBand("code_max_na_locals", UNSIGNED5);
|
|
1509 |
IntBand code_handler_count = code_bands.newIntBand("code_handler_count", UNSIGNED5);
|
|
1510 |
IntBand code_handler_start_P = code_bands.newIntBand("code_handler_start_P", BCI5);
|
|
1511 |
IntBand code_handler_end_PO = code_bands.newIntBand("code_handler_end_PO", BRANCH5);
|
|
1512 |
IntBand code_handler_catch_PO = code_bands.newIntBand("code_handler_catch_PO", BRANCH5);
|
|
1513 |
CPRefBand code_handler_class_RCN = code_bands.newCPRefBand("code_handler_class_RCN", UNSIGNED5, CONSTANT_Class, NULL_IS_OK);
|
|
1514 |
|
|
1515 |
MultiBand code_attr_bands = class_bands.newMultiBand("(code_attr_bands)", UNSIGNED5);
|
|
1516 |
IntBand code_flags_hi = code_attr_bands.newIntBand("code_flags_hi");
|
|
1517 |
IntBand code_flags_lo = code_attr_bands.newIntBand("code_flags_lo");
|
|
1518 |
IntBand code_attr_count = code_attr_bands.newIntBand("code_attr_count");
|
|
1519 |
IntBand code_attr_indexes = code_attr_bands.newIntBand("code_attr_indexes");
|
|
1520 |
IntBand code_attr_calls = code_attr_bands.newIntBand("code_attr_calls");
|
|
1521 |
|
|
1522 |
MultiBand stackmap_bands = code_attr_bands.newMultiBand("StackMapTable_bands", UNSIGNED5);
|
|
1523 |
IntBand code_StackMapTable_N = stackmap_bands.newIntBand("code_StackMapTable_N");
|
|
1524 |
IntBand code_StackMapTable_frame_T = stackmap_bands.newIntBand("code_StackMapTable_frame_T",BYTE1);
|
|
1525 |
IntBand code_StackMapTable_local_N = stackmap_bands.newIntBand("code_StackMapTable_local_N");
|
|
1526 |
IntBand code_StackMapTable_stack_N = stackmap_bands.newIntBand("code_StackMapTable_stack_N");
|
|
1527 |
IntBand code_StackMapTable_offset = stackmap_bands.newIntBand("code_StackMapTable_offset", UNSIGNED5);
|
|
1528 |
IntBand code_StackMapTable_T = stackmap_bands.newIntBand("code_StackMapTable_T", BYTE1);
|
|
1529 |
CPRefBand code_StackMapTable_RC = stackmap_bands.newCPRefBand("code_StackMapTable_RC", CONSTANT_Class);
|
|
1530 |
IntBand code_StackMapTable_P = stackmap_bands.newIntBand("code_StackMapTable_P", BCI5);
|
|
1531 |
|
|
1532 |
// bands for predefined LineNumberTable attribute
|
|
1533 |
IntBand code_LineNumberTable_N = code_attr_bands.newIntBand("code_LineNumberTable_N");
|
|
1534 |
IntBand code_LineNumberTable_bci_P = code_attr_bands.newIntBand("code_LineNumberTable_bci_P", BCI5);
|
|
1535 |
IntBand code_LineNumberTable_line = code_attr_bands.newIntBand("code_LineNumberTable_line");
|
|
1536 |
|
|
1537 |
// bands for predefined LocalVariable{Type}Table attributes
|
|
1538 |
IntBand code_LocalVariableTable_N = code_attr_bands.newIntBand("code_LocalVariableTable_N");
|
|
1539 |
IntBand code_LocalVariableTable_bci_P = code_attr_bands.newIntBand("code_LocalVariableTable_bci_P", BCI5);
|
|
1540 |
IntBand code_LocalVariableTable_span_O = code_attr_bands.newIntBand("code_LocalVariableTable_span_O", BRANCH5);
|
|
1541 |
CPRefBand code_LocalVariableTable_name_RU = code_attr_bands.newCPRefBand("code_LocalVariableTable_name_RU", CONSTANT_Utf8);
|
|
1542 |
CPRefBand code_LocalVariableTable_type_RS = code_attr_bands.newCPRefBand("code_LocalVariableTable_type_RS", CONSTANT_Signature);
|
|
1543 |
IntBand code_LocalVariableTable_slot = code_attr_bands.newIntBand("code_LocalVariableTable_slot");
|
|
1544 |
IntBand code_LocalVariableTypeTable_N = code_attr_bands.newIntBand("code_LocalVariableTypeTable_N");
|
|
1545 |
IntBand code_LocalVariableTypeTable_bci_P = code_attr_bands.newIntBand("code_LocalVariableTypeTable_bci_P", BCI5);
|
|
1546 |
IntBand code_LocalVariableTypeTable_span_O = code_attr_bands.newIntBand("code_LocalVariableTypeTable_span_O", BRANCH5);
|
|
1547 |
CPRefBand code_LocalVariableTypeTable_name_RU = code_attr_bands.newCPRefBand("code_LocalVariableTypeTable_name_RU", CONSTANT_Utf8);
|
|
1548 |
CPRefBand code_LocalVariableTypeTable_type_RS = code_attr_bands.newCPRefBand("code_LocalVariableTypeTable_type_RS", CONSTANT_Signature);
|
|
1549 |
IntBand code_LocalVariableTypeTable_slot = code_attr_bands.newIntBand("code_LocalVariableTypeTable_slot");
|
|
1550 |
|
|
1551 |
// bands for bytecodes
|
|
1552 |
MultiBand bc_bands = all_bands.newMultiBand("(byte_codes)", UNSIGNED5);
|
|
1553 |
ByteBand bc_codes = bc_bands.newByteBand("bc_codes"); //BYTE1
|
|
1554 |
// remaining bands provide typed opcode fields required by the bc_codes
|
|
1555 |
|
|
1556 |
IntBand bc_case_count = bc_bands.newIntBand("bc_case_count"); // *switch
|
|
1557 |
IntBand bc_case_value = bc_bands.newIntBand("bc_case_value", DELTA5); // *switch
|
|
1558 |
ByteBand bc_byte = bc_bands.newByteBand("bc_byte"); //BYTE1 // bipush, iinc, *newarray
|
|
1559 |
IntBand bc_short = bc_bands.newIntBand("bc_short", DELTA5); // sipush, wide iinc
|
|
1560 |
IntBand bc_local = bc_bands.newIntBand("bc_local"); // *load, *store, iinc, ret
|
|
1561 |
IntBand bc_label = bc_bands.newIntBand("bc_label", BRANCH5); // if*, goto*, jsr*, *switch
|
|
1562 |
|
|
1563 |
// Most CP refs exhibit some correlation, and benefit from delta coding.
|
|
1564 |
// The notable exceptions are class and method references.
|
|
1565 |
|
|
1566 |
// ldc* operands:
|
|
1567 |
CPRefBand bc_intref = bc_bands.newCPRefBand("bc_intref", DELTA5, CONSTANT_Integer);
|
|
1568 |
CPRefBand bc_floatref = bc_bands.newCPRefBand("bc_floatref", DELTA5, CONSTANT_Float);
|
|
1569 |
CPRefBand bc_longref = bc_bands.newCPRefBand("bc_longref", DELTA5, CONSTANT_Long);
|
|
1570 |
CPRefBand bc_doubleref = bc_bands.newCPRefBand("bc_doubleref", DELTA5, CONSTANT_Double);
|
|
1571 |
CPRefBand bc_stringref = bc_bands.newCPRefBand("bc_stringref", DELTA5, CONSTANT_String);
|
|
1572 |
|
|
1573 |
// nulls produced by bc_classref are taken to mean the current class
|
|
1574 |
CPRefBand bc_classref = bc_bands.newCPRefBand("bc_classref", UNSIGNED5, CONSTANT_Class, NULL_IS_OK); // new, *anew*, c*cast, i*of, ldc
|
|
1575 |
CPRefBand bc_fieldref = bc_bands.newCPRefBand("bc_fieldref", DELTA5, CONSTANT_Fieldref); // get*, put*
|
|
1576 |
CPRefBand bc_methodref = bc_bands.newCPRefBand("bc_methodref", CONSTANT_Methodref); // invoke[vs]*
|
|
1577 |
CPRefBand bc_imethodref = bc_bands.newCPRefBand("bc_imethodref", DELTA5, CONSTANT_InterfaceMethodref); // invokeinterface
|
|
1578 |
|
|
1579 |
// _self_linker_op family
|
|
1580 |
CPRefBand bc_thisfield = bc_bands.newCPRefBand("bc_thisfield", CONSTANT_None); // any field within cur. class
|
|
1581 |
CPRefBand bc_superfield = bc_bands.newCPRefBand("bc_superfield", CONSTANT_None); // any field within superclass
|
|
1582 |
CPRefBand bc_thismethod = bc_bands.newCPRefBand("bc_thismethod", CONSTANT_None); // any method within cur. class
|
|
1583 |
CPRefBand bc_supermethod = bc_bands.newCPRefBand("bc_supermethod", CONSTANT_None); // any method within superclass
|
|
1584 |
// bc_invokeinit family:
|
|
1585 |
IntBand bc_initref = bc_bands.newIntBand("bc_initref");
|
|
1586 |
// escapes
|
|
1587 |
CPRefBand bc_escref = bc_bands.newCPRefBand("bc_escref", CONSTANT_All);
|
|
1588 |
IntBand bc_escrefsize = bc_bands.newIntBand("bc_escrefsize");
|
|
1589 |
IntBand bc_escsize = bc_bands.newIntBand("bc_escsize");
|
|
1590 |
ByteBand bc_escbyte = bc_bands.newByteBand("bc_escbyte");
|
|
1591 |
|
|
1592 |
// bands for carrying resource files and file attributes:
|
|
1593 |
MultiBand file_bands = all_bands.newMultiBand("(file_bands)", UNSIGNED5);
|
|
1594 |
CPRefBand file_name = file_bands.newCPRefBand("file_name", CONSTANT_Utf8);
|
|
1595 |
IntBand file_size_hi = file_bands.newIntBand("file_size_hi");
|
|
1596 |
IntBand file_size_lo = file_bands.newIntBand("file_size_lo");
|
|
1597 |
IntBand file_modtime = file_bands.newIntBand("file_modtime", DELTA5);
|
|
1598 |
IntBand file_options = file_bands.newIntBand("file_options");
|
|
1599 |
ByteBand file_bits = file_bands.newByteBand("file_bits");
|
|
1600 |
|
|
1601 |
// End of band definitions!
|
|
1602 |
|
|
1603 |
/** Given CP indexes, distribute tag-specific indexes to bands. */
|
|
1604 |
protected void setBandIndexes() {
|
|
1605 |
// Handle prior calls to setBandIndex:
|
|
1606 |
for (Iterator i = needPredefIndex.iterator(); i.hasNext(); ) {
|
|
1607 |
Object[] need = (Object[]) i.next();
|
|
1608 |
CPRefBand b = (CPRefBand) need[0];
|
|
1609 |
Byte which = (Byte) need[1];
|
|
1610 |
b.setIndex(getCPIndex(which.byteValue()));
|
|
1611 |
}
|
|
1612 |
needPredefIndex = null; // no more predefs
|
|
1613 |
|
|
1614 |
if (verbose > 3) {
|
|
1615 |
printCDecl(all_bands);
|
|
1616 |
}
|
|
1617 |
}
|
|
1618 |
|
|
1619 |
protected void setBandIndex(CPRefBand b, byte which) {
|
|
1620 |
Object[] need = { b, new Byte(which) };
|
|
1621 |
if (which == CONSTANT_Literal) {
|
|
1622 |
// I.e., attribute layouts KQ (no null) or KQN (null ok).
|
|
1623 |
allKQBands.add(b);
|
|
1624 |
} else if (needPredefIndex != null) {
|
|
1625 |
needPredefIndex.add(need);
|
|
1626 |
} else {
|
|
1627 |
// Not in predefinition mode; getCPIndex now works.
|
|
1628 |
b.setIndex(getCPIndex(which));
|
|
1629 |
}
|
|
1630 |
}
|
|
1631 |
|
|
1632 |
protected void setConstantValueIndex(Class.Field f) {
|
|
1633 |
Index ix = null;
|
|
1634 |
if (f != null) {
|
|
1635 |
byte tag = f.getLiteralTag();
|
|
1636 |
ix = getCPIndex(tag);
|
|
1637 |
if (verbose > 2)
|
|
1638 |
Utils.log.fine("setConstantValueIndex "+f+" "+ConstantPool.tagName(tag)+" => "+ix);
|
|
1639 |
assert(ix != null);
|
|
1640 |
}
|
|
1641 |
// Typically, allKQBands is the singleton of field_ConstantValue_KQ.
|
|
1642 |
for (Iterator i = allKQBands.iterator(); i.hasNext(); ) {
|
|
1643 |
CPRefBand xxx_KQ = (CPRefBand) i.next();
|
|
1644 |
xxx_KQ.setIndex(ix);
|
|
1645 |
}
|
|
1646 |
}
|
|
1647 |
|
|
1648 |
// Table of bands which contain metadata.
|
|
1649 |
protected MultiBand[] metadataBands = new MultiBand[ATTR_CONTEXT_LIMIT];
|
|
1650 |
{
|
|
1651 |
metadataBands[ATTR_CONTEXT_CLASS] = class_metadata_bands;
|
|
1652 |
metadataBands[ATTR_CONTEXT_FIELD] = field_metadata_bands;
|
|
1653 |
metadataBands[ATTR_CONTEXT_METHOD] = method_metadata_bands;
|
|
1654 |
}
|
|
1655 |
|
|
1656 |
// Attribute layouts.
|
|
1657 |
public static final int ADH_CONTEXT_MASK = 0x3; // (ad_hdr & ADH_CONTEXT_MASK)
|
|
1658 |
public static final int ADH_BIT_SHIFT = 0x2; // (ad_hdr >> ADH_BIT_SHIFT)
|
|
1659 |
public static final int ADH_BIT_IS_LSB = 1;
|
|
1660 |
public static final int ATTR_INDEX_OVERFLOW = -1;
|
|
1661 |
|
|
1662 |
public int[] attrIndexLimit = new int[ATTR_CONTEXT_LIMIT];
|
|
1663 |
// Each index limit is either 32 or 63, depending on AO_HAVE_XXX_FLAGS_HI.
|
|
1664 |
|
|
1665 |
// Which flag bits are taken over by attributes?
|
|
1666 |
protected long[] attrFlagMask = new long[ATTR_CONTEXT_LIMIT];
|
|
1667 |
// Which flag bits have been taken over explicitly?
|
|
1668 |
protected long[] attrDefSeen = new long[ATTR_CONTEXT_LIMIT];
|
|
1669 |
|
|
1670 |
// What pseudo-attribute bits are there to watch for?
|
|
1671 |
protected int[] attrOverflowMask = new int[ATTR_CONTEXT_LIMIT];
|
|
1672 |
protected int attrClassFileVersionMask;
|
|
1673 |
|
|
1674 |
// Mapping from Attribute.Layout to Band[] (layout element bands).
|
|
1675 |
protected HashMap attrBandTable = new HashMap();
|
|
1676 |
|
|
1677 |
// Well-known attributes:
|
|
1678 |
protected final Attribute.Layout attrCodeEmpty;
|
|
1679 |
protected final Attribute.Layout attrInnerClassesEmpty;
|
|
1680 |
protected final Attribute.Layout attrClassFileVersion;
|
|
1681 |
protected final Attribute.Layout attrConstantValue;
|
|
1682 |
|
|
1683 |
// Mapping from Attribute.Layout to Integer (inverse of attrDefs)
|
|
1684 |
HashMap attrIndexTable = new HashMap();
|
|
1685 |
|
|
1686 |
// Mapping from attribute index (<32 are flag bits) to attributes.
|
|
1687 |
protected ArrayList[] attrDefs = new ArrayList[ATTR_CONTEXT_LIMIT];
|
|
1688 |
{
|
|
1689 |
for (int i = 0; i < ATTR_CONTEXT_LIMIT; i++) {
|
|
1690 |
assert(attrIndexLimit[i] == 0);
|
|
1691 |
attrIndexLimit[i] = 32; // just for the sake of predefs.
|
|
1692 |
attrDefs[i] = new ArrayList(Collections.nCopies(attrIndexLimit[i], null));
|
|
1693 |
}
|
|
1694 |
|
|
1695 |
// Add predefined attribute definitions:
|
|
1696 |
attrInnerClassesEmpty =
|
|
1697 |
predefineAttribute(CLASS_ATTR_InnerClasses, ATTR_CONTEXT_CLASS, null,
|
|
1698 |
"InnerClasses", "");
|
|
1699 |
assert(attrInnerClassesEmpty == Package.attrInnerClassesEmpty);
|
|
1700 |
predefineAttribute(CLASS_ATTR_SourceFile, ATTR_CONTEXT_CLASS,
|
|
1701 |
new Band[] { class_SourceFile_RUN },
|
|
1702 |
"SourceFile", "RUNH");
|
|
1703 |
predefineAttribute(CLASS_ATTR_EnclosingMethod, ATTR_CONTEXT_CLASS,
|
|
1704 |
new Band[] {
|
|
1705 |
class_EnclosingMethod_RC,
|
|
1706 |
class_EnclosingMethod_RDN
|
|
1707 |
},
|
|
1708 |
"EnclosingMethod", "RCHRDNH");
|
|
1709 |
attrClassFileVersion =
|
|
1710 |
predefineAttribute(CLASS_ATTR_ClassFile_version, ATTR_CONTEXT_CLASS,
|
|
1711 |
new Band[] {
|
|
1712 |
class_ClassFile_version_minor_H,
|
|
1713 |
class_ClassFile_version_major_H
|
|
1714 |
},
|
|
1715 |
".ClassFile.version", "HH");
|
|
1716 |
predefineAttribute(X_ATTR_Signature, ATTR_CONTEXT_CLASS,
|
|
1717 |
new Band[] { class_Signature_RS },
|
|
1718 |
"Signature", "RSH");
|
|
1719 |
predefineAttribute(X_ATTR_Deprecated, ATTR_CONTEXT_CLASS, null,
|
|
1720 |
"Deprecated", "");
|
|
1721 |
//predefineAttribute(X_ATTR_Synthetic, ATTR_CONTEXT_CLASS, null,
|
|
1722 |
// "Synthetic", "");
|
|
1723 |
predefineAttribute(X_ATTR_OVERFLOW, ATTR_CONTEXT_CLASS, null,
|
|
1724 |
".Overflow", "");
|
|
1725 |
attrConstantValue =
|
|
1726 |
predefineAttribute(FIELD_ATTR_ConstantValue, ATTR_CONTEXT_FIELD,
|
|
1727 |
new Band[] { field_ConstantValue_KQ },
|
|
1728 |
"ConstantValue", "KQH");
|
|
1729 |
predefineAttribute(X_ATTR_Signature, ATTR_CONTEXT_FIELD,
|
|
1730 |
new Band[] { field_Signature_RS },
|
|
1731 |
"Signature", "RSH");
|
|
1732 |
predefineAttribute(X_ATTR_Deprecated, ATTR_CONTEXT_FIELD, null,
|
|
1733 |
"Deprecated", "");
|
|
1734 |
//predefineAttribute(X_ATTR_Synthetic, ATTR_CONTEXT_FIELD, null,
|
|
1735 |
// "Synthetic", "");
|
|
1736 |
predefineAttribute(X_ATTR_OVERFLOW, ATTR_CONTEXT_FIELD, null,
|
|
1737 |
".Overflow", "");
|
|
1738 |
attrCodeEmpty =
|
|
1739 |
predefineAttribute(METHOD_ATTR_Code, ATTR_CONTEXT_METHOD, null,
|
|
1740 |
"Code", "");
|
|
1741 |
predefineAttribute(METHOD_ATTR_Exceptions, ATTR_CONTEXT_METHOD,
|
|
1742 |
new Band[] {
|
|
1743 |
method_Exceptions_N,
|
|
1744 |
method_Exceptions_RC
|
|
1745 |
},
|
|
1746 |
"Exceptions", "NH[RCH]");
|
|
1747 |
assert(attrCodeEmpty == Package.attrCodeEmpty);
|
|
1748 |
predefineAttribute(X_ATTR_Signature, ATTR_CONTEXT_METHOD,
|
|
1749 |
new Band[] { method_Signature_RS },
|
|
1750 |
"Signature", "RSH");
|
|
1751 |
predefineAttribute(X_ATTR_Deprecated, ATTR_CONTEXT_METHOD, null,
|
|
1752 |
"Deprecated", "");
|
|
1753 |
//predefineAttribute(X_ATTR_Synthetic, ATTR_CONTEXT_METHOD, null,
|
|
1754 |
// "Synthetic", "");
|
|
1755 |
predefineAttribute(X_ATTR_OVERFLOW, ATTR_CONTEXT_METHOD, null,
|
|
1756 |
".Overflow", "");
|
|
1757 |
|
|
1758 |
for (int ctype = 0; ctype < ATTR_CONTEXT_LIMIT; ctype++) {
|
|
1759 |
MultiBand xxx_metadata_bands = metadataBands[ctype];
|
|
1760 |
if (xxx_metadata_bands == null)
|
|
1761 |
continue; // no code attrs
|
|
1762 |
|
|
1763 |
// These arguments cause the bands to be built
|
|
1764 |
// automatically for this complicated layout:
|
|
1765 |
predefineAttribute(X_ATTR_RuntimeVisibleAnnotations,
|
|
1766 |
ATTR_CONTEXT_NAME[ctype]+"_RVA_",
|
|
1767 |
xxx_metadata_bands,
|
|
1768 |
Attribute.lookup(null, ctype,
|
|
1769 |
"RuntimeVisibleAnnotations"));
|
|
1770 |
predefineAttribute(X_ATTR_RuntimeInvisibleAnnotations,
|
|
1771 |
ATTR_CONTEXT_NAME[ctype]+"_RIA_",
|
|
1772 |
xxx_metadata_bands,
|
|
1773 |
Attribute.lookup(null, ctype,
|
|
1774 |
"RuntimeInvisibleAnnotations"));
|
|
1775 |
if (ctype != ATTR_CONTEXT_METHOD)
|
|
1776 |
continue;
|
|
1777 |
|
|
1778 |
predefineAttribute(METHOD_ATTR_RuntimeVisibleParameterAnnotations,
|
|
1779 |
"method_RVPA_", xxx_metadata_bands,
|
|
1780 |
Attribute.lookup(null, ctype,
|
|
1781 |
"RuntimeVisibleParameterAnnotations"));
|
|
1782 |
predefineAttribute(METHOD_ATTR_RuntimeInvisibleParameterAnnotations,
|
|
1783 |
"method_RIPA_", xxx_metadata_bands,
|
|
1784 |
Attribute.lookup(null, ctype,
|
|
1785 |
"RuntimeInvisibleParameterAnnotations"));
|
|
1786 |
predefineAttribute(METHOD_ATTR_AnnotationDefault,
|
|
1787 |
"method_AD_", xxx_metadata_bands,
|
|
1788 |
Attribute.lookup(null, ctype,
|
|
1789 |
"AnnotationDefault"));
|
|
1790 |
}
|
|
1791 |
|
|
1792 |
|
|
1793 |
Attribute.Layout stackMapDef = Attribute.lookup(null, ATTR_CONTEXT_CODE, "StackMapTable").layout();
|
|
1794 |
predefineAttribute(CODE_ATTR_StackMapTable, ATTR_CONTEXT_CODE,
|
|
1795 |
stackmap_bands.toArray(),
|
|
1796 |
stackMapDef.name(), stackMapDef.layout());
|
|
1797 |
|
|
1798 |
predefineAttribute(CODE_ATTR_LineNumberTable, ATTR_CONTEXT_CODE,
|
|
1799 |
new Band[] {
|
|
1800 |
code_LineNumberTable_N,
|
|
1801 |
code_LineNumberTable_bci_P,
|
|
1802 |
code_LineNumberTable_line
|
|
1803 |
},
|
|
1804 |
"LineNumberTable", "NH[PHH]");
|
|
1805 |
predefineAttribute(CODE_ATTR_LocalVariableTable, ATTR_CONTEXT_CODE,
|
|
1806 |
new Band[] {
|
|
1807 |
code_LocalVariableTable_N,
|
|
1808 |
code_LocalVariableTable_bci_P,
|
|
1809 |
code_LocalVariableTable_span_O,
|
|
1810 |
code_LocalVariableTable_name_RU,
|
|
1811 |
code_LocalVariableTable_type_RS,
|
|
1812 |
code_LocalVariableTable_slot
|
|
1813 |
},
|
|
1814 |
"LocalVariableTable", "NH[PHOHRUHRSHH]");
|
|
1815 |
predefineAttribute(CODE_ATTR_LocalVariableTypeTable, ATTR_CONTEXT_CODE,
|
|
1816 |
new Band[] {
|
|
1817 |
code_LocalVariableTypeTable_N,
|
|
1818 |
code_LocalVariableTypeTable_bci_P,
|
|
1819 |
code_LocalVariableTypeTable_span_O,
|
|
1820 |
code_LocalVariableTypeTable_name_RU,
|
|
1821 |
code_LocalVariableTypeTable_type_RS,
|
|
1822 |
code_LocalVariableTypeTable_slot
|
|
1823 |
},
|
|
1824 |
"LocalVariableTypeTable", "NH[PHOHRUHRSHH]");
|
|
1825 |
predefineAttribute(X_ATTR_OVERFLOW, ATTR_CONTEXT_CODE, null,
|
|
1826 |
".Overflow", "");
|
|
1827 |
|
|
1828 |
// Clear the record of having seen these definitions,
|
|
1829 |
// so they may be redefined without error.
|
|
1830 |
for (int i = 0; i < ATTR_CONTEXT_LIMIT; i++) {
|
|
1831 |
attrDefSeen[i] = 0;
|
|
1832 |
}
|
|
1833 |
|
|
1834 |
// Set up the special masks:
|
|
1835 |
for (int i = 0; i < ATTR_CONTEXT_LIMIT; i++) {
|
|
1836 |
attrOverflowMask[i] = (1<<X_ATTR_OVERFLOW);
|
|
1837 |
attrIndexLimit[i] = 0; // will make a final decision later
|
|
1838 |
}
|
|
1839 |
attrClassFileVersionMask = (1<<CLASS_ATTR_ClassFile_version);
|
|
1840 |
}
|
|
1841 |
|
|
1842 |
private void adjustToMajver() {
|
|
1843 |
if (getPackageMajver() < JAVA6_PACKAGE_MAJOR_VERSION) {
|
|
1844 |
if (verbose > 0) Utils.log.fine("Legacy package version");
|
|
1845 |
// Revoke definition of pre-1.6 attribute type.
|
|
1846 |
undefineAttribute(CODE_ATTR_StackMapTable, ATTR_CONTEXT_CODE);
|
|
1847 |
}
|
|
1848 |
}
|
|
1849 |
|
|
1850 |
protected void initAttrIndexLimit() {
|
|
1851 |
for (int i = 0; i < ATTR_CONTEXT_LIMIT; i++) {
|
|
1852 |
assert(attrIndexLimit[i] == 0); // decide on it now!
|
|
1853 |
attrIndexLimit[i] = (haveFlagsHi(i)? 63: 32);
|
|
1854 |
assert(attrDefs[i].size() == 32); // all predef indexes are <32
|
|
1855 |
int addMore = attrIndexLimit[i] - attrDefs[i].size();
|
|
1856 |
attrDefs[i].addAll(Collections.nCopies(addMore, null));
|
|
1857 |
}
|
|
1858 |
}
|
|
1859 |
|
|
1860 |
protected boolean haveFlagsHi(int ctype) {
|
|
1861 |
int mask = 1<<(LG_AO_HAVE_XXX_FLAGS_HI+ctype);
|
|
1862 |
switch (ctype) {
|
|
1863 |
case ATTR_CONTEXT_CLASS:
|
|
1864 |
assert(mask == AO_HAVE_CLASS_FLAGS_HI); break;
|
|
1865 |
case ATTR_CONTEXT_FIELD:
|
|
1866 |
assert(mask == AO_HAVE_FIELD_FLAGS_HI); break;
|
|
1867 |
case ATTR_CONTEXT_METHOD:
|
|
1868 |
assert(mask == AO_HAVE_METHOD_FLAGS_HI); break;
|
|
1869 |
case ATTR_CONTEXT_CODE:
|
|
1870 |
assert(mask == AO_HAVE_CODE_FLAGS_HI); break;
|
|
1871 |
default:
|
|
1872 |
assert(false);
|
|
1873 |
}
|
|
1874 |
return testBit(archiveOptions, mask);
|
|
1875 |
}
|
|
1876 |
|
|
1877 |
protected ArrayList getPredefinedAttrs(int ctype) {
|
|
1878 |
assert(attrIndexLimit[ctype] != 0);
|
|
1879 |
ArrayList res = new ArrayList(attrIndexLimit[ctype]);
|
|
1880 |
// Remove nulls and non-predefs.
|
|
1881 |
for (int ai = 0; ai < attrIndexLimit[ctype]; ai++) {
|
|
1882 |
if (testBit(attrDefSeen[ctype], 1L<<ai)) continue;
|
|
1883 |
Attribute.Layout def = (Attribute.Layout) attrDefs[ctype].get(ai);
|
|
1884 |
if (def == null) continue; // unused flag bit
|
|
1885 |
assert(isPredefinedAttr(ctype, ai));
|
|
1886 |
res.add(def);
|
|
1887 |
}
|
|
1888 |
return res;
|
|
1889 |
}
|
|
1890 |
|
|
1891 |
protected boolean isPredefinedAttr(int ctype, int ai) {
|
|
1892 |
assert(attrIndexLimit[ctype] != 0);
|
|
1893 |
// Overflow attrs are never predefined.
|
|
1894 |
if (ai >= attrIndexLimit[ctype]) return false;
|
|
1895 |
// If the bit is set, it was explicitly def'd.
|
|
1896 |
if (testBit(attrDefSeen[ctype], 1L<<ai)) return false;
|
|
1897 |
return (attrDefs[ctype].get(ai) != null);
|
|
1898 |
}
|
|
1899 |
|
|
1900 |
protected void adjustSpecialAttrMasks() {
|
|
1901 |
// Clear special masks if new definitions have been seen for them.
|
|
1902 |
attrClassFileVersionMask &= ~ attrDefSeen[ATTR_CONTEXT_CLASS];
|
|
1903 |
// It is possible to clear the overflow mask (bit 16).
|
|
1904 |
for (int i = 0; i < ATTR_CONTEXT_LIMIT; i++) {
|
|
1905 |
attrOverflowMask[i] &= ~ attrDefSeen[i];
|
|
1906 |
}
|
|
1907 |
}
|
|
1908 |
|
|
1909 |
protected Attribute makeClassFileVersionAttr(int minver, int majver) {
|
|
1910 |
byte[] bytes = {
|
|
1911 |
(byte)(minver >> 8), (byte)minver,
|
|
1912 |
(byte)(majver >> 8), (byte)majver
|
|
1913 |
};
|
|
1914 |
return attrClassFileVersion.addContent(bytes);
|
|
1915 |
}
|
|
1916 |
|
|
1917 |
protected short[] parseClassFileVersionAttr(Attribute attr) {
|
|
1918 |
assert(attr.layout() == attrClassFileVersion);
|
|
1919 |
assert(attr.size() == 4);
|
|
1920 |
byte[] bytes = attr.bytes();
|
|
1921 |
int minver = ((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xFF);
|
|
1922 |
int majver = ((bytes[2] & 0xFF) << 8) | (bytes[3] & 0xFF);
|
|
1923 |
return new short[]{ (short) minver, (short) majver };
|
|
1924 |
}
|
|
1925 |
|
|
1926 |
private boolean assertBandOKForElems(Band[] ab, Attribute.Layout.Element[] elems) {
|
|
1927 |
for (int i = 0; i < elems.length; i++) {
|
|
1928 |
assert(assertBandOKForElem(ab, elems[i]));
|
|
1929 |
}
|
|
1930 |
return true;
|
|
1931 |
}
|
|
1932 |
private boolean assertBandOKForElem(Band[] ab, Attribute.Layout.Element e) {
|
|
1933 |
Band b = null;
|
|
1934 |
if (e.bandIndex != Attribute.NO_BAND_INDEX)
|
|
1935 |
b = ab[e.bandIndex];
|
|
1936 |
Coding rc = UNSIGNED5;
|
|
1937 |
boolean wantIntBand = true;
|
|
1938 |
switch (e.kind) {
|
|
1939 |
case Attribute.EK_INT:
|
|
1940 |
if (e.flagTest(Attribute.EF_SIGN)) {
|
|
1941 |
rc = SIGNED5;
|
|
1942 |
} else if (e.len == 1) {
|
|
1943 |
rc = BYTE1;
|
|
1944 |
}
|
|
1945 |
break;
|
|
1946 |
case Attribute.EK_BCI:
|
|
1947 |
if (!e.flagTest(Attribute.EF_DELTA)) {
|
|
1948 |
rc = BCI5;
|
|
1949 |
} else {
|
|
1950 |
rc = BRANCH5;
|
|
1951 |
}
|
|
1952 |
break;
|
|
1953 |
case Attribute.EK_BCO:
|
|
1954 |
rc = BRANCH5;
|
|
1955 |
break;
|
|
1956 |
case Attribute.EK_FLAG:
|
|
1957 |
if (e.len == 1) rc = BYTE1;
|
|
1958 |
break;
|
|
1959 |
case Attribute.EK_REPL:
|
|
1960 |
if (e.len == 1) rc = BYTE1;
|
|
1961 |
assertBandOKForElems(ab, e.body);
|
|
1962 |
break;
|
|
1963 |
case Attribute.EK_UN:
|
|
1964 |
if (e.flagTest(Attribute.EF_SIGN)) {
|
|
1965 |
rc = SIGNED5;
|
|
1966 |
} else if (e.len == 1) {
|
|
1967 |
rc = BYTE1;
|
|
1968 |
}
|
|
1969 |
assertBandOKForElems(ab, e.body);
|
|
1970 |
break;
|
|
1971 |
case Attribute.EK_CASE:
|
|
1972 |
assert(b == null);
|
|
1973 |
assertBandOKForElems(ab, e.body);
|
|
1974 |
return true; // no direct band
|
|
1975 |
case Attribute.EK_CALL:
|
|
1976 |
assert(b == null);
|
|
1977 |
return true; // no direct band
|
|
1978 |
case Attribute.EK_CBLE:
|
|
1979 |
assert(b == null);
|
|
1980 |
assertBandOKForElems(ab, e.body);
|
|
1981 |
return true; // no direct band
|
|
1982 |
case Attribute.EK_REF:
|
|
1983 |
wantIntBand = false;
|
|
1984 |
assert(b instanceof CPRefBand);
|
|
1985 |
assert(((CPRefBand)b).nullOK == e.flagTest(Attribute.EF_NULL));
|
|
1986 |
break;
|
|
1987 |
default: assert(false);
|
|
1988 |
}
|
|
1989 |
assert(b.regularCoding == rc)
|
|
1990 |
: (e+" // "+b);
|
|
1991 |
if (wantIntBand)
|
|
1992 |
assert(b instanceof IntBand);
|
|
1993 |
return true;
|
|
1994 |
}
|
|
1995 |
|
|
1996 |
private
|
|
1997 |
Attribute.Layout predefineAttribute(int index, int ctype, Band[] ab,
|
|
1998 |
String name, String layout) {
|
|
1999 |
// Use Attribute.find to get uniquification of layouts.
|
|
2000 |
Attribute.Layout def = Attribute.find(ctype, name, layout).layout();
|
|
2001 |
//def.predef = true;
|
|
2002 |
if (index >= 0) {
|
|
2003 |
setAttributeLayoutIndex(def, index);
|
|
2004 |
}
|
|
2005 |
if (ab == null) {
|
|
2006 |
ab = new Band[0];
|
|
2007 |
}
|
|
2008 |
assert(attrBandTable.get(def) == null); // no redef
|
|
2009 |
attrBandTable.put(def, ab);
|
|
2010 |
assert(def.bandCount == ab.length)
|
|
2011 |
: (def+" // "+Arrays.asList(ab));
|
|
2012 |
// Let's make sure the band types match:
|
|
2013 |
assert(assertBandOKForElems(ab, def.elems));
|
|
2014 |
return def;
|
|
2015 |
}
|
|
2016 |
|
|
2017 |
// This version takes bandPrefix/addHere instead of prebuilt Band[] ab.
|
|
2018 |
private
|
|
2019 |
Attribute.Layout predefineAttribute(int index,
|
|
2020 |
String bandPrefix, MultiBand addHere,
|
|
2021 |
Attribute attr) {
|
|
2022 |
//Attribute.Layout def = Attribute.find(ctype, name, layout).layout();
|
|
2023 |
Attribute.Layout def = attr.layout();
|
|
2024 |
int ctype = def.ctype();
|
|
2025 |
return predefineAttribute(index, ctype,
|
|
2026 |
makeNewAttributeBands(bandPrefix, def,
|
|
2027 |
addHere),
|
|
2028 |
def.name(), def.layout());
|
|
2029 |
}
|
|
2030 |
|
|
2031 |
private
|
|
2032 |
void undefineAttribute(int index, int ctype) {
|
|
2033 |
if (verbose > 1) {
|
|
2034 |
System.out.println("Removing predefined "+ATTR_CONTEXT_NAME[ctype]+
|
|
2035 |
" attribute on bit "+index);
|
|
2036 |
}
|
|
2037 |
List defList = attrDefs[ctype];
|
|
2038 |
Attribute.Layout def = (Attribute.Layout) defList.get(index);
|
|
2039 |
assert(def != null);
|
|
2040 |
defList.set(index, null);
|
|
2041 |
attrIndexTable.put(def, null);
|
|
2042 |
// Clear the def bit. (For predefs, it's already clear.)
|
|
2043 |
assert(index < 64);
|
|
2044 |
attrDefSeen[ctype] &= ~(1L<<index);
|
|
2045 |
attrFlagMask[ctype] &= ~(1L<<index);
|
|
2046 |
Band[] ab = (Band[]) attrBandTable.get(def);
|
|
2047 |
for (int j = 0; j < ab.length; j++) {
|
|
2048 |
ab[j].doneWithUnusedBand();
|
|
2049 |
}
|
|
2050 |
}
|
|
2051 |
|
|
2052 |
// Bands which contain non-predefined attrs.
|
|
2053 |
protected MultiBand[] attrBands = new MultiBand[ATTR_CONTEXT_LIMIT];
|
|
2054 |
{
|
|
2055 |
attrBands[ATTR_CONTEXT_CLASS] = class_attr_bands;
|
|
2056 |
attrBands[ATTR_CONTEXT_FIELD] = field_attr_bands;
|
|
2057 |
attrBands[ATTR_CONTEXT_METHOD] = method_attr_bands;
|
|
2058 |
attrBands[ATTR_CONTEXT_CODE] = code_attr_bands;
|
|
2059 |
}
|
|
2060 |
|
|
2061 |
// Create bands for all non-predefined attrs.
|
|
2062 |
void makeNewAttributeBands() {
|
|
2063 |
// Retract special flag bit bindings, if they were taken over.
|
|
2064 |
adjustSpecialAttrMasks();
|
|
2065 |
|
|
2066 |
for (int ctype = 0; ctype < ATTR_CONTEXT_LIMIT; ctype++) {
|
|
2067 |
String cname = ATTR_CONTEXT_NAME[ctype];
|
|
2068 |
MultiBand xxx_attr_bands = attrBands[ctype];
|
|
2069 |
long defSeen = attrDefSeen[ctype];
|
|
2070 |
// Note: attrDefSeen is always a subset of attrFlagMask.
|
|
2071 |
assert((defSeen & ~attrFlagMask[ctype]) == 0);
|
|
2072 |
for (int i = 0; i < attrDefs[ctype].size(); i++) {
|
|
2073 |
Attribute.Layout def = (Attribute.Layout)
|
|
2074 |
attrDefs[ctype].get(i);
|
|
2075 |
if (def == null) continue; // unused flag bit
|
|
2076 |
if (def.bandCount == 0) continue; // empty attr
|
|
2077 |
if (i < attrIndexLimit[ctype] && !testBit(defSeen, 1L<<i)) {
|
|
2078 |
// There are already predefined bands here.
|
|
2079 |
assert(attrBandTable.get(def) != null);
|
|
2080 |
continue;
|
|
2081 |
}
|
|
2082 |
int base = xxx_attr_bands.size();
|
|
2083 |
String pfx = cname+"_"+def.name()+"_"; // debug only
|
|
2084 |
if (verbose > 1)
|
|
2085 |
Utils.log.fine("Making new bands for "+def);
|
|
2086 |
Band[] newAB = makeNewAttributeBands(pfx, def,
|
|
2087 |
xxx_attr_bands);
|
|
2088 |
assert(newAB.length == def.bandCount);
|
|
2089 |
Band[] prevAB = (Band[]) attrBandTable.put(def, newAB);
|
|
2090 |
if (prevAB != null) {
|
|
2091 |
// We won't be using these predefined bands.
|
|
2092 |
for (int j = 0; j < prevAB.length; j++) {
|
|
2093 |
prevAB[j].doneWithUnusedBand();
|
|
2094 |
}
|
|
2095 |
}
|
|
2096 |
}
|
|
2097 |
}
|
|
2098 |
//System.out.println(prevForAssertMap);
|
|
2099 |
}
|
|
2100 |
private
|
|
2101 |
Band[] makeNewAttributeBands(String pfx, Attribute.Layout def,
|
|
2102 |
MultiBand addHere) {
|
|
2103 |
int base = addHere.size();
|
|
2104 |
makeNewAttributeBands(pfx, def.elems, addHere);
|
|
2105 |
int nb = addHere.size() - base;
|
|
2106 |
Band[] newAB = new Band[nb];
|
|
2107 |
for (int i = 0; i < nb; i++) {
|
|
2108 |
newAB[i] = addHere.get(base+i);
|
|
2109 |
}
|
|
2110 |
return newAB;
|
|
2111 |
}
|
|
2112 |
// Recursive helper, operates on a "body" or other sequence of elems:
|
|
2113 |
private
|
|
2114 |
void makeNewAttributeBands(String pfx, Attribute.Layout.Element[] elems,
|
|
2115 |
MultiBand ab) {
|
|
2116 |
for (int i = 0; i < elems.length; i++) {
|
|
2117 |
Attribute.Layout.Element e = elems[i];
|
|
2118 |
String name = pfx+ab.size()+"_"+e.layout;
|
|
2119 |
{
|
|
2120 |
int tem;
|
|
2121 |
if ((tem = name.indexOf('[')) > 0)
|
|
2122 |
name = name.substring(0, tem);
|
|
2123 |
if ((tem = name.indexOf('(')) > 0)
|
|
2124 |
name = name.substring(0, tem);
|
|
2125 |
if (name.endsWith("H"))
|
|
2126 |
name = name.substring(0, name.length()-1);
|
|
2127 |
}
|
|
2128 |
Band nb;
|
|
2129 |
switch (e.kind) {
|
|
2130 |
case Attribute.EK_INT:
|
|
2131 |
nb = newElemBand(e, name, ab);
|
|
2132 |
break;
|
|
2133 |
case Attribute.EK_BCI:
|
|
2134 |
if (!e.flagTest(Attribute.EF_DELTA)) {
|
|
2135 |
// PH: transmit R(bci), store bci
|
|
2136 |
nb = ab.newIntBand(name, BCI5);
|
|
2137 |
} else {
|
|
2138 |
// POH: transmit D(R(bci)), store bci
|
|
2139 |
nb = ab.newIntBand(name, BRANCH5);
|
|
2140 |
}
|
|
2141 |
// Note: No case for BYTE1 here.
|
|
2142 |
break;
|
|
2143 |
case Attribute.EK_BCO:
|
|
2144 |
// OH: transmit D(R(bci)), store D(bci)
|
|
2145 |
nb = ab.newIntBand(name, BRANCH5);
|
|
2146 |
// Note: No case for BYTE1 here.
|
|
2147 |
break;
|
|
2148 |
case Attribute.EK_FLAG:
|
|
2149 |
assert(!e.flagTest(Attribute.EF_SIGN));
|
|
2150 |
nb = newElemBand(e, name, ab);
|
|
2151 |
break;
|
|
2152 |
case Attribute.EK_REPL:
|
|
2153 |
assert(!e.flagTest(Attribute.EF_SIGN));
|
|
2154 |
nb = newElemBand(e, name, ab);
|
|
2155 |
makeNewAttributeBands(pfx, e.body, ab);
|
|
2156 |
break;
|
|
2157 |
case Attribute.EK_UN:
|
|
2158 |
nb = newElemBand(e, name, ab);
|
|
2159 |
makeNewAttributeBands(pfx, e.body, ab);
|
|
2160 |
break;
|
|
2161 |
case Attribute.EK_CASE:
|
|
2162 |
if (!e.flagTest(Attribute.EF_BACK)) {
|
|
2163 |
// If it's not a duplicate body, make the bands.
|
|
2164 |
makeNewAttributeBands(pfx, e.body, ab);
|
|
2165 |
}
|
|
2166 |
continue; // no new band to make
|
|
2167 |
case Attribute.EK_REF:
|
|
2168 |
byte refKind = e.refKind;
|
|
2169 |
boolean nullOK = e.flagTest(Attribute.EF_NULL);
|
|
2170 |
nb = ab.newCPRefBand(name, UNSIGNED5, refKind, nullOK);
|
|
2171 |
// Note: No case for BYTE1 here.
|
|
2172 |
break;
|
|
2173 |
case Attribute.EK_CALL:
|
|
2174 |
continue; // no new band to make
|
|
2175 |
case Attribute.EK_CBLE:
|
|
2176 |
makeNewAttributeBands(pfx, e.body, ab);
|
|
2177 |
continue; // no new band to make
|
|
2178 |
default: assert(false); continue;
|
|
2179 |
}
|
|
2180 |
if (verbose > 1) {
|
|
2181 |
Utils.log.fine("New attribute band "+nb);
|
|
2182 |
}
|
|
2183 |
}
|
|
2184 |
}
|
|
2185 |
private
|
|
2186 |
Band newElemBand(Attribute.Layout.Element e, String name, MultiBand ab) {
|
|
2187 |
if (e.flagTest(Attribute.EF_SIGN)) {
|
|
2188 |
return ab.newIntBand(name, SIGNED5);
|
|
2189 |
} else if (e.len == 1) {
|
|
2190 |
return ab.newIntBand(name, BYTE1); // Not ByteBand, please.
|
|
2191 |
} else {
|
|
2192 |
return ab.newIntBand(name, UNSIGNED5);
|
|
2193 |
}
|
|
2194 |
}
|
|
2195 |
|
|
2196 |
protected int setAttributeLayoutIndex(Attribute.Layout def, int index) {
|
|
2197 |
int ctype = def.ctype;
|
|
2198 |
assert(ATTR_INDEX_OVERFLOW <= index && index < attrIndexLimit[ctype]);
|
|
2199 |
List defList = attrDefs[ctype];
|
|
2200 |
if (index == ATTR_INDEX_OVERFLOW) {
|
|
2201 |
// Overflow attribute.
|
|
2202 |
index = defList.size();
|
|
2203 |
defList.add(def);
|
|
2204 |
if (verbose > 0)
|
|
2205 |
Utils.log.info("Adding new attribute at "+def +": "+index);
|
|
2206 |
attrIndexTable.put(def, new Integer(index));
|
|
2207 |
return index;
|
|
2208 |
}
|
|
2209 |
|
|
2210 |
// Detect redefinitions:
|
|
2211 |
if (testBit(attrDefSeen[ctype], 1L<<index)) {
|
|
2212 |
throw new RuntimeException("Multiple explicit definition at "+index+": "+def);
|
|
2213 |
}
|
|
2214 |
attrDefSeen[ctype] |= (1L<<index);
|
|
2215 |
|
|
2216 |
// Adding a new fixed attribute.
|
|
2217 |
assert(0 <= index && index < attrIndexLimit[ctype]);
|
|
2218 |
if (verbose > (attrClassFileVersionMask == 0? 2:0))
|
|
2219 |
Utils.log.fine("Fixing new attribute at "+index
|
|
2220 |
+": "+def
|
|
2221 |
+(defList.get(index) == null? "":
|
|
2222 |
"; replacing "+defList.get(index)));
|
|
2223 |
attrFlagMask[ctype] |= (1L<<index);
|
|
2224 |
// Remove index binding of any previous fixed attr.
|
|
2225 |
attrIndexTable.put(defList.get(index), null);
|
|
2226 |
defList.set(index, def);
|
|
2227 |
attrIndexTable.put(def, new Integer(index));
|
|
2228 |
return index;
|
|
2229 |
}
|
|
2230 |
|
|
2231 |
// encodings found in the code_headers band
|
|
2232 |
private static final int[][] shortCodeLimits = {
|
|
2233 |
{ 12, 12 }, // s<12, l<12, e=0 [1..144]
|
|
2234 |
{ 8, 8 }, // s<8, l<8, e=1 [145..208]
|
|
2235 |
{ 7, 7 }, // s<7, l<7, e=2 [209..256]
|
|
2236 |
};
|
|
2237 |
public final int shortCodeHeader_h_limit = shortCodeLimits.length;
|
|
2238 |
|
|
2239 |
// return 0 if it won't encode, else a number in [1..255]
|
|
2240 |
static int shortCodeHeader(Code code) {
|
|
2241 |
int s = code.max_stack;
|
|
2242 |
int l0 = code.max_locals;
|
|
2243 |
int h = code.handler_class.length;
|
|
2244 |
if (h >= shortCodeLimits.length) return LONG_CODE_HEADER;
|
|
2245 |
int siglen = code.getMethod().getArgumentSize();
|
|
2246 |
assert(l0 >= siglen); // enough locals for signature!
|
|
2247 |
if (l0 < siglen) return LONG_CODE_HEADER;
|
|
2248 |
int l1 = l0 - siglen; // do not count locals required by the signature
|
|
2249 |
int lims = shortCodeLimits[h][0];
|
|
2250 |
int liml = shortCodeLimits[h][1];
|
|
2251 |
if (s >= lims || l1 >= liml) return LONG_CODE_HEADER;
|
|
2252 |
int sc = shortCodeHeader_h_base(h);
|
|
2253 |
sc += s + lims*l1;
|
|
2254 |
if (sc > 255) return LONG_CODE_HEADER;
|
|
2255 |
assert(shortCodeHeader_max_stack(sc) == s);
|
|
2256 |
assert(shortCodeHeader_max_na_locals(sc) == l1);
|
|
2257 |
assert(shortCodeHeader_handler_count(sc) == h);
|
|
2258 |
return sc;
|
|
2259 |
}
|
|
2260 |
|
|
2261 |
static final int LONG_CODE_HEADER = 0;
|
|
2262 |
static int shortCodeHeader_handler_count(int sc) {
|
|
2263 |
assert(sc > 0 && sc <= 255);
|
|
2264 |
for (int h = 0; ; h++) {
|
|
2265 |
if (sc < shortCodeHeader_h_base(h+1))
|
|
2266 |
return h;
|
|
2267 |
}
|
|
2268 |
}
|
|
2269 |
static int shortCodeHeader_max_stack(int sc) {
|
|
2270 |
int h = shortCodeHeader_handler_count(sc);
|
|
2271 |
int lims = shortCodeLimits[h][0];
|
|
2272 |
return (sc - shortCodeHeader_h_base(h)) % lims;
|
|
2273 |
}
|
|
2274 |
static int shortCodeHeader_max_na_locals(int sc) {
|
|
2275 |
int h = shortCodeHeader_handler_count(sc);
|
|
2276 |
int lims = shortCodeLimits[h][0];
|
|
2277 |
return (sc - shortCodeHeader_h_base(h)) / lims;
|
|
2278 |
}
|
|
2279 |
|
|
2280 |
private static int shortCodeHeader_h_base(int h) {
|
|
2281 |
assert(h <= shortCodeLimits.length);
|
|
2282 |
int sc = 1;
|
|
2283 |
for (int h0 = 0; h0 < h; h0++) {
|
|
2284 |
int lims = shortCodeLimits[h0][0];
|
|
2285 |
int liml = shortCodeLimits[h0][1];
|
|
2286 |
sc += lims * liml;
|
|
2287 |
}
|
|
2288 |
return sc;
|
|
2289 |
}
|
|
2290 |
|
|
2291 |
// utilities for accessing the bc_label band:
|
|
2292 |
protected void putLabel(IntBand bc_label, Code c, int pc, int targetPC) {
|
|
2293 |
bc_label.putInt(c.encodeBCI(targetPC) - c.encodeBCI(pc));
|
|
2294 |
}
|
|
2295 |
protected int getLabel(IntBand bc_label, Code c, int pc) {
|
|
2296 |
return c.decodeBCI(bc_label.getInt() + c.encodeBCI(pc));
|
|
2297 |
}
|
|
2298 |
|
|
2299 |
protected CPRefBand getCPRefOpBand(int bc) {
|
|
2300 |
switch (Instruction.getCPRefOpTag(bc)) {
|
|
2301 |
case CONSTANT_Class:
|
|
2302 |
return bc_classref;
|
|
2303 |
case CONSTANT_Fieldref:
|
|
2304 |
return bc_fieldref;
|
|
2305 |
case CONSTANT_Methodref:
|
|
2306 |
return bc_methodref;
|
|
2307 |
case CONSTANT_InterfaceMethodref:
|
|
2308 |
return bc_imethodref;
|
|
2309 |
case CONSTANT_Literal:
|
|
2310 |
switch (bc) {
|
|
2311 |
case _ildc: case _ildc_w:
|
|
2312 |
return bc_intref;
|
|
2313 |
case _fldc: case _fldc_w:
|
|
2314 |
return bc_floatref;
|
|
2315 |
case _lldc2_w:
|
|
2316 |
return bc_longref;
|
|
2317 |
case _dldc2_w:
|
|
2318 |
return bc_doubleref;
|
|
2319 |
case _aldc: case _aldc_w:
|
|
2320 |
return bc_stringref;
|
|
2321 |
case _cldc: case _cldc_w:
|
|
2322 |
return bc_classref;
|
|
2323 |
}
|
|
2324 |
break;
|
|
2325 |
}
|
|
2326 |
assert(false);
|
|
2327 |
return null;
|
|
2328 |
}
|
|
2329 |
|
|
2330 |
protected CPRefBand selfOpRefBand(int self_bc) {
|
|
2331 |
assert(Instruction.isSelfLinkerOp(self_bc));
|
|
2332 |
int idx = (self_bc - _self_linker_op);
|
|
2333 |
boolean isSuper = (idx >= _self_linker_super_flag);
|
|
2334 |
if (isSuper) idx -= _self_linker_super_flag;
|
|
2335 |
boolean isAload = (idx >= _self_linker_aload_flag);
|
|
2336 |
if (isAload) idx -= _self_linker_aload_flag;
|
|
2337 |
int origBC = _first_linker_op + idx;
|
|
2338 |
boolean isField = Instruction.isFieldOp(origBC);
|
|
2339 |
if (!isSuper)
|
|
2340 |
return isField? bc_thisfield: bc_thismethod;
|
|
2341 |
else
|
|
2342 |
return isField? bc_superfield: bc_supermethod;
|
|
2343 |
}
|
|
2344 |
|
|
2345 |
////////////////////////////////////////////////////////////////////
|
|
2346 |
|
|
2347 |
static int nextSeqForDebug;
|
|
2348 |
static File dumpDir;
|
|
2349 |
static OutputStream getDumpStream(Band b, String ext) throws IOException {
|
|
2350 |
return getDumpStream(b.name, b.seqForDebug, ext, b);
|
|
2351 |
}
|
|
2352 |
static OutputStream getDumpStream(Index ix, String ext) throws IOException {
|
|
2353 |
if (ix.size() == 0) return new ByteArrayOutputStream();
|
|
2354 |
int seq = ConstantPool.TAG_ORDER[ix.cpMap[0].tag];
|
|
2355 |
return getDumpStream(ix.debugName, seq, ext, ix);
|
|
2356 |
}
|
|
2357 |
static OutputStream getDumpStream(String name, int seq, String ext, Object b) throws IOException {
|
|
2358 |
if (dumpDir == null) {
|
|
2359 |
dumpDir = File.createTempFile("BD_", "", new File("."));
|
|
2360 |
dumpDir.delete();
|
|
2361 |
if (dumpDir.mkdir())
|
|
2362 |
Utils.log.info("Dumping bands to "+dumpDir);
|
|
2363 |
}
|
|
2364 |
name = name.replace('(', ' ').replace(')', ' ');
|
|
2365 |
name = name.replace('/', ' ');
|
|
2366 |
name = name.replace('*', ' ');
|
|
2367 |
name = name.trim().replace(' ','_');
|
|
2368 |
name = ((10000+seq) + "_" + name).substring(1);
|
|
2369 |
File dumpFile = new File(dumpDir, name+ext);
|
|
2370 |
Utils.log.info("Dumping "+b+" to "+dumpFile);
|
|
2371 |
return new BufferedOutputStream(new FileOutputStream(dumpFile));
|
|
2372 |
}
|
|
2373 |
|
|
2374 |
// DEBUG ONLY: Validate me at each length change.
|
|
2375 |
static boolean assertCanChangeLength(Band b) {
|
|
2376 |
switch (b.phase) {
|
|
2377 |
case COLLECT_PHASE:
|
|
2378 |
case READ_PHASE:
|
|
2379 |
return true;
|
|
2380 |
}
|
|
2381 |
return false;
|
|
2382 |
}
|
|
2383 |
|
|
2384 |
// DEBUG ONLY: Validate a phase.
|
|
2385 |
static boolean assertPhase(Band b, int phaseExpected) {
|
|
2386 |
if (b.phase() != phaseExpected) {
|
|
2387 |
Utils.log.warning("phase expected "+phaseExpected+" was "+b.phase()+" in "+b);
|
|
2388 |
return false;
|
|
2389 |
}
|
|
2390 |
return true;
|
|
2391 |
}
|
|
2392 |
|
|
2393 |
|
|
2394 |
// DEBUG ONLY: Tells whether verbosity is turned on.
|
|
2395 |
static int verbose() {
|
|
2396 |
return Utils.currentPropMap().getInteger(Utils.DEBUG_VERBOSE);
|
|
2397 |
}
|
|
2398 |
|
|
2399 |
|
|
2400 |
// DEBUG ONLY: Validate me at each phase change.
|
|
2401 |
static boolean assertPhaseChangeOK(Band b, int p0, int p1) {
|
|
2402 |
switch (p0*10+p1) {
|
|
2403 |
/// Writing phases:
|
|
2404 |
case NO_PHASE*10+COLLECT_PHASE:
|
|
2405 |
// Ready to collect data from the input classes.
|
|
2406 |
assert(!b.isReader());
|
|
2407 |
assert(b.capacity() >= 0);
|
|
2408 |
assert(b.length() == 0);
|
|
2409 |
return true;
|
|
2410 |
case COLLECT_PHASE*10+FROZEN_PHASE:
|
|
2411 |
case FROZEN_PHASE*10+FROZEN_PHASE:
|
|
2412 |
assert(b.length() == 0);
|
|
2413 |
return true;
|
|
2414 |
case COLLECT_PHASE*10+WRITE_PHASE:
|
|
2415 |
case FROZEN_PHASE*10+WRITE_PHASE:
|
|
2416 |
// Data is all collected. Ready to write bytes to disk.
|
|
2417 |
return true;
|
|
2418 |
case WRITE_PHASE*10+DONE_PHASE:
|
|
2419 |
// Done writing to disk. Ready to reset, in principle.
|
|
2420 |
return true;
|
|
2421 |
|
|
2422 |
/// Reading phases:
|
|
2423 |
case NO_PHASE*10+EXPECT_PHASE:
|
|
2424 |
assert(b.isReader());
|
|
2425 |
assert(b.capacity() < 0);
|
|
2426 |
return true;
|
|
2427 |
case EXPECT_PHASE*10+READ_PHASE:
|
|
2428 |
// Ready to read values from disk.
|
|
2429 |
assert(Math.max(0,b.capacity()) >= b.valuesExpected());
|
|
2430 |
assert(b.length() <= 0);
|
|
2431 |
return true;
|
|
2432 |
case READ_PHASE*10+DISBURSE_PHASE:
|
|
2433 |
// Ready to disburse values.
|
|
2434 |
assert(b.valuesRemainingForDebug() == b.length());
|
|
2435 |
return true;
|
|
2436 |
case DISBURSE_PHASE*10+DONE_PHASE:
|
|
2437 |
// Done disbursing values. Ready to reset, in principle.
|
|
2438 |
assert(assertDoneDisbursing(b));
|
|
2439 |
return true;
|
|
2440 |
}
|
|
2441 |
if (p0 == p1)
|
|
2442 |
Utils.log.warning("Already in phase "+p0);
|
|
2443 |
else
|
|
2444 |
Utils.log.warning("Unexpected phase "+p0+" -> "+p1);
|
|
2445 |
return false;
|
|
2446 |
}
|
|
2447 |
|
|
2448 |
static private boolean assertDoneDisbursing(Band b) {
|
|
2449 |
if (b.phase != DISBURSE_PHASE) {
|
|
2450 |
Utils.log.warning("assertDoneDisbursing: still in phase "+b.phase+": "+b);
|
|
2451 |
if (verbose() <= 1) return false; // fail now
|
|
2452 |
}
|
|
2453 |
int left = b.valuesRemainingForDebug();
|
|
2454 |
if (left > 0) {
|
|
2455 |
Utils.log.warning("assertDoneDisbursing: "+left+" values left in "+b);
|
|
2456 |
if (verbose() <= 1) return false; // fail now
|
|
2457 |
}
|
|
2458 |
if (b instanceof MultiBand) {
|
|
2459 |
MultiBand mb = (MultiBand) b;
|
|
2460 |
for (int i = 0; i < mb.bandCount; i++) {
|
|
2461 |
Band sub = mb.bands[i];
|
|
2462 |
if (sub.phase != DONE_PHASE) {
|
|
2463 |
Utils.log.warning("assertDoneDisbursing: sub-band still in phase "+sub.phase+": "+sub);
|
|
2464 |
if (verbose() <= 1) return false; // fail now
|
|
2465 |
}
|
|
2466 |
}
|
|
2467 |
}
|
|
2468 |
return true;
|
|
2469 |
}
|
|
2470 |
|
|
2471 |
static private void printCDecl(Band b) {
|
|
2472 |
if (b instanceof MultiBand) {
|
|
2473 |
MultiBand mb = (MultiBand) b;
|
|
2474 |
for (int i = 0; i < mb.bandCount; i++) {
|
|
2475 |
printCDecl(mb.bands[i]);
|
|
2476 |
}
|
|
2477 |
return;
|
|
2478 |
}
|
|
2479 |
String ixS = "NULL";
|
|
2480 |
if (b instanceof CPRefBand) {
|
|
2481 |
Index ix = ((CPRefBand)b).index;
|
|
2482 |
if (ix != null) ixS = "INDEX("+ix.debugName+")";
|
|
2483 |
}
|
|
2484 |
Coding[] knownc = { BYTE1, CHAR3, BCI5, BRANCH5, UNSIGNED5,
|
|
2485 |
UDELTA5, SIGNED5, DELTA5, MDELTA5 };
|
|
2486 |
String[] knowns = { "BYTE1", "CHAR3", "BCI5", "BRANCH5", "UNSIGNED5",
|
|
2487 |
"UDELTA5", "SIGNED5", "DELTA5", "MDELTA5" };
|
|
2488 |
Coding rc = b.regularCoding;
|
|
2489 |
int rci = Arrays.asList(knownc).indexOf(rc);
|
|
2490 |
String cstr;
|
|
2491 |
if (rci >= 0)
|
|
2492 |
cstr = knowns[rci];
|
|
2493 |
else
|
|
2494 |
cstr = "CODING"+rc.keyString();
|
|
2495 |
System.out.println(" BAND_INIT(\""+b.name()+"\""
|
|
2496 |
+", "+cstr+", "+ixS+"),");
|
|
2497 |
}
|
|
2498 |
|
|
2499 |
private HashMap prevForAssertMap;
|
|
2500 |
|
|
2501 |
// DEBUG ONLY: Record something about the band order.
|
|
2502 |
boolean notePrevForAssert(Band b, Band p) {
|
|
2503 |
if (prevForAssertMap == null)
|
|
2504 |
prevForAssertMap = new HashMap();
|
|
2505 |
prevForAssertMap.put(b, p);
|
|
2506 |
return true;
|
|
2507 |
}
|
|
2508 |
|
|
2509 |
// DEBUG ONLY: Validate next input band.
|
|
2510 |
private boolean assertReadyToReadFrom(Band b, InputStream in) throws IOException {
|
|
2511 |
Band p = (Band) prevForAssertMap.get(b);
|
|
2512 |
// Any previous band must be done reading before this one starts.
|
|
2513 |
if (p != null && phaseCmp(p.phase(), DISBURSE_PHASE) < 0) {
|
|
2514 |
Utils.log.warning("Previous band not done reading.");
|
|
2515 |
Utils.log.info(" Previous band: "+p);
|
|
2516 |
Utils.log.info(" Next band: "+b);
|
|
2517 |
Thread.dumpStack();
|
|
2518 |
assert(verbose > 0); // die unless verbose is true
|
|
2519 |
}
|
|
2520 |
String name = b.name;
|
|
2521 |
if (optDebugBands && !name.startsWith("(")) {
|
|
2522 |
// Verify synchronization between reader & writer:
|
|
2523 |
StringBuffer buf = new StringBuffer();
|
|
2524 |
int ch;
|
|
2525 |
while ((ch = in.read()) > 0)
|
|
2526 |
buf.append((char)ch);
|
|
2527 |
String inName = buf.toString();
|
|
2528 |
if (!inName.equals(name)) {
|
|
2529 |
StringBuffer sb = new StringBuffer();
|
|
2530 |
sb.append("Expected "+name+" but read: ");
|
|
2531 |
inName += (char)ch;
|
|
2532 |
while (inName.length() < 10)
|
|
2533 |
inName += (char)in.read();
|
|
2534 |
for (int i = 0; i < inName.length(); i++)
|
|
2535 |
sb.append(inName.charAt(i));
|
|
2536 |
Utils.log.warning(sb.toString());
|
|
2537 |
return false;
|
|
2538 |
}
|
|
2539 |
}
|
|
2540 |
return true;
|
|
2541 |
}
|
|
2542 |
|
|
2543 |
// DEBUG ONLY: Make sure a bunch of cprefs are correct.
|
|
2544 |
private boolean assertValidCPRefs(CPRefBand b) {
|
|
2545 |
if (b.index == null) return true;
|
|
2546 |
int limit = b.index.size()+1;
|
|
2547 |
for (int i = 0; i < b.length(); i++) {
|
|
2548 |
int v = b.valueAtForDebug(i);
|
|
2549 |
if (v < 0 || v >= limit) {
|
|
2550 |
Utils.log.warning("CP ref out of range "+
|
|
2551 |
"["+i+"] = "+v+" in "+b);
|
|
2552 |
return false;
|
|
2553 |
}
|
|
2554 |
}
|
|
2555 |
return true;
|
|
2556 |
}
|
|
2557 |
|
|
2558 |
// DEBUG ONLY: Maybe write a debugging cookie to next output band.
|
|
2559 |
private boolean assertReadyToWriteTo(Band b, OutputStream out) throws IOException {
|
|
2560 |
Band p = (Band) prevForAssertMap.get(b);
|
|
2561 |
// Any previous band must be done writing before this one starts.
|
|
2562 |
if (p != null && phaseCmp(p.phase(), DONE_PHASE) < 0) {
|
|
2563 |
Utils.log.warning("Previous band not done writing.");
|
|
2564 |
Utils.log.info(" Previous band: "+p);
|
|
2565 |
Utils.log.info(" Next band: "+b);
|
|
2566 |
Thread.dumpStack();
|
|
2567 |
assert(verbose > 0); // die unless verbose is true
|
|
2568 |
}
|
|
2569 |
String name = b.name;
|
|
2570 |
if (optDebugBands && !name.startsWith("(")) {
|
|
2571 |
// Verify synchronization between reader & writer:
|
|
2572 |
for (int j = 0; j < name.length(); j++) {
|
|
2573 |
out.write((byte)name.charAt(j));
|
|
2574 |
}
|
|
2575 |
out.write((byte)0);
|
|
2576 |
}
|
|
2577 |
return true;
|
|
2578 |
}
|
|
2579 |
|
|
2580 |
protected static boolean testBit(int flags, int bitMask) {
|
|
2581 |
return (flags & bitMask) != 0;
|
|
2582 |
}
|
|
2583 |
protected static int setBit(int flags, int bitMask, boolean z) {
|
|
2584 |
return z ? (flags | bitMask) : (flags &~ bitMask);
|
|
2585 |
}
|
|
2586 |
protected static boolean testBit(long flags, long bitMask) {
|
|
2587 |
return (flags & bitMask) != 0;
|
|
2588 |
}
|
|
2589 |
protected static long setBit(long flags, long bitMask, boolean z) {
|
|
2590 |
return z ? (flags | bitMask) : (flags &~ bitMask);
|
|
2591 |
}
|
|
2592 |
|
|
2593 |
|
|
2594 |
static void printArrayTo(PrintStream ps, int[] values, int start, int end) {
|
|
2595 |
int len = end-start;
|
|
2596 |
for (int i = 0; i < len; i++) {
|
|
2597 |
if (i % 10 == 0)
|
|
2598 |
ps.println();
|
|
2599 |
else
|
|
2600 |
ps.print(" ");
|
|
2601 |
ps.print(values[start+i]);
|
|
2602 |
}
|
|
2603 |
ps.println();
|
|
2604 |
}
|
|
2605 |
|
|
2606 |
static void printArrayTo(PrintStream ps, Entry[] cpMap, int start, int end) {
|
|
2607 |
StringBuffer buf = new StringBuffer();
|
|
2608 |
int len = end-start;
|
|
2609 |
for (int i = 0; i < len; i++) {
|
|
2610 |
String s = cpMap[start+i].stringValue();
|
|
2611 |
buf.setLength(0);
|
|
2612 |
for (int j = 0; j < s.length(); j++) {
|
|
2613 |
char ch = s.charAt(j);
|
|
2614 |
if (!(ch < ' ' || ch > '~' || ch == '\\')) {
|
|
2615 |
buf.append(ch);
|
|
2616 |
} else if (ch == '\n') {
|
|
2617 |
buf.append("\\n");
|
|
2618 |
} else if (ch == '\t') {
|
|
2619 |
buf.append("\\t");
|
|
2620 |
} else if (ch == '\r') {
|
|
2621 |
buf.append("\\r");
|
|
2622 |
} else {
|
|
2623 |
buf.append("\\x"+Integer.toHexString(ch));
|
|
2624 |
}
|
|
2625 |
}
|
|
2626 |
ps.println(buf);
|
|
2627 |
}
|
|
2628 |
}
|
|
2629 |
|
|
2630 |
|
|
2631 |
// Utilities for reallocating:
|
|
2632 |
protected static Object[] realloc(Object[] a, int len) {
|
|
2633 |
java.lang.Class elt = a.getClass().getComponentType();
|
|
2634 |
Object[] na = (Object[]) java.lang.reflect.Array.newInstance(elt, len);
|
|
2635 |
System.arraycopy(a, 0, na, 0, Math.min(a.length, len));
|
|
2636 |
return na;
|
|
2637 |
}
|
|
2638 |
protected static Object[] realloc(Object[] a) {
|
|
2639 |
return realloc(a, Math.max(10, a.length*2));
|
|
2640 |
}
|
|
2641 |
static private int[] noInts = {};
|
|
2642 |
protected static int[] realloc(int[] a, int len) {
|
|
2643 |
if (len == 0) return noInts;
|
|
2644 |
if (a == null) return new int[len];
|
|
2645 |
int[] na = new int[len];
|
|
2646 |
System.arraycopy(a, 0, na, 0, Math.min(a.length, len));
|
|
2647 |
return na;
|
|
2648 |
}
|
|
2649 |
protected static int[] realloc(int[] a) {
|
|
2650 |
return realloc(a, Math.max(10, a.length*2));
|
|
2651 |
}
|
|
2652 |
static private byte[] noBytes = {};
|
|
2653 |
protected static byte[] realloc(byte[] a, int len) {
|
|
2654 |
if (len == 0) return noBytes;
|
|
2655 |
if (a == null) return new byte[len];
|
|
2656 |
byte[] na = new byte[len];
|
|
2657 |
System.arraycopy(a, 0, na, 0, Math.min(a.length, len));
|
|
2658 |
return na;
|
|
2659 |
}
|
|
2660 |
protected static byte[] realloc(byte[] a) {
|
|
2661 |
return realloc(a, Math.max(10, a.length*2));
|
|
2662 |
}
|
|
2663 |
}
|