public class FileUrlResource extends UrlResource implements WritableResource
UrlResource
which assumes file resolution, to the degree
of implementing the WritableResource
interface for it. This resource
variant also caches resolved File
handles from getFile()
.
This is the class resolved by DefaultResourceLoader
for a "file:..."
URL location, allowing a downcast to WritableResource
for it.
Alternatively, for direct construction from a File
handle,
consider using FileSystemResource
. For an NIO Path
,
consider using PathResource
instead.
Constructor and Description |
---|
FileUrlResource(String location)
Create a new
FileUrlResource based on the given file location,
using the URL protocol "file". |
FileUrlResource(URL url)
Create a new
FileUrlResource based on the given URL object. |
Modifier and Type | Method and Description |
---|---|
Resource |
createRelative(String relativePath)
This implementation creates a
UrlResource , applying the given path
relative to the path of the underlying URL of this resource descriptor. |
File |
getFile()
This implementation returns a File reference for the underlying URL/URI,
provided that it refers to a file in the file system.
|
OutputStream |
getOutputStream()
Return an
OutputStream for the underlying resource,
allowing to (over-)write its content. |
boolean |
isWritable()
Indicate whether the contents of this resource can be written
via
WritableResource.getOutputStream() . |
WritableByteChannel |
writableChannel()
Return a
WritableByteChannel . |
equals, getDescription, getFilename, getInputStream, getURI, getURL, hashCode, isFile
contentLength, customizeConnection, customizeConnection, exists, getFile, getFileForLastModifiedCheck, isFile, isReadable, lastModified, readableChannel
isOpen, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
contentLength, exists, getDescription, getFilename, getURI, getURL, isFile, isOpen, isReadable, lastModified, readableChannel
getInputStream
public FileUrlResource(URL url)
FileUrlResource
based on the given URL object.
Note that this does not enforce "file" as URL protocol. If a protocol is known to be resolvable to a file, it is acceptable for this purpose.
url
- a URLResourceUtils.isFileURL(URL)
,
getFile()
public FileUrlResource(String location) throws MalformedURLException
FileUrlResource
based on the given file location,
using the URL protocol "file".
The given parts will automatically get encoded if necessary.
location
- the location (i.e. the file path within that protocol)MalformedURLException
- if the given URL specification is not validUrlResource.UrlResource(String, String)
,
ResourceUtils.URL_PROTOCOL_FILE
public File getFile() throws IOException
UrlResource
getFile
in interface Resource
getFile
in class UrlResource
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 failuresResourceUtils.getFile(java.net.URL, String)
public boolean isWritable()
WritableResource
WritableResource.getOutputStream()
.
Will be true
for typical resource descriptors;
note that actual content writing may still fail when attempted.
However, a value of false
is a definitive indication
that the resource content cannot be modified.
isWritable
in interface WritableResource
WritableResource.getOutputStream()
,
Resource.isReadable()
public OutputStream getOutputStream() throws IOException
WritableResource
OutputStream
for the underlying resource,
allowing to (over-)write its content.getOutputStream
in interface WritableResource
IOException
- if the stream could not be openedInputStreamSource.getInputStream()
public WritableByteChannel writableChannel() throws IOException
WritableResource
WritableByteChannel
.
It is expected that each call creates a fresh channel.
The default implementation returns Channels.newChannel(OutputStream)
with the result of WritableResource.getOutputStream()
.
writableChannel
in interface WritableResource
null
)FileNotFoundException
- if the underlying resource doesn't existIOException
- if the content channel could not be openedWritableResource.getOutputStream()
public Resource createRelative(String relativePath) throws MalformedURLException
UrlResource
UrlResource
, applying the given path
relative to the path of the underlying URL of this resource descriptor.createRelative
in interface Resource
createRelative
in class UrlResource
relativePath
- the relative path (relative to this resource)MalformedURLException
URL.URL(java.net.URL, String)