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