jdk/src/java.base/share/classes/java/lang/module/ModuleReader.java
author alanb
Fri, 17 Jun 2016 08:41:39 +0100
changeset 39050 9de41b79ec7e
parent 38565 e97761823da2
child 41817 b90ad1de93ea
permissions -rw-r--r--
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class 8159248: ModuleFinder.of not clear that FindException thrown if module descriptor cannot be derived for automatic module Reviewed-by: chegar, mchung
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;
38565
e97761823da2 8156142: ModuleReader instances don't always throw NPE for passed null args
alanb
parents: 37779
diff changeset
    33
import java.util.Objects;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.Optional;
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
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
 * Provides access to the content of a module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 * <p> A module reader is intended for cases where access to the resources in a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
 * module is required, regardless of whether the module has been loaded.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 * A framework that scans a collection of packaged modules on the file system,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 * for example, may use a module reader to access a specific resource in each
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
 * module. A module reader is also intended to be used by {@code ClassLoader}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 * implementations that load classes and resources from modules. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 * <p> A {@code ModuleReader} is {@linkplain ModuleReference#open open} upon
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
 * creation and is closed by invoking the {@link #close close} method.  Failure
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 * to close a module reader may result in a resource leak.  The {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 * try-with-resources} statement provides a useful construct to ensure that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
 * module readers are closed. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
 * <p> A {@code ModuleReader} implementation may require permissions to access
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
 * resources in the module. Consequently the {@link #find find}, {@link #open
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
 * open} and {@link #read read} methods may throw {@code SecurityException} if
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
 * access is denied by the security manager. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
 * @see ModuleReference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
 * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
public interface ModuleReader extends Closeable {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
     * Finds a resource, returning a URI to the resource in the module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
     * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
     *         The name of the resource to open for reading
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
     * @return A URI to the resource; an empty {@code Optional} if the resource
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
     *         is not found or a URI cannot be constructed to locate the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
     *         resource
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
     *         If an I/O error occurs or the module reader is closed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
     * @see ClassLoader#getResource(String)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
    Optional<URI> find(String name) throws IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
     * Opens a resource, returning an input stream to read the resource in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
     * the module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
     * @implSpec The default implementation invokes the {@link #find(String)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
     * find} method to get a URI to the resource. If found, then it attempts
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
     * to construct a {@link java.net.URL URL} and open a connection to the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
     * resource.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
     * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
     *         The name of the resource to open for reading
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
     * @return An input stream to read the resource or an empty
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
     *         {@code Optional} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
     *         If an I/O error occurs or the module reader is closed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    default Optional<InputStream> open(String name) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        Optional<URI> ouri = find(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        if (ouri.isPresent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
            return Optional.of(ouri.get().toURL().openStream());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
            return Optional.empty();
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
     * Reads a resource, returning a byte buffer with the contents of the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
     * resource.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
     * The element at the returned buffer's position is the first byte of the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
     * resource, the element at the buffer's limit is the last byte of the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
     * resource. Once consumed, the {@link #release(ByteBuffer) release} method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
     * must be invoked. Failure to invoke the {@code release} method may result
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
     * in a resource leak.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
     * @apiNote This method is intended for high-performance class loading. It
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
     * is not capable (or intended) to read arbitrary large resources that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
     * could potentially be 2GB or larger. The rational for using this method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
     * in conjunction with the {@code release} method is to allow module reader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
     * implementations manage buffers in an efficient manner.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
     * @implSpec The default implementation invokes the {@link #open(String)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
     * open} method and reads all bytes from the input stream into a byte
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
     * buffer.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
     * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
     *         The name of the resource to read
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
     * @return A byte buffer containing the contents of the resource or an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
     *         empty {@code Optional} if not found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
     *         If an I/O error occurs or the module reader is closed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
     * @see ClassLoader#defineClass(String, ByteBuffer, java.security.ProtectionDomain)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
    default Optional<ByteBuffer> read(String name) throws IOException {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   146
        Optional<InputStream> oin = open(name);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   147
        if (oin.isPresent()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   148
            try (InputStream in = oin.get()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   149
                return Optional.of(ByteBuffer.wrap(in.readAllBytes()));
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   150
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
            return Optional.empty();
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
     * Release a byte buffer. This method should be invoked after consuming
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
     * the contents of the buffer returned by the {@code read} method.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
     * The behavior of this method when invoked to release a buffer that has
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
     * already been released, or the behavior when invoked to release a buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
     * after a {@code ModuleReader} is closed is implementation specific and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
     * therefore not specified.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
     * @param  bb
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
     *         The byte buffer to release
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
     *
38565
e97761823da2 8156142: ModuleReader instances don't always throw NPE for passed null args
alanb
parents: 37779
diff changeset
   167
     * @implSpec The default implementation doesn't do anything except check
e97761823da2 8156142: ModuleReader instances don't always throw NPE for passed null args
alanb
parents: 37779
diff changeset
   168
     * if the byte buffer is null.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
     */
38565
e97761823da2 8156142: ModuleReader instances don't always throw NPE for passed null args
alanb
parents: 37779
diff changeset
   170
    default void release(ByteBuffer bb) {
e97761823da2 8156142: ModuleReader instances don't always throw NPE for passed null args
alanb
parents: 37779
diff changeset
   171
        Objects.requireNonNull(bb);
e97761823da2 8156142: ModuleReader instances don't always throw NPE for passed null args
alanb
parents: 37779
diff changeset
   172
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
     * Closes the module reader. Once closed then subsequent calls to locate or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
     * read a resource will fail by returning {@code Optional.empty()} or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
     * throwing {@code IOException}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
     * <p> A module reader is not required to be asynchronously closeable. If a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
     * thread is reading a resource and another thread invokes the close method,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
     * then the second thread may block until the read operation is complete.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
     * <p> The behavior of {@code InputStream}s obtained using the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
     * #open(String) open} method and used after the module reader is closed
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
     * is implementation specific and therefore not specified.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
    void close() throws IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
}