AutoCloseable
, Closeable
public interface ModuleReader extends Closeable
A module reader is intended for cases where access to the resources in a
module is required, regardless of whether the module has been loaded.
A framework that scans a collection of packaged modules on the file system,
for example, may use a module reader to access a specific resource in each
module. A module reader is also intended to be used by ClassLoader
implementations that load classes and resources from modules.
A ModuleReader
is open upon
creation and is closed by invoking the close
method. Failure
to close a module reader may result in a resource leak. The
try-with-resources
statement provides a useful construct to ensure that
module readers are closed.
A ModuleReader
implementation may require permissions to access
resources in the module. Consequently the find
, open
and read
methods may throw SecurityException
if
access is denied by the security manager.
ModuleReference
Modifier and Type | Method | Description |
---|---|---|
void |
close() |
Closes the module reader.
|
Optional<URI> |
find(String name) |
Finds a resource, returning a URI to the resource in the module.
|
default Optional<InputStream> |
open(String name) |
Opens a resource, returning an input stream to read the resource in
the module.
|
default Optional<ByteBuffer> |
read(String name) |
Reads a resource, returning a byte buffer with the contents of the
resource.
|
default void |
release(ByteBuffer bb) |
Release a byte buffer.
|
Optional<URI> find(String name) throws IOException
name
- The name of the resource to open for readingOptional
if the resource
is not found or a URI cannot be constructed to locate the
resourceIOException
- If an I/O error occurs or the module reader is closedSecurityException
- If denied by the security managerClassLoader.getResource(String)
default Optional<InputStream> open(String name) throws IOException
find
method to get a URI to the resource. If found, then it attempts
to construct a URL
and open a connection to the
resource.name
- The name of the resource to open for readingOptional
if not foundIOException
- If an I/O error occurs or the module reader is closedSecurityException
- If denied by the security managerdefault Optional<ByteBuffer> read(String name) throws IOException
release
method
must be invoked. Failure to invoke the release
method may result
in a resource leak.release
method is to allow module reader
implementations manage buffers in an efficient manner.open
method and reads all bytes from the input stream into a byte
buffer.name
- The name of the resource to readOptional
if not foundIOException
- If an I/O error occurs or the module reader is closedSecurityException
- If denied by the security managerClassLoader.defineClass(String, ByteBuffer, java.security.ProtectionDomain)
default void release(ByteBuffer bb)
read
method.
The behavior of this method when invoked to release a buffer that has
already been released, or the behavior when invoked to release a buffer
after a ModuleReader
is closed is implementation specific and
therefore not specified.bb
- The byte buffer to releasevoid close() throws IOException
Optional.empty()
or
throwing IOException
.
A module reader is not required to be asynchronously closeable. If a thread is reading a resource and another thread invokes the close method, then the second thread may block until the read operation is complete.
The behavior of InputStream
s obtained using the open
method and used after the module reader is closed
is implementation specific and therefore not specified.
close
in interface AutoCloseable
close
in interface Closeable
IOException
- if an I/O error occurs Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2016, Oracle and/or its affiliates. All rights reserved.
DRAFT 9-internal+0-2016-10-04-161205.jjg.dev.8159855.tools-spi