diff --git a/src/java/com/threerings/resource/KnownAvailabilityResourceBundle.java b/src/java/com/threerings/resource/KnownAvailabilityResourceBundle.java new file mode 100644 index 00000000..863aeea0 --- /dev/null +++ b/src/java/com/threerings/resource/KnownAvailabilityResourceBundle.java @@ -0,0 +1,27 @@ +package com.threerings.resource; + +import java.util.Set; + +/** + * Keeps a set of known resources and allows implementors to ask if a resource is in it before + * performing a potentially expensive failed lookup. + */ +public abstract class KnownAvailabilityResourceBundle extends ResourceBundle +{ + public KnownAvailabilityResourceBundle (Set availableResources) + { + _rsrcs = availableResources; + } + + /** + * Returns whether we believe the path to be available. If we have no list, we assume + * everything may be available. + */ + protected boolean isPossiblyAvailable (String path) + { + return _rsrcs == null || _rsrcs.contains(path); + } + + /** All the resources included in this bundle. */ + protected Set _rsrcs; +} diff --git a/src/java/com/threerings/resource/NetworkResourceBundle.java b/src/java/com/threerings/resource/NetworkResourceBundle.java index a0c7a37d..30ee2ce0 100644 --- a/src/java/com/threerings/resource/NetworkResourceBundle.java +++ b/src/java/com/threerings/resource/NetworkResourceBundle.java @@ -21,32 +21,25 @@ package com.threerings.resource; +import static com.threerings.resource.Log.log; + +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.security.AccessControlException; - -import java.util.HashSet; - -import java.io.IOException; -import java.io.InputStream; - -import java.awt.image.BufferedImage; - -import static com.threerings.resource.Log.log; +import java.util.Set; /** * Resource bundle that retrieves its contents via HTTP over the network from a root URL. */ -public class NetworkResourceBundle extends ResourceBundle +public class NetworkResourceBundle extends KnownAvailabilityResourceBundle { - public NetworkResourceBundle (String root, String path) - { - this(root, path, null); - } - - public NetworkResourceBundle (String root, String path, HashSet rsrcList) + public NetworkResourceBundle (String root, String path, Set rsrcList) { + super(rsrcList); if (!root.endsWith("/")) { root += "/"; } @@ -56,8 +49,6 @@ public class NetworkResourceBundle extends ResourceBundle log.warning("Created malformed URL for resource. [root=" + root + ", path=" + path); } _ident = path; - - _rsrcList = rsrcList; } @Override // documentation inherited @@ -71,7 +62,7 @@ public class NetworkResourceBundle extends ResourceBundle throws IOException { // If we can reject it before opening a connection, then save the network latency. - if (!inResourceList(path)) { + if (!isPossiblyAvailable(_ident + path)) { return null; } @@ -81,14 +72,11 @@ public class NetworkResourceBundle extends ResourceBundle protected static InputStream getResource(URL resourceUrl) { - URLConnection ucon = null; + URLConnection ucon; try { ucon = resourceUrl.openConnection(); } catch (IOException ioe) { log.warning("Unable to open connection [url=" + resourceUrl + ", ex=" + ioe + "]"); - } - - if (ucon == null) { return null; } @@ -115,15 +103,6 @@ public class NetworkResourceBundle extends ResourceBundle return ResourceManager.loadImage(in, false); } - /** - * Returns whether we believe the path to be available. If we have no list, we assume - * everything may be available. - */ - protected boolean inResourceList (String path) - { - return _rsrcList == null || _rsrcList.contains(_ident + path); - } - /** * Returns a string representation of this resource bundle. */ @@ -138,7 +117,4 @@ public class NetworkResourceBundle extends ResourceBundle /** Our root url to the resources in this bundle. */ protected URL _bundleURL; - - /** A list of all the resources included in this bundle. */ - protected HashSet _rsrcList; } diff --git a/src/java/com/threerings/resource/ResourceManager.java b/src/java/com/threerings/resource/ResourceManager.java index 1fb0aab1..eff4d872 100644 --- a/src/java/com/threerings/resource/ResourceManager.java +++ b/src/java/com/threerings/resource/ResourceManager.java @@ -32,6 +32,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.Set; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -852,7 +853,7 @@ public class ResourceManager * Creates an appropriate bundle for fetching resources from the network. */ protected ResourceBundle createNetworkResourceBundle (String root, String path, - HashSet rsrcList) + Set rsrcList) { return new NetworkResourceBundle(root, path, rsrcList); }