com.threerings.resource
Class FileResourceBundle

java.lang.Object
  extended by com.threerings.resource.ResourceBundle
      extended by com.threerings.resource.FileResourceBundle

public class FileResourceBundle
extends ResourceBundle

A resource bundle provides access to the resources in a jar file.


Field Summary
protected  File _cache
          A directory into which we unpack files from our bundle.
protected  JarFile _jarSource
          The jar file from which we load resources.
protected  File _source
          The file from which we construct our jar file.
protected  long _sourceLastMod
          The last modified time of our source jar file.
protected static File _tmpdir
          A directory in which we temporarily unpack our resource files.
protected  File _unpacked
          A file whose timestamp indicates whether or not our existing jar file has been unpacked.
 
Constructor Summary
FileResourceBundle(File source)
          Constructs a resource bundle with the supplied jar file.
FileResourceBundle(File source, boolean delay, boolean unpack)
          Constructs a resource bundle with the supplied jar file.
 
Method Summary
protected  void closeJar()
          Closes our (possibly opened) jar file.
 boolean containsResource(String path)
          Returns true if this resource bundle contains the resource with the specified path.
static File getCacheDir()
          Returns the cache directory used for unpacked resources.
 String getIdent()
          Uniquely identifies this resource bundle.
 BufferedImage getImageResource(String path, boolean useFastIO)
          Decodes and returns the specified image resource.
 InputStream getResource(String path)
          Fetches the named resource from this bundle.
 File getResourceFile(String path)
          Returns a file from which the specified resource can be loaded.
 File getSource()
          Returns the File from which resources are fetched for this bundle.
 boolean isUnpacked()
           
protected  boolean resolveJarFile()
          Creates the internal jar file reference if we've not already got it; we do this lazily so as to avoid any jar- or zip-file-related antics until and unless doing so is required, and because the resource manager would like to be able to create bundles before the associated files have been fully downloaded.
static void setCacheDir(File tmpdir)
          Specifies the directory in which our temporary resource files should be stored.
 boolean sourceIsReady()
          Called by the resource manager once it has ensured that our resource jar file is up to date and ready for reading.
protected static String stripSuffix(String path)
          Strips the .jar off of jar file paths.
 String toString()
           
 void wipeBundle(boolean deleteJar)
          Clears out everything associated with this resource bundle in the hopes that we can download it afresh and everything will work the next time around.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_source

protected File _source
The file from which we construct our jar file.


_sourceLastMod

protected long _sourceLastMod
The last modified time of our source jar file.


_unpacked

protected File _unpacked
A file whose timestamp indicates whether or not our existing jar file has been unpacked.


_cache

protected File _cache
A directory into which we unpack files from our bundle.


_jarSource

protected JarFile _jarSource
The jar file from which we load resources.


_tmpdir

protected static File _tmpdir
A directory in which we temporarily unpack our resource files.

Constructor Detail

FileResourceBundle

public FileResourceBundle(File source)
Constructs a resource bundle with the supplied jar file.

Parameters:
source - a file object that references our source jar file.

FileResourceBundle

public FileResourceBundle(File source,
                          boolean delay,
                          boolean unpack)
Constructs a resource bundle with the supplied jar file.

Parameters:
source - a file object that references our source jar file.
delay - if true, the bundle will wait until someone calls sourceIsReady() before allowing access to its resources.
unpack - if true the bundle will unpack itself into a temporary directory
Method Detail

getIdent

public String getIdent()
Description copied from class: ResourceBundle
Uniquely identifies this resource bundle. Used to create cache keys.

Specified by:
getIdent in class ResourceBundle

getResource

public InputStream getResource(String path)
                        throws IOException
Description copied from class: ResourceBundle
Fetches the named resource from this bundle. The path should be specified as a relative, platform independent path (forward slashes). For example sounds/scream.au.

Specified by:
getResource in class ResourceBundle
Parameters:
path - the path to the resource in this jar file.
Returns:
an input stream from which the resource can be loaded or null if no such resource exists.
Throws:
IOException - thrown if an error occurs locating the resource in the jar file.

getImageResource

public BufferedImage getImageResource(String path,
                                      boolean useFastIO)
                               throws IOException
Description copied from class: ResourceBundle
Decodes and returns the specified image resource. Returns null if no resource exists at the specified path.

Specified by:
getImageResource in class ResourceBundle
Throws:
IOException

getSource

public File getSource()
Returns the File from which resources are fetched for this bundle.


isUnpacked

public boolean isUnpacked()
Returns:
true if the bundle is fully downloaded and successfully unpacked.

sourceIsReady

public boolean sourceIsReady()
Called by the resource manager once it has ensured that our resource jar file is up to date and ready for reading.

Returns:
true if we successfully unpacked our resources, false if we encountered errors in doing so.

wipeBundle

public void wipeBundle(boolean deleteJar)
Clears out everything associated with this resource bundle in the hopes that we can download it afresh and everything will work the next time around.


getResourceFile

public File getResourceFile(String path)
                     throws IOException
Returns a file from which the specified resource can be loaded. This method will unpack the resource into a temporary directory and return a reference to that file.

Parameters:
path - the path to the resource in this jar file.
Returns:
a file from which the resource can be loaded or null if no such resource exists.
Throws:
IOException

containsResource

public boolean containsResource(String path)
Returns true if this resource bundle contains the resource with the specified path. This avoids actually loading the resource, in the event that the caller only cares to know that the resource exists.


toString

public String toString()
Overrides:
toString in class Object

resolveJarFile

protected boolean resolveJarFile()
                          throws IOException
Creates the internal jar file reference if we've not already got it; we do this lazily so as to avoid any jar- or zip-file-related antics until and unless doing so is required, and because the resource manager would like to be able to create bundles before the associated files have been fully downloaded.

Returns:
true if the jar file could not yet be resolved because we haven't yet heard from the resource manager that it is ready for us to access, false if all is cool.
Throws:
IOException

closeJar

protected void closeJar()
Closes our (possibly opened) jar file.


getCacheDir

public static File getCacheDir()
Returns the cache directory used for unpacked resources.


setCacheDir

public static void setCacheDir(File tmpdir)
Specifies the directory in which our temporary resource files should be stored.


stripSuffix

protected static String stripSuffix(String path)
Strips the .jar off of jar file paths.