src/java.base/share/classes/jdk/internal/jimage/ImageLocation.java
author erikj
Wed, 07 Mar 2018 13:26:15 -0800
changeset 49357 aaedb8343784
parent 47216 71c04702a3d5
permissions -rw-r--r--
8198243: Add build time check for global operator new/delete in object files Reviewed-by: tbell, kbarrett, dholmes, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     1
/*
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
     2
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     4
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    10
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    15
 * accompanied this code).
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    16
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    20
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    23
 * questions.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    24
 */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    25
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    26
package jdk.internal.jimage;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    27
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    28
import java.nio.ByteBuffer;
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
    29
import java.util.Objects;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    30
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    31
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    32
 * @implNote This class needs to maintain JDK 8 source compatibility.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    33
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    34
 * It is used internally in the JDK to implement jimage/jrtfs access,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    35
 * but also compiled and delivered as part of the jrtfs.jar to support access
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    36
 * to the jimage file provided by the shipped JDK by tools running on JDK 8.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    37
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    38
public class ImageLocation {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    39
    public static final int ATTRIBUTE_END = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    40
    public static final int ATTRIBUTE_MODULE = 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    41
    public static final int ATTRIBUTE_PARENT = 2;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    42
    public static final int ATTRIBUTE_BASE = 3;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    43
    public static final int ATTRIBUTE_EXTENSION = 4;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    44
    public static final int ATTRIBUTE_OFFSET = 5;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    45
    public static final int ATTRIBUTE_COMPRESSED = 6;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    46
    public static final int ATTRIBUTE_UNCOMPRESSED = 7;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    47
    public static final int ATTRIBUTE_COUNT = 8;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    48
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    49
    protected final long[] attributes;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    51
    protected final ImageStrings strings;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    53
    public ImageLocation(long[] attributes, ImageStrings strings) {
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
    54
        this.attributes = Objects.requireNonNull(attributes);
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
    55
        this.strings = Objects.requireNonNull(strings);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    56
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    57
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    58
    ImageStrings getStrings() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    59
        return strings;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    60
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    61
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    62
    static long[] decompress(ByteBuffer bytes) {
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
    63
        Objects.requireNonNull(bytes);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    64
        long[] attributes = new long[ATTRIBUTE_COUNT];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    65
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    66
        if (bytes != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    67
            while (bytes.hasRemaining()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    68
                int data = bytes.get() & 0xFF;
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    69
                int kind = data >>> 3;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    70
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    71
                if (kind == ATTRIBUTE_END) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    72
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    73
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    74
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    75
                if (kind < ATTRIBUTE_END || ATTRIBUTE_COUNT <= kind) {
41121
91734a3ed04b 8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents: 38582
diff changeset
    76
                    throw new InternalError(
91734a3ed04b 8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents: 38582
diff changeset
    77
                        "Invalid jimage attribute kind: " + kind);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    78
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    79
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    80
                int length = (data & 0x7) + 1;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    81
                long value = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    82
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    83
                for (int j = 0; j < length; j++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    84
                    value <<= 8;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    86
                    if (!bytes.hasRemaining()) {
41121
91734a3ed04b 8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents: 38582
diff changeset
    87
                        throw new InternalError("Missing jimage attribute data");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    88
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    89
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    90
                    value |= bytes.get() & 0xFF;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    91
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    92
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    93
                 attributes[kind] = value;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    94
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    95
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    97
        return attributes;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    98
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    99
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   100
    public static byte[] compress(long[] attributes) {
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
   101
        Objects.requireNonNull(attributes);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   102
        ImageStream stream = new ImageStream(16);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   103
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   104
        for (int kind = ATTRIBUTE_END + 1; kind < ATTRIBUTE_COUNT; kind++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   105
            long value = attributes[kind];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   106
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   107
            if (value != 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   108
                int n = (63 - Long.numberOfLeadingZeros(value)) >> 3;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   109
                stream.put((kind << 3) | n);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   110
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   111
                for (int i = n; i >= 0; i--) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   112
                    stream.put((int)(value >> (i << 3)));
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   113
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   114
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   115
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   116
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   117
        stream.put(ATTRIBUTE_END << 3);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   118
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   119
        return stream.toArray();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   120
     }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   121
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   122
    public boolean verify(String name) {
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   123
        return verify(name, attributes, strings);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   124
    }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   125
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   126
    /**
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   127
     * A simpler verification would be {@code name.equals(getFullName())}, but
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   128
     * by not creating the full name and enabling early returns we allocate
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   129
     * fewer objects. Could possibly be made allocation free by extending
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   130
     * ImageStrings to test if strings at an offset match the name region.
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   131
     */
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   132
    static boolean verify(String name, long[] attributes, ImageStrings strings) {
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
   133
        Objects.requireNonNull(name);
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   134
        final int length = name.length();
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   135
        int index = 0;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   136
        int moduleOffset = (int)attributes[ATTRIBUTE_MODULE];
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   137
        if (moduleOffset != 0) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   138
            String module = strings.get(moduleOffset);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   139
            final int moduleLen = module.length();
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   140
            index = moduleLen + 1;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   141
            if (length <= index
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   142
                    || name.charAt(0) != '/'
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   143
                    || !name.regionMatches(1, module, 0, moduleLen)
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   144
                    || name.charAt(index++) != '/') {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   145
                return false;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   146
            }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   147
        }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   148
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   149
        return verifyName(name, index, length, attributes, strings);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   150
    }
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
   151
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   152
    static boolean verify(String module, String name, long[] attributes,
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   153
            ImageStrings strings) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   154
        Objects.requireNonNull(module);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   155
        Objects.requireNonNull(name);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   156
        int moduleOffset = (int)attributes[ATTRIBUTE_MODULE];
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   157
        if (moduleOffset != 0) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   158
            if (!module.equals(strings.get(moduleOffset))) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   159
                return false;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   160
            }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   161
        }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   162
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   163
        return verifyName(name, 0, name.length(), attributes, strings);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   164
    }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   165
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   166
    private static boolean verifyName(String name, int index, int length,
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   167
            long[] attributes, ImageStrings strings) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   168
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   169
        int parentOffset = (int) attributes[ATTRIBUTE_PARENT];
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   170
        if (parentOffset != 0) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   171
            String parent = strings.get(parentOffset);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   172
            final int parentLen = parent.length();
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   173
            if (!name.regionMatches(index, parent, 0, parentLen)) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   174
                return false;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   175
            }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   176
            index += parentLen;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   177
            if (length <= index || name.charAt(index++) != '/') {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   178
                return false;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   179
            }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   180
        }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   181
        String base = strings.get((int) attributes[ATTRIBUTE_BASE]);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   182
        final int baseLen = base.length();
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   183
        if (!name.regionMatches(index, base, 0, baseLen)) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   184
            return false;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   185
        }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   186
        index += baseLen;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   187
        int extOffset = (int) attributes[ATTRIBUTE_EXTENSION];
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   188
        if (extOffset != 0) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   189
            String extension = strings.get(extOffset);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   190
            int extLen = extension.length();
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   191
            if (length <= index
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   192
                    || name.charAt(index++) != '.'
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   193
                    || !name.regionMatches(index, extension, 0, extLen)) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   194
                return false;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   195
            }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   196
            index += extLen;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   197
        }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   198
        return length == index;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   199
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   200
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   201
    long getAttribute(int kind) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   202
        if (kind < ATTRIBUTE_END || ATTRIBUTE_COUNT <= kind) {
41121
91734a3ed04b 8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents: 38582
diff changeset
   203
            throw new InternalError(
91734a3ed04b 8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents: 38582
diff changeset
   204
                "Invalid jimage attribute kind: " + kind);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   205
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   206
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   207
        return attributes[kind];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   208
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   209
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   210
    String getAttributeString(int kind) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   211
        if (kind < ATTRIBUTE_END || ATTRIBUTE_COUNT <= kind) {
41121
91734a3ed04b 8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents: 38582
diff changeset
   212
            throw new InternalError(
91734a3ed04b 8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents: 38582
diff changeset
   213
                "Invalid jimage attribute kind: " + kind);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   214
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   215
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   216
        return getStrings().get((int)attributes[kind]);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   217
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   218
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   219
    public String getModule() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   220
        return getAttributeString(ATTRIBUTE_MODULE);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   221
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   222
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   223
    public int getModuleOffset() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   224
        return (int)getAttribute(ATTRIBUTE_MODULE);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   225
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   226
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   227
    public String getBase() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   228
        return getAttributeString(ATTRIBUTE_BASE);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   229
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   230
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   231
    public int getBaseOffset() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   232
        return (int)getAttribute(ATTRIBUTE_BASE);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   233
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   234
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   235
    public String getParent() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   236
        return getAttributeString(ATTRIBUTE_PARENT);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   237
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   238
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   239
    public int getParentOffset() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   240
        return (int)getAttribute(ATTRIBUTE_PARENT);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   241
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   242
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   243
    public String getExtension() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   244
        return getAttributeString(ATTRIBUTE_EXTENSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   245
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   246
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   247
    public int getExtensionOffset() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   248
        return (int)getAttribute(ATTRIBUTE_EXTENSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   249
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   250
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   251
    public String getFullName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   252
        return getFullName(false);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   253
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   254
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   255
    public String getFullName(boolean modulesPrefix) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   256
        StringBuilder builder = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   257
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   258
        if (getModuleOffset() != 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   259
            if (modulesPrefix) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   260
                builder.append("/modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   261
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   262
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   263
            builder.append('/');
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   264
            builder.append(getModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   265
            builder.append('/');
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   266
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   267
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   268
        if (getParentOffset() != 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   269
            builder.append(getParent());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   270
            builder.append('/');
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   271
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   272
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   273
        builder.append(getBase());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   274
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   275
        if (getExtensionOffset() != 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   276
            builder.append('.');
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   277
            builder.append(getExtension());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   278
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   279
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   280
        return builder.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   281
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   282
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   283
    String buildName(boolean includeModule, boolean includeParent,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   284
            boolean includeName) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   285
        StringBuilder builder = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   286
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   287
        if (includeModule && getModuleOffset() != 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   288
            builder.append("/modules/");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   289
            builder.append(getModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   290
         }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   291
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   292
        if (includeParent && getParentOffset() != 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   293
            builder.append('/');
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   294
            builder.append(getParent());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   295
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   296
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   297
        if (includeName) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   298
            if (includeModule || includeParent) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   299
                builder.append('/');
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   300
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   301
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   302
            builder.append(getBase());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   303
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   304
            if (getExtensionOffset() != 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   305
                builder.append('.');
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   306
                builder.append(getExtension());
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   307
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   308
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   309
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   310
        return builder.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   311
   }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   312
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   313
    public long getContentOffset() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   314
        return getAttribute(ATTRIBUTE_OFFSET);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   315
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   316
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   317
    public long getCompressedSize() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   318
        return getAttribute(ATTRIBUTE_COMPRESSED);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   319
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   320
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   321
    public long getUncompressedSize() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   322
        return getAttribute(ATTRIBUTE_UNCOMPRESSED);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   323
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   324
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   325
    static ImageLocation readFrom(BasicImageReader reader, int offset) {
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
   326
        Objects.requireNonNull(reader);
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   327
        long[] attributes = reader.getAttributes(offset);
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   328
        ImageStringsReader strings = reader.getStrings();
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   329
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 27565
diff changeset
   330
        return new ImageLocation(attributes, strings);
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   331
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   332
}