src/java.base/share/classes/jdk/internal/jimage/ImageStringsReader.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:
31673
135283550686 8080511: Refresh of jimage support
jfdenise
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.
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     4
 *
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    10
 *
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    15
 * accompanied this code).
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    16
 *
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    20
 *
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    23
 * questions.
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    24
 */
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    25
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    26
package jdk.internal.jimage;
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    27
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    28
import java.io.UTFDataFormatException;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    29
import java.nio.ByteBuffer;
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
    30
import java.util.Objects;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    31
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    32
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    33
 * @implNote This class needs to maintain JDK 8 source compatibility.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    34
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    35
 * It is used internally in the JDK to implement jimage/jrtfs access,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    36
 * but also compiled and delivered as part of the jrtfs.jar to support access
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    37
 * 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
    38
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    39
public class ImageStringsReader implements ImageStrings {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    40
    public static final int HASH_MULTIPLIER = 0x01000193;
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    41
    public static final int POSITIVE_MASK = 0x7FFFFFFF;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    42
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    43
    private final BasicImageReader reader;
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    44
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    45
    ImageStringsReader(BasicImageReader reader) {
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
    46
        this.reader = Objects.requireNonNull(reader);
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    47
    }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    48
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    49
    @Override
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    50
    public String get(int offset) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    51
        return reader.getString(offset);
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    52
    }
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    53
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    54
    @Override
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    55
    public int add(final String string) {
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    56
        throw new InternalError("Can not add strings at runtime");
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
    57
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    58
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    59
    public static int hashCode(String s) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    60
        return hashCode(s, HASH_MULTIPLIER);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    61
    }
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
    62
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    63
    public static int hashCode(String s, int seed) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    64
        return unmaskedHashCode(s, seed) & POSITIVE_MASK;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    65
    }
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
    66
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    67
    public static int hashCode(String module, String name) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    68
        return hashCode(module, name, HASH_MULTIPLIER);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    69
    }
38582
8618f4ccb220 8156209: Add argument checks to BasicImageReader calls
jlaskey
parents: 36511
diff changeset
    70
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    71
    public static int hashCode(String module, String name, int seed) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    72
        seed = unmaskedHashCode("/", seed);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    73
        seed = unmaskedHashCode(module, seed);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    74
        seed = unmaskedHashCode("/", seed);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    75
        seed = unmaskedHashCode(name, seed);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    76
        return seed & POSITIVE_MASK;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    77
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    78
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    79
    public static int unmaskedHashCode(String s, int seed) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    80
        int slen = s.length();
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    81
        byte[] buffer = null;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    82
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    83
        for (int i = 0; i < slen; i++) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    84
            char ch = s.charAt(i);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    85
            int uch = ch & 0xFFFF;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    86
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    87
            if ((uch & ~0x7F) != 0) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    88
                if (buffer == null) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    89
                    buffer = new byte[8];
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    90
                }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    91
                int mask = ~0x3F;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    92
                int n = 0;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
    93
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    94
                do {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    95
                    buffer[n++] = (byte)(0x80 | (uch & 0x3F));
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    96
                    uch >>= 6;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    97
                    mask >>= 1;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    98
                } while ((uch & mask) != 0);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
    99
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   100
                buffer[n] = (byte)((mask << 1) | uch);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   101
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   102
                do {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   103
                    seed = (seed * HASH_MULTIPLIER) ^ (buffer[n--] & 0xFF);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   104
                } while (0 <= n);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   105
            } else if (uch == 0) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   106
                seed = (seed * HASH_MULTIPLIER) ^ (0xC0);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   107
                seed = (seed * HASH_MULTIPLIER) ^ (0x80);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   108
            } else {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   109
                seed = (seed * HASH_MULTIPLIER) ^ (uch);
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   110
            }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   111
        }
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   112
        return seed;
