public class UrlResource extends AbstractFileResolvingResource
Resource
implementation for java.net.URL
locators.
Supports resolution as a URL
and also as a File
in
case of the "file:"
protocol.URL
Constructor and Description |
---|
UrlResource(String path)
Create a new
UrlResource based on a URL path. |
UrlResource(String protocol,
String location)
Create a new
UrlResource based on a URI specification. |
UrlResource(String protocol,
String location,
String fragment)
Create a new
UrlResource based on a URI specification. |
UrlResource(URI uri)
Create a new
UrlResource based on the given URI object. |
UrlResource(URL url)
Create a new
UrlResource based on the given URL object. |
Modifier and Type | Method and Description |
---|---|
Resource |
createRelative(String relativePath)
This implementation creates a
UrlResource , delegating to
createRelativeURL(String) for adapting the relative path. |
protected URL |
createRelativeURL(String relativePath)
This delegate creates a
java.net.URL , applying the given path
relative to the path of the underlying URL of this resource descriptor. |
boolean |
equals(Object other)
This implementation compares the underlying URL references.
|
String |
getDescription()
This implementation returns a description that includes the URL.
|
File |
getFile()
This implementation returns a File reference for the underlying URL/URI,
provided that it refers to a file in the file system.
|
String |
getFilename()
This implementation returns the name of the file that this URL refers to.
|
InputStream |
getInputStream()
This implementation opens an InputStream for the given URL.
|
URI |
getURI()
This implementation returns the underlying URI directly,
if possible.
|
URL |
getURL()
This implementation returns the underlying URL reference.
|
int |
hashCode()
This implementation returns the hash code of the underlying URL reference.
|
boolean |
isFile()
This implementation always returns
false . |
contentLength, customizeConnection, customizeConnection, exists, getFile, getFileForLastModifiedCheck, isFile, isReadable, lastModified, readableChannel
isOpen, toString
public UrlResource(URL url)
UrlResource
based on the given URL object.url
- a URLUrlResource(URI)
,
UrlResource(String)
public UrlResource(URI uri) throws MalformedURLException
UrlResource
based on the given URI object.uri
- a URIMalformedURLException
- if the given URL path is not validpublic UrlResource(String path) throws MalformedURLException
UrlResource
based on a URL path.
Note: The given path needs to be pre-encoded if necessary.
path
- a URL pathMalformedURLException
- if the given URL path is not validURL(String)
public UrlResource(String protocol, String location) throws MalformedURLException
UrlResource
based on a URI specification.
The given parts will automatically get encoded if necessary.
protocol
- the URL protocol to use (e.g. "jar" or "file" - without colon);
also known as "scheme"location
- the location (e.g. the file path within that protocol);
also known as "scheme-specific part"MalformedURLException
- if the given URL specification is not validURI(String, String, String)
public UrlResource(String protocol, String location, @Nullable String fragment) throws MalformedURLException
UrlResource
based on a URI specification.
The given parts will automatically get encoded if necessary.
protocol
- the URL protocol to use (e.g. "jar" or "file" - without colon);
also known as "scheme"location
- the location (e.g. the file path within that protocol);
also known as "scheme-specific part"fragment
- the fragment within that location (e.g. anchor on an HTML page,
as following after a "#" separator)MalformedURLException
- if the given URL specification is not validURI(String, String, String)
public InputStream getInputStream() throws IOException
It sets the useCaches
flag to false
,
mainly to avoid jar file locking on Windows.
null
)FileNotFoundException
- if the underlying resource does not existIOException
- if the content stream could not be openedURL.openConnection()
,
URLConnection.setUseCaches(boolean)
,
URLConnection.getInputStream()
public URL getURL()
getURL
in interface Resource
getURL
in class AbstractResource
public URI getURI() throws IOException
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 descriptorpublic boolean isFile()
AbstractResource
false
.isFile
in interface Resource
isFile
in class AbstractFileResolvingResource
Resource.getFile()
public File getFile() throws IOException
getFile
in interface Resource
getFile
in class AbstractFileResolvingResource
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 Resource createRelative(String relativePath) throws MalformedURLException
UrlResource
, delegating to
createRelativeURL(String)
for adapting the relative path.createRelative
in interface Resource
createRelative
in class AbstractResource
relativePath
- the relative path (relative to this resource)MalformedURLException
createRelativeURL(String)
protected URL createRelativeURL(String relativePath) throws MalformedURLException
java.net.URL
, applying the given path
relative to the path of the underlying URL of this resource descriptor.
A leading slash will get dropped; a "#" symbol will get encoded.MalformedURLException
createRelative(String)
,
URL(java.net.URL, String)
public String getFilename()
getFilename
in interface Resource
getFilename
in class AbstractResource
URL.getPath()
public String getDescription()
Object.toString()
public boolean equals(@Nullable Object other)
equals
in class AbstractResource
Resource.getDescription()
public int hashCode()
hashCode
in class AbstractResource
Resource.getDescription()