jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ByteArrayModuleEntry.java
author jlaskey
Tue, 28 Jun 2016 16:07:23 -0300
changeset 39308 f6517755057f
parent 39151 34455cc82f5e
permissions -rw-r--r--
8160459: jlink minor code clean up Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39151
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
     1
/*
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
     4
 *
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    10
 *
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    15
 * accompanied this code).
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    16
 *
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    20
 *
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    23
 * questions.
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    24
 */
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    25
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    26
package jdk.tools.jlink.internal;
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    27
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    28
import java.io.ByteArrayInputStream;
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    29
import java.io.IOException;
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    30
import java.io.InputStream;
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    31
import java.io.OutputStream;
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    32
import java.io.UncheckedIOException;
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    33
import java.util.Objects;
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    34
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    35
/**
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    36
 * A ModuleEntry backed by a given byte[].
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    37
 */
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    38
class ByteArrayModuleEntry extends AbstractModuleEntry {
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    39
    private final byte[] buffer;
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    40
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    41
    /**
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    42
     * Create a new ByteArrayModuleEntry.
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    43
     *
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    44
     * @param module The module name.
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    45
     * @param path The data path identifier.
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    46
     * @param type The data type.
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    47
     * @param buf  The byte buffer.
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    48
     */
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    49
    ByteArrayModuleEntry(String module, String path, Type type, byte[] buffer) {
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    50
        super(module, path, type);
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    51
        this.buffer = Objects.requireNonNull(buffer);
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    52
    }
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    53
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    54
    @Override
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    55
    public byte[] getBytes() {
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    56
        return buffer.clone();
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    57
    }
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    58
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    59
    @Override
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    60
    public InputStream stream() {
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    61
        return new ByteArrayInputStream(buffer);
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    62
    }
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    63
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    64
    @Override
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    65
    public void write(OutputStream out) {
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    66
        try {
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    67
            out.write(buffer);
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    68
        } catch (IOException ex) {
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    69
            throw new UncheckedIOException(ex);
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    70
        }
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    71
    }
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    72
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    73
    @Override
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    74
    public long getLength() {
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    75
        return buffer.length;
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    76
    }
34455cc82f5e 8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
diff changeset
    77
}