jdk/src/share/classes/com/sun/java/util/jar/pack/Fixups.java
author ksrini
Tue, 29 May 2012 14:56:48 -0700
changeset 12857 0a5f341c2a28
parent 10115 eb08d08c7ef7
child 17490 46864558d068
permissions -rw-r--r--
7168401: pack200 does not produce a compatible pack file for JDK7 classes if indy is not present Reviewed-by: jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.java.util.jar.pack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    28
import com.sun.java.util.jar.pack.ConstantPool.Entry;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    29
import java.util.AbstractCollection;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    30
import java.util.ArrayList;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    31
import java.util.Collection;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    32
import java.util.Iterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Collection of relocatable constant pool references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * It operates with respect to a particular byte array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * and stores some of its state in the bytes themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * As a Collection, it can be iterated over, but it is not a List,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * since it does not natively support indexed access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
    45
final class Fixups extends AbstractCollection<Fixups.Fixup> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    byte[] bytes;    // the subject of the relocations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    int head;        // desc locating first reloc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    int tail;        // desc locating last reloc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    int size;        // number of relocations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    Entry[] entries; // [0..size-1] relocations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    int[] bigDescs;  // descs which cannot be stored in the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // A "desc" (descriptor) is a bit-encoded pair of a location
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // and format.  Every fixup occurs at a "desc".  Until final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // patching, bytes addressed by descs may also be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // link this data structure together.  If the bytes are missing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // or if the "desc" is too large to encode in the bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // it is kept in the bigDescs array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    Fixups(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.bytes = bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        entries = new Entry[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        bigDescs = noBigDescs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    Fixups() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // If there are no bytes, all descs are kept in bigDescs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this((byte[])null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
    69
    Fixups(byte[] bytes, Collection<Fixup> fixups) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        addAll(fixups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
    73
    Fixups(Collection<Fixup> fixups) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this((byte[])null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        addAll(fixups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final int MINBIGSIZE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // cleverly share empty bigDescs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static int[] noBigDescs = {MINBIGSIZE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public void trimToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (size != entries.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            Entry[] oldEntries = entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            entries = new Entry[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            System.arraycopy(oldEntries, 0, entries, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int bigSize = bigDescs[BIGSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (bigSize == MINBIGSIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            bigDescs = noBigDescs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } else if (bigSize != bigDescs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            int[] oldBigDescs = bigDescs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            bigDescs = new int[bigSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            System.arraycopy(oldBigDescs, 0, bigDescs, 0, bigSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   102
    public void visitRefs(Collection<Entry> refs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            refs.add(entries[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (bytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // Clean the bytes:
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   111
            for (Fixup fx : this) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                //System.out.println("clean "+fx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                storeIndex(fx.location(), fx.format(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (bigDescs != noBigDescs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            bigDescs[BIGSIZE] = MINBIGSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // do not trim to size, however
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public void setBytes(byte[] newBytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (bytes == newBytes)  return;
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   128
        ArrayList<Fixup> old = null;
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   129
        assert((old = new ArrayList<>(this)) != null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (bytes == null || newBytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            // One or the other representations is deficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            // Construct a checkpoint.
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   133
            ArrayList<Fixup> save = new ArrayList<>(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            bytes = newBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            addAll(save);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            // assume newBytes is some sort of bitwise copy of the old bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            bytes = newBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   141
        assert(old.equals(new ArrayList<>(this)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static final int LOC_SHIFT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    static final int FMT_MASK = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    static final byte UNUSED_BYTE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    static final byte OVERFLOW_BYTE = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // fill pointer of bigDescs array is in element [0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    static final int BIGSIZE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    // Format values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static final int U2_FORMAT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public static final int U1_FORMAT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    // Special values for the static methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static final int SPECIAL_LOC = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static final int SPECIAL_FMT = U2_FORMAT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    static int fmtLen(int fmt) { return 1+(fmt-U1_FORMAT)/(U2_FORMAT-U1_FORMAT); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    static int descLoc(int desc) { return desc >>> LOC_SHIFT; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    static int descFmt(int desc) { return desc  &  FMT_MASK; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    static int descEnd(int desc) { return descLoc(desc) + fmtLen(descFmt(desc)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    static int makeDesc(int loc, int fmt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        int desc = (loc << LOC_SHIFT) | fmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        assert(descLoc(desc) == loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        assert(descFmt(desc) == fmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    int fetchDesc(int loc, int fmt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        byte b1 = bytes[loc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        assert(b1 != OVERFLOW_BYTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (fmt == U2_FORMAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            byte b2 = bytes[loc+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            value = ((b1 & 0xFF) << 8) + (b2 & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            value = (b1 & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // Stored loc field is difference between its own loc and next loc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return value + (loc << LOC_SHIFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    boolean storeDesc(int loc, int fmt, int desc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (bytes == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        int value = desc - (loc << LOC_SHIFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        byte b1, b2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        switch (fmt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        case U2_FORMAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            assert(bytes[loc+0] == UNUSED_BYTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            assert(bytes[loc+1] == UNUSED_BYTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            b1 = (byte)(value >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            b2 = (byte)(value >> 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (value == (value & 0xFFFF) && b1 != OVERFLOW_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                bytes[loc+0] = b1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                bytes[loc+1] = b2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                assert(fetchDesc(loc, fmt) == desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        case U1_FORMAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            assert(bytes[loc] == UNUSED_BYTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            b1 = (byte)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (value == (value & 0xFF) && b1 != OVERFLOW_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                bytes[loc] = b1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                assert(fetchDesc(loc, fmt) == desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        default: assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // Failure.  Caller must allocate a bigDesc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        bytes[loc] = OVERFLOW_BYTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        assert(fmt==U1_FORMAT || (bytes[loc+1]=(byte)bigDescs[BIGSIZE])!=999);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    void storeIndex(int loc, int fmt, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        storeIndex(bytes, loc, fmt, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    void storeIndex(byte[] bytes, int loc, int fmt, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        switch (fmt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        case U2_FORMAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            assert(value == (value & 0xFFFF)) : (value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            bytes[loc+0] = (byte)(value >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            bytes[loc+1] = (byte)(value >> 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        case U1_FORMAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            assert(value == (value & 0xFF)) : (value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            bytes[loc] = (byte)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        default: assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /** Simple and necessary tuple to present each fixup. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public static
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   237
    class Fixup implements Comparable<Fixup> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        int desc;         // location and format of reloc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        Entry entry;      // which entry to plug into the bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        Fixup(int desc, Entry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            this.desc = desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            this.entry = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        public Fixup(int loc, int fmt, Entry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            this.desc = makeDesc(loc, fmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            this.entry = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        public int location() { return descLoc(desc); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        public int format() { return descFmt(desc); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        public Entry entry() { return entry; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        public int compareTo(Fixup that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // Ordering depends only on location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return this.location() - that.location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        public boolean equals(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (!(x instanceof Fixup))  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            Fixup that = (Fixup) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return this.desc == that.desc && this.entry == that.entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return "@"+location()+(format()==U1_FORMAT?".1":"")+"="+entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   266
    class Itr implements Iterator<Fixup> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        int index = 0;               // index into entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        int bigIndex = BIGSIZE+1;    // index into bigDescs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        int next = head;             // desc pointing to next fixup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        public boolean hasNext() { return index < size; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        public void remove() { throw new UnsupportedOperationException(); }
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   272
        public Fixup next() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            int thisIndex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return new Fixup(nextDesc(), entries[thisIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        int nextDesc() {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   277
            index++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            int thisDesc = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (index < size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                // Fetch next desc eagerly, in case this fixup gets finalized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                int loc = descLoc(thisDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                int fmt = descFmt(thisDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                if (bytes != null && bytes[loc] != OVERFLOW_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    next = fetchDesc(loc, fmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    // The unused extra byte is "asserted" to be equal to BI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    // This helps keep the overflow descs in sync.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    assert(fmt==U1_FORMAT || bytes == null || bytes[loc+1]==(byte)bigIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    next = bigDescs[bigIndex++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            return thisDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   296
    public Iterator<Fixup> iterator() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return new Itr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public void add(int location, int format, Entry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        addDesc(makeDesc(location, format), entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public boolean add(Fixup f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        addDesc(f.desc, f.entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   306
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   307
    public boolean addAll(Collection<? extends Fixup> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (c instanceof Fixups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            // Use knowledge of Itr structure to avoid building little structs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            Fixups that = (Fixups) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (that.size == 0)  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (this.size == 0 && entries.length < that.size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                growEntries(that.size);  // presize exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            Entry[] thatEntries = that.entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            for (Itr i = that.new Itr(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                int ni = i.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                addDesc(i.nextDesc(), thatEntries[ni]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return super.addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    // Here is how things get added:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private void addDesc(int thisDesc, Entry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (entries.length == size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            growEntries(size * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        entries[size] = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            head = tail = thisDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            int prevDesc = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            // Store new desc in previous tail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            int prevLoc = descLoc(prevDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            int prevFmt = descFmt(prevDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            int prevLen = fmtLen(prevFmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            int thisLoc = descLoc(thisDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            // The collection must go in ascending order, and not overlap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (thisLoc < prevLoc + prevLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                badOverlap(thisLoc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            tail = thisDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if (!storeDesc(prevLoc, prevFmt, thisDesc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                // overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                int bigSize = bigDescs[BIGSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                if (bigDescs.length == bigSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    growBigDescs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                //System.out.println("bigDescs["+bigSize+"] = "+thisDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                bigDescs[bigSize++] = thisDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                bigDescs[BIGSIZE] = bigSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        size += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    private void badOverlap(int thisLoc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        throw new IllegalArgumentException("locs must be ascending and must not overlap:  "+thisLoc+" >> "+this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    private void growEntries(int newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        Entry[] oldEntries = entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        entries = new Entry[Math.max(3, newSize)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        System.arraycopy(oldEntries, 0, entries, 0, oldEntries.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private void growBigDescs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        int[] oldBigDescs = bigDescs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        bigDescs = new int[oldBigDescs.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        System.arraycopy(oldBigDescs, 0, bigDescs, 0, oldBigDescs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /// Static methods that optimize the use of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    Object add(Object prevFixups,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
               byte[] bytes, int loc, int fmt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
               Entry e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        Fixups f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (prevFixups == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            if (loc == SPECIAL_LOC && fmt == SPECIAL_FMT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                // Special convention:  If the attribute has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                // U2 relocation at position zero, store the Entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                // rather than building a Fixups structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            f = new Fixups(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        } else if (!(prevFixups instanceof Fixups)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            // Recognize the special convention:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            Entry firstEntry = (Entry) prevFixups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            f = new Fixups(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            f.add(SPECIAL_LOC, SPECIAL_FMT, firstEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            f = (Fixups) prevFixups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            assert(f.bytes == bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        f.add(loc, fmt, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    void setBytes(Object fixups, byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (fixups instanceof Fixups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            Fixups f = (Fixups) fixups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            f.setBytes(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    Object trimToSize(Object fixups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (fixups instanceof Fixups) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            Fixups f = (Fixups) fixups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            f.trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (f.size() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                fixups = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        return fixups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    // Iterate over all the references in this set of fixups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public static
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   417
    void visitRefs(Object fixups, Collection<Entry> refs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (fixups == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        } else if (!(fixups instanceof Fixups)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            // Special convention; see above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            refs.add((Entry) fixups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            Fixups f = (Fixups) fixups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            f.visitRefs(refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    // Clear out this set of fixups by replacing each reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    // by a hardcoded coding of its reference, drawn from ix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    void finishRefs(Object fixups, byte[] bytes, ConstantPool.Index ix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (fixups == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if (!(fixups instanceof Fixups)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            // Special convention; see above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            int index = ix.indexOf((Entry) fixups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            storeIndex(bytes, SPECIAL_LOC, SPECIAL_FMT, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        Fixups f = (Fixups) fixups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        assert(f.bytes == bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        f.finishRefs(ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    void finishRefs(ConstantPool.Index ix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            return;
10115
eb08d08c7ef7 7060849: Eliminate pack200 build warnings
ksrini
parents: 7816
diff changeset
   448
        for (Fixup fx : this) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            int index = ix.indexOf(fx.entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            //System.out.println("finish "+fx+" = "+index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            // Note that the iterator has already fetched the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            // bytes we are about to overwrite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            storeIndex(fx.location(), fx.format(), index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // Further iterations should do nothing:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        bytes = null;  // do not clean them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /// Testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public static void main(String[] av) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        byte[] bytes = new byte[1 << 20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        ConstantPool cp = new ConstantPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        Fixups f = new Fixups(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        boolean isU1 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        int span = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        int nextLoc = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        int[] locs = new int[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        final int[] indexes = new int[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        int iptr = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        for (int loc = 0; loc < bytes.length; loc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            if (loc == nextLoc && loc+1 < bytes.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                int fmt = (isU1 ? U1_FORMAT : U2_FORMAT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                Entry e = ConstantPool.getUtf8Entry("L"+loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                f.add(loc, fmt, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                isU1 ^= true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                if (iptr < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    // Make it close in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    nextLoc += fmtLen(fmt) + (iptr < 5 ? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    nextLoc += span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    span = (int)(span * 1.77);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                // Here are the bytes that would have gone here:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                locs[iptr] = loc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                if (fmt == U1_FORMAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    indexes[iptr++] = (loc & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    indexes[iptr++] = ((loc & 0xFF) << 8) | ((loc+1) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    ++loc;  // skip a byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            bytes[loc] = (byte)loc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        System.out.println("size="+f.size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                           +" overflow="+(f.bigDescs[BIGSIZE]-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        System.out.println("Fixups: "+f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        // Test collection contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        assert(iptr == 1+f.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        List l = new ArrayList(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        Collections.sort(l);  // should not change the order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        if (!l.equals(new ArrayList(f)))  System.out.println("** disordered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        f.setBytes(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if (!l.equals(new ArrayList(f)))  System.out.println("** bad set 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        f.setBytes(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (!l.equals(new ArrayList(f)))  System.out.println("** bad set 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        Fixups f3 = new Fixups(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        if (!l.equals(new ArrayList(f3))) System.out.println("** bad set 3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        Iterator fi = f.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        for (int i = 1; i < iptr; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            Fixup fx = (Fixup) fi.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            if (fx.location() != locs[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                System.out.println("** "+fx+" != "+locs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            if (fx.format() == U1_FORMAT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                System.out.println(fx+" -> "+bytes[locs[i]]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                System.out.println(fx+" -> "+bytes[locs[i]]+" "+bytes[locs[i]+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        assert(!fi.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        indexes[0] = 1;  // like iptr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        Index ix = new Index("ix") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            public int indexOf(Entry e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                return indexes[indexes[0]++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        f.finishRefs(ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        for (int loc = 0; loc < bytes.length; loc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            if (bytes[loc] != (byte)loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                System.out.println("** ["+loc+"] = "+bytes[loc]+" != "+(byte)loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
//*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
}