org.jicengine.io
Class AbstractResource

java.lang.Object
  extended byorg.jicengine.io.AbstractResource
All Implemented Interfaces:
Resource
Direct Known Subclasses:
ClasspathResource, ClassResource, FileResource, StringResource, UrlResource, WebApplicationResource

public abstract class AbstractResource
extends java.lang.Object
implements Resource

implements the most general methods of the Resource-interface.

Version:
1.0
Author:
.timo

Constructor Summary
protected AbstractResource(java.lang.String identifier)
           
 
Method Summary
 java.lang.String getIdentifier()
          Returns the identifier of this resource.
abstract  java.io.InputStream getInputStream()
          A primary way reading the resource.
 java.lang.String getMimeType()
          Returns the mime-type set with setMimeType().
 java.io.Reader getReader()
           Simple implementation of getReader() that wraps the InputStream obtained from getInputStream() with a InputStreamReader.
abstract  Resource getResource(java.lang.String relativePath)
           Locates another Resource whose path is defined relative to this Resource.
 boolean isAvailable()
           Determines the availability of the Resource by calling getInputStream(): resource is available if the method doesn't throw an exception.
 void setMimeType(java.lang.String mimeType)
           
 java.lang.String toString()
           
 void writeTo(java.io.OutputStream out)
          Writes the content of this resource into an OutputStream.
 void writeTo(java.io.Writer writer)
           Writes the content of this resource into a Writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractResource

protected AbstractResource(java.lang.String identifier)
Method Detail

getInputStream

public abstract java.io.InputStream getInputStream()
                                            throws java.io.IOException
Description copied from interface: Resource
A primary way reading the resource.

Specified by:
getInputStream in interface Resource
Returns:
InputStream
Throws:
java.io.IOException - if the reading fails - if the resource doesn't exist, for example.

getReader

public java.io.Reader getReader()
                         throws java.io.IOException

Simple implementation of getReader() that wraps the InputStream obtained from getInputStream() with a InputStreamReader.

Override this method if a better alternative for creating the Reader is available.

Specified by:
getReader in interface Resource
Returns:
A Reader that returns data from this Resource.
Throws:
java.io.IOException - if the reading fails - if the resource doesn't exist, for example.

isAvailable

public boolean isAvailable()

Determines the availability of the Resource by calling getInputStream(): resource is available if the method doesn't throw an exception.

Override this if a more efficient way for determining the availability is possible.

Specified by:
isAvailable in interface Resource

writeTo

public void writeTo(java.io.OutputStream out)
             throws java.io.IOException
Description copied from interface: Resource
Writes the content of this resource into an OutputStream. This is an alternative way for obtaining the data of this Resource.

Specified by:
writeTo in interface Resource
Throws:
java.io.IOException - if the content of this Resource isn't available - if the resource doesn't exist, for example.

writeTo

public void writeTo(java.io.Writer writer)
             throws java.io.IOException
Description copied from interface: Resource

Writes the content of this resource into a Writer. This is an alternative way for obtaining the data of this Resource.

Specified by:
writeTo in interface Resource
Throws:
java.io.IOException - if the content of this Resource isn't available - if the resource doesn't exist, for example.

getIdentifier

public java.lang.String getIdentifier()
Description copied from interface: Resource
Returns the identifier of this resource. Depending on the kind of resource, the identifier could be a file-path, url, etc. The identifier is descriptive - it will help a human to find out what kind of resource is in question, but don't rely in the format and try to create a resource based on the identifier. the format varies and may be changed in the future.

Specified by:
getIdentifier in interface Resource

setMimeType

public void setMimeType(java.lang.String mimeType)

getMimeType

public java.lang.String getMimeType()
Returns the mime-type set with setMimeType().

Specified by:
getMimeType in interface Resource
Returns:
NOTE: this property is optional! null is returned if the mime-type information is not available. and most in cases it probably isn't.

getResource

public abstract Resource getResource(java.lang.String relativePath)
                              throws java.io.IOException
Description copied from interface: Resource

Locates another Resource whose path is defined relative to this Resource.

the path scheme used with files and urls is used for specifying relative paths.

the method generally returns instances of the same Resource-subclass than the current instance, but this is not obligatory.

NOTE:

NOTE: there is no support for absolute paths. yet.

Specified by:
getResource in interface Resource
Parameters:
relativePath - name of the neighbouring resource. only relative paths are allowed, don't put the root mark '/' in the beginning. notations like '../' can be used (in most of the cases, at least) Windows-like paths '\joku\jotain.txt' won't work.
Returns:
a new Resource. The returned Resource is most likely of the same type as this resource (although there's no guarantee). In other words, if this Resource is a FileResource, the returned Resource will also be a FileResource. NOTE: this method doesn't necessary check the availability of the relative resource, because that may be too slow. if you want to make sure that the returned resource is available, you must examine the availability of the returned resource by your self.
Throws:
java.io.IOException - if a reference to the neighbouring resource couldn't be created.

toString

public java.lang.String toString()
Returns:
the identifier