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