Class FileResource

All Implemented Interfaces:
AsyncResource, Resource, AutoCloseable

public class FileResource extends AbstractAsyncResource

A Resource implementation that identifies resources by a URI of the form file:/path/to/resource and looks up data on the file system.

A typical usage is:


 FileResource resource = new FileResource(URI.create("file:/path/to/resource"));
 ...
 
Since:
1.0
Author:
Jeremy Kuhn
See Also:
  • Field Details

  • Constructor Details

    • FileResource

      public FileResource(URI uri) throws IllegalArgumentException

      Creates a file resource with the specified URI.

      Parameters:
      uri - the resource URI
      Throws:
      IllegalArgumentException - if the specified URI does not designate a file resource
    • FileResource

      public FileResource(File file)

      Creates a file resource from the specified file.

      Parameters:
      file - a file
    • FileResource

      public FileResource(String pathname)

      Creates a file resource from the specified path name.

      Parameters:
      pathname - a path to a file
    • FileResource

      public FileResource(Path path)

      Creates a file resource from the specified path.

      Parameters:
      path - a path to a file
    • FileResource

      public FileResource(URI uri, MediaTypeService mediaTypeService) throws IllegalArgumentException

      Creates a file resource with the specified URI and media type service.

      Parameters:
      uri - the resource URI
      mediaTypeService - a media type service
      Throws:
      IllegalArgumentException - if the specified URI does not designate a file resource
    • FileResource

      public FileResource(File file, MediaTypeService mediaTypeService)

      Creates a file resource from the specified file with the specified media type service.

      Parameters:
      file - a file
      mediaTypeService - a media type service
    • FileResource

      public FileResource(String pathname, MediaTypeService mediaTypeService)

      Creates a file resource from the specified path name with the specified media type service.

      Parameters:
      pathname - a path to a file
      mediaTypeService - a media type service
    • FileResource

      public FileResource(Path path, MediaTypeService mediaTypeService)

      Creates a file resource from the specified path with the specified media type service.

      Parameters:
      path - a path to a file
      mediaTypeService - a media type service
  • Method Details

    • checkUri

      public static URI checkUri(URI uri) throws IllegalArgumentException

      Checks that the specified URI is a file resource URI.

      Parameters:
      uri - the uri to check
      Returns:
      the uri if it is a file resource URI
      Throws:
      IllegalArgumentException - if the specified URI does not designate a file resource
    • setExecutor

      public void setExecutor(ExecutorService executor)
      Description copied from interface: AsyncResource

      Sets the executor service to use when reading or writing the resource asynchronously.

      Specified by:
      setExecutor in interface AsyncResource
      Overrides:
      setExecutor in class AbstractAsyncResource
      Parameters:
      executor - the executor service to set
    • getURI

      public URI getURI()
      Description copied from interface: Resource

      Returns the resource URI.

      Returns:
      the resource URI
    • getFilename

      public String getFilename() throws ResourceException
      Description copied from interface: Resource

      Returns the resource file name.

      Returns:
      the resource file name
      Throws:
      ResourceException - if there was an error resolving the resource file name
    • getMediaType

      public String getMediaType() throws ResourceException
      Description copied from interface: Resource

      Returns the resource media type.

      Specified by:
      getMediaType in interface Resource
      Overrides:
      getMediaType in class AbstractResource
      Returns:
      the resource media type or null if it couldn't be determined
      Throws:
      ResourceException - if there was an error resolving the resource media type
    • exists

      public Optional<Boolean> exists() throws ResourceException
      Description copied from interface: Resource

      Determines whether the resource exists.

      Returns:
      an optional returning true if the resource exists, false otherwise or an empty optional if existence couldn't be determined
      Throws:
      ResourceException - if there was an error determining resource existence
    • isFile

      public Optional<Boolean> isFile() throws ResourceException
      Description copied from interface: Resource

      Determines whether this resource represents a file.

      A file resource is a resource that can be accessed through a FileChannel.

      Returns:
      an optional returning true if the resource is a file, false otherwise or an empty optional if it couldn't be determined
      Throws:
      ResourceException - if there was an error determining whether the resource is a file
    • lastModified

      public Optional<FileTime> lastModified() throws ResourceException
      Description copied from interface: Resource

      Returns the resource last modified time stamp.

      Returns:
      an optional returning the resource last modified time stamp or an empty optional if it couldn't be determined
      Throws:
      ResourceException - if there was an error resolving resource last modified time stamp
    • size

      public Optional<Long> size() throws ResourceException
      Description copied from interface: Resource

      Returns the resource content size.

      Returns:
      an optional returning the resource content size or an empty optional if it couldn't be determined
      Throws:
      ResourceException - if there was an error resolving resource content size
    • openReadableByteChannel

      public Optional<ReadableByteChannel> openReadableByteChannel() throws ResourceException
      Description copied from interface: Resource

      Opens a readable byte channel to the resource.

      The caller is responsible for closing the channel to prevent resource leak.

      Returns:
      an optional returning a readable byte channel or an empty optional if the resource is not readable
      Throws:
      ResourceException - if there was an error opening the readable byte channel
    • openWritableByteChannel

      public Optional<WritableByteChannel> openWritableByteChannel(boolean append, boolean createParents) throws ResourceException
      Description copied from interface: Resource

      Opens a writable byte channel to the resource that will append or not content to an existing resource and create or not missing parent directories.

      The caller is responsible for closing the channel to prevent resource leak.

      Parameters:
      append - true to append content to an existing resource
      createParents - true to create missing parent directories
      Returns:
      an optional returning a writable byte channel or an empty optional if the resource is not writable
      Throws:
      ResourceException - if there was an error opening the writable byte channel
    • read

      Description copied from interface: Resource

      Reads the resource in a reactive way.

      Specified by:
      read in interface Resource
      Overrides:
      read in class AbstractAsyncResource
      Returns:
      a stream of ByteBuf
      Throws:
      NotReadableResourceException - if the resource is not readable
      ResourceException - if there was an error reading the resource
    • write

      public Publisher<Integer> write(Publisher<ByteBuf> data, boolean append, boolean createParents) throws NotWritableResourceException, ResourceException
      Description copied from interface: Resource

      Writes content to the resource in a reactive way appending or not content to an existing resource and create or not missing parent directories.

      Specified by:
      write in interface Resource
      Overrides:
      write in class AbstractAsyncResource
      Parameters:
      data - the stream of data to write
      append - true to append content to an existing resource
      createParents - true to create missing parent directories
      Returns:
      a stream of integer emitting number of bytes written
      Throws:
      ResourceException - if there was an error writing to the resource
      NotWritableResourceException
    • delete

      public boolean delete() throws ResourceException
      Description copied from interface: Resource

      Deletes the resource.

      Returns:
      true if the resource had been deleted, false otherwise
      Throws:
      ResourceException - if there was an error deleting to the resource
    • resolve

      public Resource resolve(Path path) throws ResourceException
      Description copied from interface: Resource

      Resolves the specified URI against the resource URI as defined by Path.resolve(Path).

      Parameters:
      path - the path to resolve
      Returns:
      a new resource resulting from the resolution of the specified path against the resource
      Throws:
      ResourceException - if there was an error resolving the resource
    • close

      public void close()