36511
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
    static int charsFromMUTF8Length(byte[] bytes, int offset, int count) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   116
        int length = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   117
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   118
        for (int i = offset; i < offset + count; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   119
            byte ch = bytes[i];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   120
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   121
            if (ch == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   122
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   123
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   124
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   125
            if ((ch & 0xC0) != 0x80) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   126
                length++;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   127
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   128
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   129
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   130
        return length;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   131
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   132
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   133
    static void charsFromMUTF8(char[] chars, byte[] bytes, int offset, int count) throws UTFDataFormatException {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   134
        int j = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   135
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   136
        for (int i = offset; i < offset + count; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   137
            byte ch = bytes[i];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   138
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   139
            if (ch == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   140
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   141
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   142
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   143
            boolean is_unicode = (ch & 0x80) != 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   144
            int uch = ch & 0x7F;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   145
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   146
            if (is_unicode) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   147
                int mask = 0x40;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   148
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   149
                while ((uch & mask) != 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   150
                    ch = bytes[++i];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   151
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   152
                    if ((ch & 0xC0) != 0x80) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   153
                        throw new UTFDataFormatException("bad continuation 0x" + Integer.toHexString(ch));
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   154
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   155
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   156
                    uch = ((uch & ~mask) << 6) | (ch & 0x3F);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   157
                    mask <<= 6 - 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   158
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   159
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   160
                if ((uch & 0xFFFF) != uch) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   161
                    throw new UTFDataFormatException("character out of range \\u" + Integer.toHexString(uch));
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   162
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   163
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   164
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   165
            chars[j++] = (char)uch;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   166
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   167
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   168
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   169
    public static String stringFromMUTF8(byte[] bytes, int offset, int count) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   170
        int length = charsFromMUTF8Length(bytes, offset, count);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   171
        char[] chars = new char[length];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   172
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   173
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   174
            charsFromMUTF8(chars, bytes, offset, count);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   175
        } catch (UTFDataFormatException ex) {
41121
91734a3ed04b 8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents: 38584
diff changeset
   176
            throw new InternalError("Attempt to convert non modified UTF-8 byte sequence", ex);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   177
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   178
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   179
        return new String(chars);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   180
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   181
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   182
    public static String stringFromMUTF8(byte[] bytes) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   183
        return stringFromMUTF8(bytes, 0, bytes.length);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   184
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   185
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   186
    static int charsFromByteBufferLength(ByteBuffer buffer) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   187
        int length = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   188
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   189
        while(buffer.hasRemaining()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   190
            byte ch = buffer.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   191
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   192
            if (ch == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   193
                return length;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   194
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   195
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   196
            if ((ch & 0xC0) != 0x80) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   197
                length++;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   198
            }
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
        throw new InternalError("No terminating zero byte for modified UTF-8 byte sequence");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   202
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   203
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   204
    static void charsFromByteBuffer(char[] chars, ByteBuffer buffer) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   205
        int j = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   206
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   207
        while(buffer.hasRemaining()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   208
            byte ch = buffer.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   209
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   210
            if (ch == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   211
                return;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   212
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   213
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   214
            boolean is_unicode = (ch & 0x80) != 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   215
            int uch = ch & 0x7F;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   216
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   217
            if (is_unicode) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   218
                int mask = 0x40;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   219
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   220
                while ((uch & mask) != 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   221
                    ch = buffer.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   222
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   223
                    if ((ch & 0xC0) != 0x80) {
41121
91734a3ed04b 8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents: 38584
diff changeset
   224
                        throw new InternalError("Bad continuation in " +
91734a3ed04b 8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents: 38584
diff changeset
   225
                            "modified UTF-8 byte sequence: " + ch);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   226
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   227
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   228
                    uch = ((uch & ~mask) << 6) | (ch & 0x3F);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   229
                    mask <<= 6 - 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   230
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   231
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   232
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   233
            if ((uch & 0xFFFF) != uch) {
41121
91734a3ed04b 8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents: 38584
diff changeset
   234
                throw new InternalError("UTF-32 char in modified UTF-8 " +
91734a3ed04b 8151832: Improve exception messages in exception thrown by new JDK 9 code
coffeys
parents: 38584
diff changeset
   235
                    "byte sequence: " + uch);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   236
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   237
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   238
            chars[j++] = (char)uch;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   239
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   240
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   241
        throw new InternalError("No terminating zero byte for modified UTF-8 byte sequence");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   242
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   243
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   244
    public static String stringFromByteBuffer(ByteBuffer buffer) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   245
        int length = charsFromByteBufferLength(buffer);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   246
        buffer.rewind();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   247
        char[] chars = new char[length];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   248
        charsFromByteBuffer(chars, buffer);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   249
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   250
        return new String(chars);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   251
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   252
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   253
    static int mutf8FromStringLength(String s) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   254
        int length = 0;
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   255
        int slen = s.length();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   256
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   257
        for (int i = 0; i < slen; i++) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   258
            char ch = s.charAt(i);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   259
            int uch = ch & 0xFFFF;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   260
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   261
            if ((uch & ~0x7F) != 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   262
                int mask = ~0x3F;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   263
                int n = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   264
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   265
                do {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   266
                    n++;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   267
                    uch >>= 6;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   268
                    mask >>= 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   269
                } while ((uch & mask) != 0);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   270
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   271
                length += n + 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   272
            } else if (uch == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   273
                length += 2;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   274
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   275
                length++;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   276
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   277
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   278
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   279
        return length;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   280
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   281
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   282
    static void mutf8FromString(byte[] bytes, int offset, String s) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   283
        int j = offset;
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   284
        byte[] buffer = null;
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   285
        int slen = s.length();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   286
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   287
        for (int i = 0; i < slen; i++) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   288
            char ch = s.charAt(i);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   289
            int uch = ch & 0xFFFF;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   290
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   291
            if ((uch & ~0x7F) != 0) {
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   292
                if (buffer == null) {
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   293
                    buffer = new byte[8];
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   294
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   295
                int mask = ~0x3F;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   296
                int n = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   297
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   298
                do {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   299
                    buffer[n++] = (byte)(0x80 | (uch & 0x3F));
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   300
                    uch >>= 6;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   301
                    mask >>= 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   302
                } while ((uch & mask) != 0);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   303
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   304
                buffer[n] = (byte)((mask << 1) | uch);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   305
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   306
                do {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   307
                    bytes[j++] = buffer[n--];
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   308
                } while (0 <= n);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   309
            } else if (uch == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   310
                bytes[j++] = (byte)0xC0;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   311
                bytes[j++] = (byte)0x80;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   312
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   313
                bytes[j++] = (byte)uch;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   314
            }
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
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   318
    public static byte[] mutf8FromString(String string) {
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   319
        int length = mutf8FromStringLength(string);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   320
        byte[] bytes = new byte[length];
44034
0d664fca91a7 8175561: Memory churn in jimage code affects startup after resource encapsulation changes
redestad
parents: 41121
diff changeset
   321
        mutf8FromString(bytes, 0, string);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   322
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   323
        return bytes;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 31673
diff changeset
   324
    }
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents:
diff changeset
   325
}