Package org.springframework.core.io

# Class ModuleResource

java.lang.Object
org.springframework.core.io.AbstractResource
org.springframework.core.io.ModuleResource

All Implemented Interfaces:
  `InputStreamSource`, `Resource`

public class ModuleResource extends AbstractResource
`Resource` implementation for `Module` resolution, performing `getInputStream()` access via `Module.getResourceAsStream(java.lang.String)`.

Alternatively, consider accessing resources in a module path layout via `ClassPathResource` for exported resources, or specifically relative to a `Class` via `ClassPathResource(String, Class)` for local resolution within the containing module of that specific class. In common scenarios, module resources will simply be transparently visible as classpath resources and therefore do not need any special treatment at all.

Since:
  6.1
Author:
  Juergen Hoeller, Sam Brannen
See Also:
  - `Module.getResourceAsStream(java.lang.String)`
  - `ClassPathResource`

## Constructor Summary

Constructors
Constructor
Description
`ModuleResource(Module module, String path)`
Create a new `ModuleResource` for the given `Module` and the given resource path.

## Method Summary

Modifier and Type
Method
Description
`Resource`
`createRelative(String relativePath)`
This implementation throws a FileNotFoundException, assuming that relative resources cannot be created for this resource.
`boolean`
`equals(Object obj)`
This implementation compares description strings.
`String`
`getDescription()`
Return a description for this resource, to be used for error output when working with the resource.
`String`
`getFilename()`
This implementation always returns `null`, assuming that this resource type does not have a filename.
`InputStream`
`getInputStream()`
Return an `InputStream` for the content of an underlying resource.
`final Module`
`getModule()`
Return the `Module` for this resource.
`final String`
`getPath()`
Return the path for this resource.
`int`
`hashCode()`
This implementation returns the description's hash code.

### Methods inherited from class org.springframework.core.io.AbstractResource

`contentLength, exists, getFile, getFileForLastModifiedCheck, getURI, getURL, isFile, isOpen, isReadable, lastModified, readableChannel, toString`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.springframework.core.io.Resource

`getContentAsByteArray, getContentAsString`

## Constructor Details

### ModuleResource

public ModuleResource(Module module, String path)
Create a new `ModuleResource` for the given `Module` and the given resource path.

Parameters:
  `module` - the runtime module to search within
  `path` - the resource path within the module

## Method Details

### getModule

public final Module getModule()
Return the `Module` for this resource.

### getPath

public final String getPath()
Return the path for this resource.

### getInputStream

public InputStream getInputStream() throws IOException
Description copied from interface: `InputStreamSource`
Return an `InputStream` for the content of an underlying resource.

It is usually expected that every such call creates a fresh stream.

This requirement is particularly important when you consider an API such as JavaMail, which needs to be able to read the stream multiple times when creating mail attachments. For such a use case, it is required that each `getInputStream()` call returns a fresh stream.

Returns:
  the input stream for the underlying resource (must not be `null`)
Throws:
  `FileNotFoundException` - if the underlying resource does not exist
  `IOException` - if the content stream could not be opened
See Also:
  - `Resource.isReadable()`
  - `Resource.isOpen()`

### createRelative

public Resource createRelative(String relativePath)
Description copied from class: `AbstractResource`
This implementation throws a FileNotFoundException, assuming that relative resources cannot be created for this resource.

Specified by:
  `createRelative` in interface `Resource`
Overrides:
  `createRelative` in class `AbstractResource`
Parameters:
  `relativePath` - the relative path (relative to this resource)
Returns:
  the resource handle for the relative resource

### getFilename

@Nullable public String getFilename()
Description copied from class: `AbstractResource`
This implementation always returns `null`, assuming that this resource type does not have a filename.

Specified by:
  `getFilename` in interface `Resource`
Overrides:
  `getFilename` in class `AbstractResource`

### getDescription

public String getDescription()
Description copied from interface: `Resource`
Return a description for this resource, to be used for error output when working with the resource.

Implementations are also encouraged to return this value from their `toString` method.

See Also:
  - `Object.toString()`

### equals

public boolean equals(@Nullable Object obj)
Description copied from class: `AbstractResource`
This implementation compares description strings.

Overrides:
  `equals` in class `AbstractResource`
See Also:
  - `Resource.getDescription()`

### hashCode

public int hashCode()
Description copied from class: `AbstractResource`
This implementation returns the description's hash code.

Overrides:
  `hashCode` in class `AbstractResource`
See Also:
  - `Resource.getDescription()`