public class PathResource extends AbstractResource implements WritableResource
Resource
implementation for Path
handles,
performing all operations and transformations via the Path
API.
Supports resolution as a File
and also as a URL
.
Implements the extended WritableResource
interface.
Note: As of 5.1, Path
support is also available
in FileSystemResource
,
applying Spring's standard String-based path transformations but
performing all operations via the Files
API.
This PathResource
is effectively a pure java.nio.path.Path
based alternative with different createRelative
behavior.
Path
,
Files
,
FileSystemResource
Constructor and Description |
---|
PathResource(Path path)
Create a new
PathResource from a Path handle. |
PathResource(String path)
Create a new
PathResource from a path string. |
PathResource(URI uri)
Create a new
PathResource from a URI . |
Modifier and Type | Method and Description |
---|---|
long |
contentLength()
This implementation returns the underlying file's length.
|
Resource |
createRelative(String relativePath)
This implementation creates a
PathResource , applying the given path
relative to the path of the underlying file of this resource descriptor. |
boolean |
equals(Object other)
This implementation compares the underlying
Path references. |
boolean |
exists()
This implementation returns whether the underlying file exists.
|
String |
getDescription()
Return a description for this resource,
to be used for error output when working with the resource.
|
File |
getFile()
This implementation returns the underlying
File reference. |
String |
getFilename()
This implementation returns the name of the file.
|
InputStream |
getInputStream()
This implementation opens an
InputStream for the underlying file. |
OutputStream |
getOutputStream()
This implementation opens an
OutputStream for the underlying file. |
String |
getPath()
Return the file path for this resource.
|
URI |
getURI()
This implementation returns a
URI for the underlying file. |
URL |
getURL()
This implementation returns a
URL for the underlying file. |
int |
hashCode()
This implementation returns the hash code of the underlying
Path reference. |
boolean |
isFile()
This implementation always indicates a file.
|
boolean |
isReadable()
This implementation checks whether the underlying file is marked as readable
(and corresponds to an actual file with content, not to a directory).
|
boolean |
isWritable()
This implementation checks whether the underlying file is marked as writable
(and corresponds to an actual file with content, not to a directory).
|
long |
lastModified()
This implementation returns the underlying file's timestamp.
|
ReadableByteChannel |
readableChannel()
This implementation opens a
ReadableByteChannel for the underlying file. |
WritableByteChannel |
writableChannel()
This implementation opens a
WritableByteChannel for the underlying file. |
getFileForLastModifiedCheck, isOpen, toString
public PathResource(Path path)
PathResource
from a Path
handle.
Note: Unlike FileSystemResource
, when building relative resources
via createRelative(java.lang.String)
, the relative path will be built underneath
the given root: e.g. Paths.get("C:/dir1/"), relative path "dir2" → "C:/dir1/dir2"!
path
- a Path handlepublic PathResource(String path)
PathResource
from a path string.
Note: Unlike FileSystemResource
, when building relative resources
via createRelative(java.lang.String)
, the relative path will be built underneath
the given root: e.g. Paths.get("C:/dir1/"), relative path "dir2" → "C:/dir1/dir2"!
path
- a pathPaths.get(String, String...)
public PathResource(URI uri)
PathResource
from a URI
.
Note: Unlike FileSystemResource
, when building relative resources
via createRelative(java.lang.String)
, the relative path will be built underneath
the given root: e.g. Paths.get("C:/dir1/"), relative path "dir2" → "C:/dir1/dir2"!
uri
- a path URIPaths.get(URI)
public final String getPath()
public boolean exists()
exists
in interface Resource
exists
in class AbstractResource
Files.exists(Path, java.nio.file.LinkOption...)
public boolean isReadable()
isReadable
in interface Resource
isReadable
in class AbstractResource
Files.isReadable(Path)
,
Files.isDirectory(Path, java.nio.file.LinkOption...)
public InputStream getInputStream() throws IOException
InputStream
for the underlying file.getInputStream
in interface InputStreamSource
null
)FileNotFoundException
- if the underlying resource does not existIOException
- if the content stream could not be openedFileSystemProvider.newInputStream(Path, OpenOption...)
public boolean isWritable()
isWritable
in interface WritableResource
Files.isWritable(Path)
,
Files.isDirectory(Path, java.nio.file.LinkOption...)
public OutputStream getOutputStream() throws IOException
OutputStream
for the underlying file.getOutputStream
in interface WritableResource
IOException
- if the stream could not be openedFileSystemProvider.newOutputStream(Path, OpenOption...)
public URL getURL() throws IOException
URL
for the underlying file.getURL
in interface Resource
getURL
in class AbstractResource
IOException
- if the resource cannot be resolved as URL,
i.e. if the resource is not available as a descriptorPath.toUri()
,
URI.toURL()
public URI getURI() throws IOException
URI
for the underlying file.getURI
in interface Resource
getURI
in class AbstractResource
IOException
- if the resource cannot be resolved as URI,
i.e. if the resource is not available as a descriptorPath.toUri()
public boolean isFile()
isFile
in interface Resource
isFile
in class AbstractResource
Resource.getFile()
public File getFile() throws IOException
File
reference.getFile
in interface Resource
getFile
in class AbstractResource
FileNotFoundException
- if the resource cannot be resolved as
absolute file path, i.e. if the resource is not available in a file systemIOException
- in case of general resolution/reading failuresInputStreamSource.getInputStream()
public ReadableByteChannel readableChannel() throws IOException
ReadableByteChannel
for the underlying file.readableChannel
in interface Resource
readableChannel
in class AbstractResource
null
)FileNotFoundException
- if the underlying resource doesn't existIOException
- if the content channel could not be openedFiles.newByteChannel(Path, OpenOption...)
public WritableByteChannel writableChannel() throws IOException
WritableByteChannel
for the underlying file.writableChannel
in interface WritableResource
null
)FileNotFoundException
- if the underlying resource doesn't existIOException
- if the content channel could not be openedFiles.newByteChannel(Path, OpenOption...)
public long contentLength() throws IOException
contentLength
in interface Resource
contentLength
in class AbstractResource
IOException
- if the resource cannot be resolved
(in the file system or as some other known physical resource type)InputStreamSource.getInputStream()
public long lastModified() throws IOException
lastModified
in interface Resource
lastModified
in class AbstractResource
IOException
- if the resource cannot be resolved
(in the file system or as some other known physical resource type)Files.getLastModifiedTime(Path, java.nio.file.LinkOption...)
public Resource createRelative(String relativePath)
PathResource
, applying the given path
relative to the path of the underlying file of this resource descriptor.createRelative
in interface Resource
createRelative
in class AbstractResource
relativePath
- the relative path (relative to this resource)Path.resolve(String)
public String getFilename()
getFilename
in interface Resource
getFilename
in class AbstractResource
Path.getFileName()
public String getDescription()
Resource
Implementations are also encouraged to return this value
from their toString
method.
getDescription
in interface Resource
Object.toString()
public boolean equals(@Nullable Object other)
Path
references.equals
in class AbstractResource
Resource.getDescription()
public int hashCode()
Path
reference.hashCode
in class AbstractResource
Resource.getDescription()