Turns out I completely screwed up my measurements, and it is in fact

(much) faster to read the decompressed files as opposed to reading them
in-place from the jar.  At least I double-checked before going too
far.  In case you were wondering, it's *really* fast to simply check
a jar file for an entry that doesn't exist.


git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@1165 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Andrzej Kapolka
2011-05-09 20:47:20 +00:00
parent d46608becd
commit 692ca57250
2 changed files with 17 additions and 62 deletions
@@ -60,31 +60,12 @@ public class FileResourceBundle extends ResourceBundle
* @param source a file object that references our source jar file. * @param source a file object that references our source jar file.
* @param delay if true, the bundle will wait until someone calls {@link #sourceIsReady} * @param delay if true, the bundle will wait until someone calls {@link #sourceIsReady}
* before allowing access to its resources. * before allowing access to its resources.
* @param unpackFully if true the bundle will unpack itself fully. * @param unpack if true the bundle will unpack itself into a temporary directory
*/ */
public FileResourceBundle (File source, boolean delay, boolean unpackFully) public FileResourceBundle (File source, boolean delay, boolean unpack)
{
this(source, delay, unpackFully, true);
}
/**
* Constructs a resource bundle with the supplied jar file.
*
* @param source a file object that references our source jar file.
* @param delay if true, the bundle will wait until someone calls {@link #sourceIsReady}
* before allowing access to its resources.
* @param unpackFully if true the bundle will unpack itself fully.
* @param unpackEntries if true and unpackFully is false, individual entries will be
* unpacked into a temporary directory as needed (as opposed to being read directly from
* the jar file).
*/
public FileResourceBundle (
File source, boolean delay, boolean unpackFully, boolean unpackEntries)
{ {
_source = source; _source = source;
_unpackEntries = unpackEntries; if (unpack) {
if (unpackFully) {
String root = stripSuffix(source.getPath()); String root = stripSuffix(source.getPath());
_unpacked = new File(root + ".stamp"); _unpacked = new File(root + ".stamp");
_cache = new File(root); _cache = new File(root);
@@ -105,25 +86,17 @@ public class FileResourceBundle extends ResourceBundle
public InputStream getResource (String path) public InputStream getResource (String path)
throws IOException throws IOException
{ {
// if specified, unpack our resources into a temp directory so that // unpack our resources into a temp directory so that we can load
// we can load them quickly and the file system can cache them sensibly // them quickly and the file system can cache them sensibly
if (_unpackEntries) { File rfile = getResourceFile(path);
File rfile = getResourceFile(path); return (rfile == null) ? null : new FileInputStream(rfile);
return (rfile == null) ? null : new FileInputStream(rfile);
}
if (_jarSource == null && resolveJarFile()) {
return null;
}
JarEntry entry = _jarSource.getJarEntry(path);
return (entry == null) ? null : _jarSource.getInputStream(entry);
} }
@Override @Override
public BufferedImage getImageResource (String path, boolean useFastIO) public BufferedImage getImageResource (String path, boolean useFastIO)
throws IOException throws IOException
{ {
return _unpackEntries ? ResourceManager.loadImage(getResourceFile(path), useFastIO) : return ResourceManager.loadImage(getResourceFile(path), useFastIO);
ResourceManager.loadImage(getResource(path), useFastIO);
} }
/** /**
@@ -427,9 +400,6 @@ public class FileResourceBundle extends ResourceBundle
/** The jar file from which we load resources. */ /** The jar file from which we load resources. */
protected JarFile _jarSource; protected JarFile _jarSource;
/** Whether or not to unpack individual entries as needed from the jar file. */
protected boolean _unpackEntries;
/** A directory in which we temporarily unpack our resource files. */ /** A directory in which we temporarily unpack our resource files. */
protected static File _tmpdir; protected static File _tmpdir;
} }
@@ -211,7 +211,7 @@ public class ResourceManager
// check a system property to determine if we should unpack our bundles, but don't freak // check a system property to determine if we should unpack our bundles, but don't freak
// out if we fail to read it // out if we fail to read it
try { try {
_unpackFully = !Boolean.getBoolean("no_unpack_resources"); _unpack = !Boolean.getBoolean("no_unpack_resources");
} catch (SecurityException se) { } catch (SecurityException se) {
// no problem, we're in a sandbox so we definitely won't be unpacking // no problem, we're in a sandbox so we definitely won't be unpacking
} }
@@ -263,16 +263,7 @@ public class ResourceManager
*/ */
public void setUnpackResources (boolean unpackResources) public void setUnpackResources (boolean unpackResources)
{ {
_unpackFully = unpackResources; _unpack = unpackResources;
}
/**
* Configures whether we unpack individual jar entries as opposed to reading them directly from
* the jar.
*/
public void setUnpackEntries (boolean unpackEntries)
{
_unpackEntries = unpackEntries;
} }
/** /**
@@ -449,8 +440,7 @@ public class ResourceManager
public boolean checkBundle (String path) public boolean checkBundle (String path)
{ {
File bfile = getResourceFile(path); File bfile = getResourceFile(path);
return (bfile == null) ? false : new FileResourceBundle( return (bfile == null) ? false : new FileResourceBundle(bfile, true, _unpack).isUnpacked();
bfile, true, _unpackFully, _unpackEntries).isUnpacked();
} }
/** /**
@@ -469,8 +459,7 @@ public class ResourceManager
return; return;
} }
final FileResourceBundle bundle = new FileResourceBundle( final FileResourceBundle bundle = new FileResourceBundle(bfile, true, _unpack);
bfile, true, _unpackFully, _unpackEntries);
if (bundle.isUnpacked()) { if (bundle.isUnpacked()) {
if (bundle.sourceIsReady()) { if (bundle.sourceIsReady()) {
listener.requestCompleted(bundle); listener.requestCompleted(bundle);
@@ -823,8 +812,7 @@ public class ResourceManager
{ {
if (setType.equals(FILE_SET_TYPE)) { if (setType.equals(FILE_SET_TYPE)) {
FileResourceBundle bundle = FileResourceBundle bundle =
createFileResourceBundle(getResourceFile(path), createFileResourceBundle(getResourceFile(path), true, _unpack);
true, _unpackFully, _unpackEntries);
if (!bundle.isUnpacked() || !bundle.sourceIsReady()) { if (!bundle.isUnpacked() || !bundle.sourceIsReady()) {
dlist.add(bundle); dlist.add(bundle);
} }
@@ -840,9 +828,9 @@ public class ResourceManager
* Creates an appropriate bundle for fetching resources from files. * Creates an appropriate bundle for fetching resources from files.
*/ */
protected FileResourceBundle createFileResourceBundle (File source, boolean delay, protected FileResourceBundle createFileResourceBundle (File source, boolean delay,
boolean unpackFully, boolean unpackEntries) boolean unpack)
{ {
return new FileResourceBundle(source, delay, unpackFully, unpackEntries); return new FileResourceBundle(source, delay, unpack);
} }
/** /**
@@ -1078,11 +1066,8 @@ public class ResourceManager
/** The root path we give to network bundles for all resources they're interested in. */ /** The root path we give to network bundles for all resources they're interested in. */
protected String _networkRootPath; protected String _networkRootPath;
/** Whether or not to unpack our resource bundles fully. */ /** Whether or not to unpack our resource bundles. */
protected boolean _unpackFully; protected boolean _unpack;
/** Whether or not to unpack individual jar entries. */
protected boolean _unpackEntries = true;
/** Our default resource set. */ /** Our default resource set. */
protected ResourceBundle[] _default = new ResourceBundle[0]; protected ResourceBundle[] _default = new ResourceBundle[0];