Package org.springframework.core.io

# Class AbstractFileResolvingResource

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

All Implemented Interfaces:
  `InputStreamSource`, `Resource`

Direct Known Subclasses:
  `ClassPathResource`, `UrlResource`

public abstract class AbstractFileResolvingResource extends AbstractResource
Abstract base class for resources which resolve URLs into File references, such as `UrlResource` or `ClassPathResource`.

Detects the "file" protocol as well as the JBoss "vfs" protocol in URLs, resolving file system references accordingly.

Since:
  3.0
Author:
  Juergen Hoeller

## Constructor Summary

Constructors
Constructor
Description
`AbstractFileResolvingResource()`

## Method Summary

Modifier and Type
Method
Description
`long`
`contentLength()`
This method reads the entire InputStream to determine the content length.
`protected void`
`customizeConnection(HttpURLConnection con)`
Customize the given `HttpURLConnection` before fetching the resource.
`protected void`
`customizeConnection(URLConnection con)`
Customize the given `URLConnection` before fetching the resource.
`boolean`
`exists()`
This implementation checks whether a File can be opened, falling back to whether an InputStream can be opened.
`File`
`getFile()`
This implementation returns a File reference for the underlying class path resource, provided that it refers to a file in the file system.
`protected File`
`getFile(URI uri)`
This implementation returns a File reference for the given URI-identified resource, provided that it refers to a file in the file system.
`protected File`
`getFileForLastModifiedCheck()`
This implementation determines the underlying File (or jar file, in case of a resource in a jar/zip).
`boolean`
`isFile()`
This implementation always returns `false`.
`protected boolean`
`isFile(URI uri)`
Determine whether the given `URI` represents a file in a file system.
`boolean`
`isReadable()`
This implementation always returns `true` for a resource that `exists` (revised as of 5.1).
`long`
`lastModified()`
This implementation checks the timestamp of the underlying File, if available.
`ReadableByteChannel`
`readableChannel()`
This implementation returns a FileChannel for the given URI-identified resource, provided that it refers to a file in the file system.

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

`createRelative, equals, getFilename, getURI, getURL, hashCode, isOpen, toString`

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

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

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

`getInputStream`

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

`getContentAsByteArray, getContentAsString, getDescription`

## Constructor Details

### AbstractFileResolvingResource

public AbstractFileResolvingResource()

## Method Details

### exists

public boolean exists()
Description copied from class: `AbstractResource`
This implementation checks whether a File can be opened, falling back to whether an InputStream can be opened.

This will cover both directories and content resources.

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

### isReadable

public boolean isReadable()
Description copied from class: `AbstractResource`
This implementation always returns `true` for a resource that `exists` (revised as of 5.1).

Specified by:
  `isReadable` in interface `Resource`
Overrides:
  `isReadable` in class `AbstractResource`
See Also:
  - `InputStreamSource.getInputStream()`
  - `Resource.exists()`

### isFile

public boolean isFile()
Description copied from class: `AbstractResource`
This implementation always returns `false`.

Specified by:
  `isFile` in interface `Resource`
Overrides:
  `isFile` in class `AbstractResource`
See Also:
  - `Resource.getFile()`

### getFile

public File getFile() throws IOException
This implementation returns a File reference for the underlying class path resource, provided that it refers to a file in the file system.

Specified by:
  `getFile` in interface `Resource`
Overrides:
  `getFile` in class `AbstractResource`
Throws:
  `FileNotFoundException` - if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
  `IOException` - in case of general resolution/reading failures
See Also:
  - `ResourceUtils.getFile(java.net.URL, String)`

### getFileForLastModifiedCheck

protected File getFileForLastModifiedCheck() throws IOException
This implementation determines the underlying File (or jar file, in case of a resource in a jar/zip).

Overrides:
  `getFileForLastModifiedCheck` in class `AbstractResource`
Returns:
  the File to use for timestamp checking (never `null`)
Throws:
  `FileNotFoundException` - if the resource cannot be resolved as an absolute file path, i.e. is not available in a file system
  `IOException` - in case of general resolution/reading failures

### isFile

protected boolean isFile(URI uri)
Determine whether the given `URI` represents a file in a file system.

Since:
  5.0
See Also:
  - `getFile(URI)`

### getFile

protected File getFile(URI uri) throws IOException
This implementation returns a File reference for the given URI-identified resource, provided that it refers to a file in the file system.

Throws:
  `IOException`
See Also:
  - `ResourceUtils.getFile(java.net.URI, String)`

### readableChannel

public ReadableByteChannel readableChannel() throws IOException
This implementation returns a FileChannel for the given URI-identified resource, provided that it refers to a file in the file system.

Specified by:
  `readableChannel` in interface `Resource`
Overrides:
  `readableChannel` in class `AbstractResource`
Returns:
  the byte channel for the underlying resource (must not be `null`)
Throws:
  `FileNotFoundException` - if the underlying resource doesn't exist
  `IOException` - if the content channel could not be opened
Since:
  5.0
See Also:
  - `getFile()`

### contentLength

public long contentLength() throws IOException
Description copied from class: `AbstractResource`
This method reads the entire InputStream to determine the content length.

For a custom subclass of `InputStreamResource`, we strongly recommend overriding this method with a more optimal implementation, for example, checking File length, or possibly simply returning -1 if the stream can only be read once.

Specified by:
  `contentLength` in interface `Resource`
Overrides:
  `contentLength` in class `AbstractResource`
Throws:
  `IOException` - if the resource cannot be resolved (in the file system or as some other known physical resource type)
See Also:
  - `InputStreamSource.getInputStream()`

### lastModified

public long lastModified() throws IOException
Description copied from class: `AbstractResource`
This implementation checks the timestamp of the underlying File, if available.

Specified by:
  `lastModified` in interface `Resource`
Overrides:
  `lastModified` in class `AbstractResource`
Throws:
  `IOException` - if the resource cannot be resolved (in the file system or as some other known physical resource type)
See Also:
  - `AbstractResource.getFileForLastModifiedCheck()`

### customizeConnection

protected void customizeConnection(URLConnection con) throws IOException
Customize the given `URLConnection` before fetching the resource.

Calls `ResourceUtils.useCachesIfNecessary(URLConnection)` and delegates to `customizeConnection(HttpURLConnection)` if possible. Can be overridden in subclasses.

Parameters:
  `con` - the URLConnection to customize
Throws:
  `IOException` - if thrown from URLConnection methods

### customizeConnection

protected void customizeConnection(HttpURLConnection con) throws IOException
Customize the given `HttpURLConnection` before fetching the resource.

Can be overridden in subclasses for configuring request headers and timeouts.

Parameters:
  `con` - the HttpURLConnection to customize
Throws:
  `IOException` - if thrown from HttpURLConnection methods