1 /* 2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang.module; 27 28 import java.io.Closeable; 29 import java.io.IOException; 30 import java.io.InputStream; 31 import java.net.URI; 32 import java.nio.ByteBuffer; 33 import java.util.Objects; 34 import java.util.Optional; 35 import java.util.stream.Stream; 36 37 38 /** 39 * Provides access to the content of a module. 40 * 41 * <p> A module reader is intended for cases where access to the resources in a 42 * module is required, regardless of whether the module has been loaded. 43 * A framework that scans a collection of packaged modules on the file system, 44 * for example, may use a module reader to access a specific resource in each 45 * module. A module reader is also intended to be used by {@code ClassLoader} 46 * implementations that load classes and resources from modules. </p> 47 * 48 * <p> A resource in a module is identified by an abstract name that is a 49 * '{@code /}'-separated path string. For example, module {@code java.base} may 50 * have a resource "{@code java/lang/Object.class}" that, by convention, is the 51 * class file for {@code java.lang.Object}. </p> 52 * 53 * <p> A {@code ModuleReader} is {@linkplain ModuleReference#open open} upon 54 * creation and is closed by invoking the {@link #close close} method. Failure 55 * to close a module reader may result in a resource leak. The {@code 56 * try-with-resources} statement provides a useful construct to ensure that 57 * module readers are closed. </p> 58 * 59 * <p> A {@code ModuleReader} implementation may require permissions to access 60 * resources in the module. Consequently the {@link #find find}, {@link #open 61 * open}, {@link #read read}, and {@link #list list} methods may throw {@code 62 * SecurityException} if access is denied by the security manager. </p> 63 * 64 * @implSpec Implementations of {@code ModuleReader} should take great care 65 * when translating an abstract resource name to the location of a resource in 66 * a packaged module or on the file system. Implementations are advised to 67 * treat resource names with elements such as '{@code .}, '{@code ..}', 68 * elements containing file separators, or empty elements as "not found". More 69 * generally, if the resource name is not in the stream of elements that the 70 * {@code list} method returns then the resource should be treated as "not 71 * found" to avoid inconsistencies. 72 * 73 * @see ModuleReference 74 * @since 9 75 * @spec JPMS 76 */ 77 78 public interface ModuleReader extends Closeable { 79 80 /** 81 * Finds a resource, returning a URI to the resource in the module. 82 * 83 * @param name 84 * The name of the resource to open for reading 85 * 86 * @return A URI to the resource; an empty {@code Optional} if the resource 87 * is not found or a URI cannot be constructed to locate the 88 * resource 89 * 90 * @throws IOException 91 * If an I/O error occurs or the module reader is closed 92 * @throws SecurityException 93 * If denied by the security manager 94 * 95 * @see ClassLoader#getResource(String) 96 */ 97 Optional<URI> find(String name) throws IOException; 98 99 /** 100 * Opens a resource, returning an input stream to read the resource in 101 * the module. 102 * 103 * <p> The behavior of the input stream when used after the module reader 104 * is closed is implementation specific and therefore not specified. </p> 105 * 106 * @implSpec The default implementation invokes the {@link #find(String) 107 * find} method to get a URI to the resource. If found, then it attempts 108 * to construct a {@link java.net.URL URL} and open a connection to the 109 * resource. 110 * 111 * @param name 112 * The name of the resource to open for reading 113 * 114 * @return An input stream to read the resource or an empty 115 * {@code Optional} if not found 116 * 117 * @throws IOException 118 * If an I/O error occurs or the module reader is closed 119 * @throws SecurityException 120 * If denied by the security manager 121 */ 122 default Optional<InputStream> open(String name) throws IOException { 123 Optional<URI> ouri = find(name); 124 if (ouri.isPresent()) { 125 return Optional.of(ouri.get().toURL().openStream()); 126 } else { 127 return Optional.empty(); 128 } 129 } 130 131 /** 132 * Reads a resource, returning a byte buffer with the contents of the 133 * resource. 134 * 135 * The element at the returned buffer's position is the first byte of the 136 * resource, the element at the buffer's limit is the last byte of the 137 * resource. Once consumed, the {@link #release(ByteBuffer) release} method 138 * must be invoked. Failure to invoke the {@code release} method may result 139 * in a resource leak. 140 * 141 * @apiNote This method is intended for high-performance class loading. It 142 * is not capable (or intended) to read arbitrary large resources that 143 * could potentially be 2GB or larger. The rational for using this method 144 * in conjunction with the {@code release} method is to allow module reader 145 * implementations manage buffers in an efficient manner. 146 * 147 * @implSpec The default implementation invokes the {@link #open(String) 148 * open} method and reads all bytes from the input stream into a byte 149 * buffer. 150 * 151 * @param name 152 * The name of the resource to read 153 * 154 * @return A byte buffer containing the contents of the resource or an 155 * empty {@code Optional} if not found 156 * 157 * @throws IOException 158 * If an I/O error occurs or the module reader is closed 159 * @throws SecurityException 160 * If denied by the security manager 161 * @throws OutOfMemoryError 162 * If the resource is larger than {@code Integer.MAX_VALUE}, 163 * the maximum capacity of a byte buffer 164 * 165 * @see ClassLoader#defineClass(String, ByteBuffer, java.security.ProtectionDomain) 166 */ 167 default Optional<ByteBuffer> read(String name) throws IOException { 168 Optional<InputStream> oin = open(name); 169 if (oin.isPresent()) { 170 try (InputStream in = oin.get()) { 171 return Optional.of(ByteBuffer.wrap(in.readAllBytes())); 172 } 173 } else { 174 return Optional.empty(); 175 } 176 } 177 178 /** 179 * Release a byte buffer. This method should be invoked after consuming 180 * the contents of the buffer returned by the {@code read} method. 181 * The behavior of this method when invoked to release a buffer that has 182 * already been released, or the behavior when invoked to release a buffer 183 * after a {@code ModuleReader} is closed is implementation specific and 184 * therefore not specified. 185 * 186 * @param bb 187 * The byte buffer to release 188 * 189 * @implSpec The default implementation doesn't do anything except check 190 * if the byte buffer is null. 191 */ 192 default void release(ByteBuffer bb) { 193 Objects.requireNonNull(bb); 194 } 195 196 /** 197 * Lists the contents of the module, returning a stream of elements that 198 * are the names of all resources in the module. 199 * 200 * <p> In lazy implementations then an {@code IOException} may be thrown 201 * when using the stream to list the module contents. If this occurs then 202 * the {@code IOException} will be wrapped in an {@link 203 * java.io.UncheckedIOException} and thrown from the method that caused the 204 * access to be attempted. {@code SecurityException} may also be thrown 205 * when using the stream to list the module contents and access is denied 206 * by the security manager. </p> 207 * 208 * <p> The behavior of the stream when used after the module reader is 209 * closed is implementation specific and therefore not specified. </p> 210 * 211 * @return A stream of elements that are the names of all resources 212 * in the module 213 * 214 * @throws IOException 215 * If an I/O error occurs or the module reader is closed 216 * @throws SecurityException 217 * If denied by the security manager 218 */ 219 Stream<String> list() throws IOException; 220 221 /** 222 * Closes the module reader. Once closed then subsequent calls to locate or 223 * read a resource will fail by throwing {@code IOException}. 224 * 225 * <p> A module reader is not required to be asynchronously closeable. If a 226 * thread is reading a resource and another thread invokes the close method, 227 * then the second thread may block until the read operation is complete. </p> 228 */ 229 @Override 230 void close() throws IOException; 231 232 }