85 *
86 * @implSpec The default implementation invokes the {@link #find(String)
87 * find} method to get a URI to the resource. If found, then it attempts
88 * to construct a {@link java.net.URL URL} and open a connection to the
89 * resource.
90 *
91 * @param name
92 * The name of the resource to open for reading
93 *
94 * @return An input stream to read the resource or an empty
95 * {@code Optional} if not found
96 *
97 * @throws IOException
98 * If an I/O error occurs or the module reader is closed
99 * @throws SecurityException
100 * If denied by the security manager
101 */
102 default Optional<InputStream> open(String name) throws IOException {
103 Optional<URI> ouri = find(name);
104 if (ouri.isPresent()) {
105 return Optional.of(ouri.get().toURL().openStream());
106 } else {
107 return Optional.empty();
108 }
109 }
110
111 /**
112 * Reads a resource, returning a byte buffer with the contents of the
113 * resource.
114 *
115 * The element at the returned buffer's position is the first byte of the
116 * resource, the element at the buffer's limit is the last byte of the
117 * resource. Once consumed, the {@link #release(ByteBuffer) release} method
118 * must be invoked. Failure to invoke the {@code release} method may result
119 * in a resource leak.
120 *
121 * @apiNote This method is intended for high-performance class loading. It
122 * is not capable (or intended) to read arbitrary large resources that
123 * could potentially be 2GB or larger. The rational for using this method
124 * in conjunction with the {@code release} method is to allow module reader
125 * implementations manage buffers in an efficient manner.
127 * @implSpec The default implementation invokes the {@link #open(String)
128 * open} method and reads all bytes from the input stream into a byte
129 * buffer.
130 *
131 * @param name
132 * The name of the resource to read
133 *
134 * @return A byte buffer containing the contents of the resource or an
135 * empty {@code Optional} if not found
136 *
137 * @throws IOException
138 * If an I/O error occurs or the module reader is closed
139 * @throws SecurityException
140 * If denied by the security manager
141 *
142 * @see ClassLoader#defineClass(String, ByteBuffer, java.security.ProtectionDomain)
143 */
144 default Optional<ByteBuffer> read(String name) throws IOException {
145 Optional<InputStream> in = open(name);
146 if (in.isPresent()) {
147 byte[] bytes = in.get().readAllBytes();
148 return Optional.of(ByteBuffer.wrap(bytes));
149 } else {
150 return Optional.empty();
151 }
152 }
153
154 /**
155 * Release a byte buffer. This method should be invoked after consuming
156 * the contents of the buffer returned by the {@code read} method.
157 * The behavior of this method when invoked to release a buffer that has
158 * already been released, or the behavior when invoked to release a buffer
159 * after a {@code ModuleReader} is closed is implementation specific and
160 * therefore not specified.
161 *
162 * @param bb
163 * The byte buffer to release
164 *
165 * @implSpec The default implementation does nothing.
166 */
167 default void release(ByteBuffer bb) { }
|
85 *
86 * @implSpec The default implementation invokes the {@link #find(String)
87 * find} method to get a URI to the resource. If found, then it attempts
88 * to construct a {@link java.net.URL URL} and open a connection to the
89 * resource.
90 *
91 * @param name
92 * The name of the resource to open for reading
93 *
94 * @return An input stream to read the resource or an empty
95 * {@code Optional} if not found
96 *
97 * @throws IOException
98 * If an I/O error occurs or the module reader is closed
99 * @throws SecurityException
100 * If denied by the security manager
101 */
102 default Optional<InputStream> open(String name) throws IOException {
103 Optional<URI> ouri = find(name);
104 if (ouri.isPresent()) {
105 return Optional.of(ouri.getWhenPresent().toURL().openStream());
106 } else {
107 return Optional.empty();
108 }
109 }
110
111 /**
112 * Reads a resource, returning a byte buffer with the contents of the
113 * resource.
114 *
115 * The element at the returned buffer's position is the first byte of the
116 * resource, the element at the buffer's limit is the last byte of the
117 * resource. Once consumed, the {@link #release(ByteBuffer) release} method
118 * must be invoked. Failure to invoke the {@code release} method may result
119 * in a resource leak.
120 *
121 * @apiNote This method is intended for high-performance class loading. It
122 * is not capable (or intended) to read arbitrary large resources that
123 * could potentially be 2GB or larger. The rational for using this method
124 * in conjunction with the {@code release} method is to allow module reader
125 * implementations manage buffers in an efficient manner.
127 * @implSpec The default implementation invokes the {@link #open(String)
128 * open} method and reads all bytes from the input stream into a byte
129 * buffer.
130 *
131 * @param name
132 * The name of the resource to read
133 *
134 * @return A byte buffer containing the contents of the resource or an
135 * empty {@code Optional} if not found
136 *
137 * @throws IOException
138 * If an I/O error occurs or the module reader is closed
139 * @throws SecurityException
140 * If denied by the security manager
141 *
142 * @see ClassLoader#defineClass(String, ByteBuffer, java.security.ProtectionDomain)
143 */
144 default Optional<ByteBuffer> read(String name) throws IOException {
145 Optional<InputStream> in = open(name);
146 if (in.isPresent()) {
147 byte[] bytes = in.getWhenPresent().readAllBytes();
148 return Optional.of(ByteBuffer.wrap(bytes));
149 } else {
150 return Optional.empty();
151 }
152 }
153
154 /**
155 * Release a byte buffer. This method should be invoked after consuming
156 * the contents of the buffer returned by the {@code read} method.
157 * The behavior of this method when invoked to release a buffer that has
158 * already been released, or the behavior when invoked to release a buffer
159 * after a {@code ModuleReader} is closed is implementation specific and
160 * therefore not specified.
161 *
162 * @param bb
163 * The byte buffer to release
164 *
165 * @implSpec The default implementation does nothing.
166 */
167 default void release(ByteBuffer bb) { }
|