langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ModuleNameReader.java
author jlahoda
Tue, 04 Oct 2016 16:25:19 +0200
changeset 41254 08f8dbf7741e
parent 36526 langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/ModuleNameReader.java@3b41f1c69604
child 42407 f3702cff2933
permissions -rw-r--r--
8152911: javac assertion error when compiling overlay sources Summary: Avoid creating ModuleSymbols with unspecified name, to avoid conflicts with predefined ModuleSymbol for the java.base module. Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    25
package com.sun.tools.javac.jvm;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    26
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.io.IOException;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.InputStream;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.nio.file.Files;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.nio.file.Path;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    31
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    32
import javax.tools.JavaFileObject;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    33
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import com.sun.tools.javac.jvm.ClassFile;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    35
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import static com.sun.tools.javac.jvm.ClassFile.*;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    37
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    38
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    39
/**
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 * Stripped down ClassReader, just sufficient to read module names from module-info.class files
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    41
 * while analyzing the module path.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 * <p>
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
 * <b>This is NOT part of any supported API. If you write code that depends on this, you do so at
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 * your own risk. This code and its internal interfaces are subject to change or deletion without
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 * notice.</b>
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
public class ModuleNameReader {
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    49
    public static class BadClassFile extends Exception {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
        private static final long serialVersionUID = 0;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    51
        BadClassFile(String msg) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    52
            super(msg);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    53
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    55
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    private static final int INITIAL_BUFFER_SIZE = 0x0fff0;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    57
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    /** The buffer containing the currently read class file.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    59
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    private byte[] buf = new byte[INITIAL_BUFFER_SIZE];
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    61
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    /** The current input pointer.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    63
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    private int bp;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    65
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    /** For every constant pool entry, an index into buf where the
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    67
     *  defining section of the entry is found.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    68
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    69
    private int[] poolIdx;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    70
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    71
    public ModuleNameReader() {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    72
    }
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    73
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    74
    public String readModuleName(Path p) throws IOException, BadClassFile {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    75
        try (InputStream in = Files.newInputStream(p)) {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    76
            return readModuleName(in);
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    77
        }
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    78
    }
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    79
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    80
    public String readModuleName(JavaFileObject jfo) throws IOException, BadClassFile {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    81
        try (InputStream in = jfo.openInputStream()) {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    82
            return readModuleName(in);
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    83
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    84
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    85
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    86
    public String readModuleName(InputStream in) throws IOException, BadClassFile {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    87
        bp = 0;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    88
        buf = readInputStream(buf, in);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    89
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    90
        int magic = nextInt();
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    91
        if (magic != JAVA_MAGIC)
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    92
            throw new BadClassFile("illegal.start.of.class.file");
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    93
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    94
        int minorVersion = nextChar();
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    95
        int majorVersion = nextChar();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    96
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    97
        indexPool();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    98
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    99
        int accessflags = nextChar();
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   100
        return readModuleInfoName(nextChar());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   101
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   102
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    /** Extract a character at position bp from buf.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   104
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    char getChar(int bp) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        return
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   107
            (char)(((buf[bp] & 0xFF) << 8) + (buf[bp+1] & 0xFF));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   108
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   109
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   110
    /** Read a character.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   111
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   112
    char nextChar() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        return (char)(((buf[bp++] & 0xFF) << 8) + (buf[bp++] & 0xFF));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   114
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   115
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   116
    /** Read an integer.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   117
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   118
    int nextInt() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        return
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            ((buf[bp++] & 0xFF) << 24) +
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            ((buf[bp++] & 0xFF) << 16) +
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            ((buf[bp++] & 0xFF) << 8) +
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            (buf[bp++] & 0xFF);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   124
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   125
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   126
    /** Index all constant pool entries, writing their start addresses into
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   127
     *  poolIdx.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   128
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   129
    void indexPool() throws BadClassFile {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        poolIdx = new int[nextChar()];
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        int i = 1;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        while (i < poolIdx.length) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            poolIdx[i++] = bp;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            byte tag = buf[bp++];
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            switch (tag) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            case CONSTANT_Utf8: case CONSTANT_Unicode: {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                int len = nextChar();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                bp = bp + len;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   141
            case CONSTANT_Class:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   142
            case CONSTANT_String:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   143
            case CONSTANT_MethodType:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                bp = bp + 2;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   146
            case CONSTANT_MethodHandle:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                bp = bp + 3;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   149
            case CONSTANT_Fieldref:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   150
            case CONSTANT_Methodref:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   151
            case CONSTANT_InterfaceMethodref:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   152
            case CONSTANT_NameandType:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   153
            case CONSTANT_Integer:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   154
            case CONSTANT_Float:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   155
            case CONSTANT_InvokeDynamic:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   156
                bp = bp + 4;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   158
            case CONSTANT_Long:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   159
            case CONSTANT_Double:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   160
                bp = bp + 8;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   161
                i++;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   162
                break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   163
            default:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   164
                throw new BadClassFile("malformed constant pool");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   165
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   167
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   168
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   169
    /** Read the class name of a module-info.class file.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   170
     * The name is stored in a CONSTANT_Class entry, where the
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   171
     * class name is of the form module-name/module-info.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   172
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   173
    String readModuleInfoName(int i) throws BadClassFile {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   174
        int classIndex = poolIdx[i];
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   175
        if (buf[classIndex] == CONSTANT_Class) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   176
            int utf8Index = poolIdx[getChar(classIndex + 1)];
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   177
            if (buf[utf8Index] == CONSTANT_Utf8) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   178
                int len = getChar(utf8Index + 1);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   179
                int start = utf8Index + 3;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   180
                String suffix = "/module-info";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                if (endsWith(buf, start, len, suffix))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   182
                    return new String(ClassFile.internalize(buf, start, len - suffix.length()));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   183
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   184
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   185
        throw new BadClassFile("bad module-info name");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   186
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   187
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   188
    private boolean endsWith(byte[] buf, int start, int len, String suffix) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   189
        if (len <= suffix.length())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   190
            return false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   191
        for (int i = 0; i < suffix.length(); i++) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   192
            if (buf[start + len - suffix.length() + i] != suffix.charAt(i))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   193
                return false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   194
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   195
        return true;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   196
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   197
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   198
    private static byte[] readInputStream(byte[] buf, InputStream s) throws IOException {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   199
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   200
            buf = ensureCapacity(buf, s.available());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   201
            int r = s.read(buf);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   202
            int bp = 0;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   203
            while (r != -1) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   204
                bp += r;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   205
                buf = ensureCapacity(buf, bp);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   206
                r = s.read(buf, bp, buf.length - bp);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   207
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   208
            return buf;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   209
        } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   210
            try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                s.close();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   212
            } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   213
                /* Ignore any errors, as this stream may have already
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   214
                 * thrown a related exception which is the one that
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   215
                 * should be reported.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   216
                 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   217
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   218
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   219
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   220
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   221
    /*
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   222
     * ensureCapacity will increase the buffer as needed, taking note that
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   223
     * the new buffer will always be greater than the needed and never
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   224
     * exactly equal to the needed size or bp. If equal then the read (above)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   225
     * will infinitely loop as buf.length - bp == 0.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   226
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   227
    private static byte[] ensureCapacity(byte[] buf, int needed) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   228
        if (buf.length <= needed) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   229
            byte[] old = buf;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   230
            buf = new byte[Integer.highestOneBit(needed) << 1];
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   231
            System.arraycopy(old, 0, buf, 0, old.length);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   232
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   233
        return buf;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   234
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   235
}