jdk/src/java.base/share/classes/java/lang/module/ModuleReader.java
author redestad
Thu, 21 Apr 2016 13:39:53 +0200
changeset 37593 824750ada3d6
parent 36511 9d0388c6b336
child 37779 7c84df693837
permissions -rw-r--r--
8154231: Simplify access to System properties from JDK code Reviewed-by: rriggs, chegar, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package java.lang.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.Closeable;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.io.InputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.nio.ByteBuffer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
 * Provides access to the content of a module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 * <p> A module reader is intended for cases where access to the resources in a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 * module is required, regardless of whether the module has been loaded.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
 * A framework that scans a collection of packaged modules on the file system,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 * for example, may use a module reader to access a specific resource in each
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 * module. A module reader is also intended to be used by {@code ClassLoader}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
 * implementations that load classes and resources from modules. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 * <p> A {@code ModuleReader} is {@linkplain ModuleReference#open open} upon
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 * creation and is closed by invoking the {@link #close close} method.  Failure
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
 * to close a module reader may result in a resource leak.  The {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 * try-with-resources} statement provides a useful construct to ensure that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 * module readers are closed. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
 * <p> A {@code ModuleReader} implementation may require permissions to access
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
 * resources in the module. Consequently the {@link #find find}, {@link #open
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
 * open} and {@link #read read} methods may throw {@code SecurityException} if
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
 * access is denied by the security manager. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
 * @see ModuleReference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
 * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
public interface ModuleReader extends Closeable {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
     * Finds a resource, returning a URI to the resource in the module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
     * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
     *         The name of the resource to open for reading
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
     * @return A URI to the resource; an empty {@code Optional} if the resource
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
     *         is not found or a URI cannot be constructed to locate the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
     *         resource
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
     *         If an I/O error occurs or the module reader is closed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
     * @see ClassLoader#getResource(String)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    Optional<URI> find(String name) throws IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
     * Opens a resource, returning an input stream to read the resource in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
     * the module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
     * @implSpec The default implementation invokes the {@link #find(String)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
     * find} method to get a URI to the resource. If found, then it attempts
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
     * to construct a {@link java.net.URL URL} and open a connection to the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
     * resource.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
     * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
     *         The name of the resource to open for reading
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
     * @return An input stream to read the resource or an empty
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
     *         {@code Optional} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
     *         If an I/O error occurs or the module reader is closed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
    default Optional<InputStream> open(String name) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        Optional<URI> ouri = find(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        if (ouri.isPresent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
            return Optional.of(ouri.get().toURL().openStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
            return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
     * Reads a resource, returning a byte buffer with the contents of the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
     * resource.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
     * The element at the returned buffer's position is the first byte of the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
     * resource, the element at the buffer's limit is the last byte of the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
     * resource. Once consumed, the {@link #release(ByteBuffer) release} method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
     * must be invoked. Failure to invoke the {@code release} method may result
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
     * in a resource leak.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
     * @apiNote This method is intended for high-performance class loading. It
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
     * is not capable (or intended) to read arbitrary large resources that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
     * could potentially be 2GB or larger. The rational for using this method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
     * in conjunction with the {@code release} method is to allow module reader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
     * implementations manage buffers in an efficient manner.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
     * @implSpec The default implementation invokes the {@link #open(String)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
     * open} method and reads all bytes from the input stream into a byte
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
     * buffer.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
     * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
     *         The name of the resource to read
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
     * @return A byte buffer containing the contents of the resource or an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
     *         empty {@code Optional} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
     *         If an I/O error occurs or the module reader is closed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
     * @see ClassLoader#defineClass(String, ByteBuffer, java.security.ProtectionDomain)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    default Optional<ByteBuffer> read(String name) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        Optional<InputStream> in = open(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
        if (in.isPresent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
            byte[] bytes = in.get().readAllBytes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
            return Optional.of(ByteBuffer.wrap(bytes));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
            return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
     * Release a byte buffer. This method should be invoked after consuming
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
     * the contents of the buffer returned by the {@code read} method.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
     * The behavior of this method when invoked to release a buffer that has
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
     * already been released, or the behavior when invoked to release a buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
     * after a {@code ModuleReader} is closed is implementation specific and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
     * therefore not specified.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
     * @param  bb
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
     *         The byte buffer to release
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
     * @implSpec The default implementation does nothing.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
    default void release(ByteBuffer bb) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
     * Closes the module reader. Once closed then subsequent calls to locate or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
     * read a resource will fail by returning {@code Optional.empty()} or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
     * throwing {@code IOException}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
     * <p> A module reader is not required to be asynchronously closeable. If a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
     * thread is reading a resource and another thread invokes the close method,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
     * then the second thread may block until the read operation is complete.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
     * <p> The behavior of {@code InputStream}s obtained using the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
     * #open(String) open} method and used after the module reader is closed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
     * is implementation specific and therefore not specified.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
    void close() throws IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
}