Class AssetURLContext

    • Constructor Detail

      • AssetURLContext

        public AssetURLContext()
    • Method Detail

      • createURL

        public static URL createURL​(String path,
                                    ClassLoader cl)
                             throws MalformedURLException
        Create an asset URL, suitable even w/o the registered asset URLStreamHandler.

        This is equivalent with:

           return new URL(null, path.startsWith("asset:") ? path : "asset:" + path, new AssetURLStreamHandler(cl));
         

        Parameters:
        path - resource path, with or w/o asset: prefix
        cl - the ClassLoader used to resolve the location, see getClassLoader().
        Returns:
        Throws:
        MalformedURLException
      • createURL

        public static URL createURL​(String path)
                             throws MalformedURLException
        Create an asset URL, suitable only with the registered asset URLStreamHandler.

        This is equivalent with:

           return new URL(path.startsWith("asset:") ? path : "asset:" + path);
         

        Parameters:
        path - resource path, with or w/o asset: prefix
        Returns:
        Throws:
        MalformedURLException
      • registerHandler

        public static boolean registerHandler​(ClassLoader cl)
        Registers the generic URLStreamHandlerFactory via GenericURLStreamHandlerFactory.register() and if successful sets the asset handler for the given ClassLoader cl.
        Returns:
        true if successful, otherwise false
      • getClassLoader

        public abstract ClassLoader getClassLoader()
        Returns an asset aware ClassLoader.

        The ClassLoader is required to find the asset resource via it's URL findResource(String) implementation.

        It's URL findResource(String) implementation shall return either an asset URL asset:sub-protocol or just the sub-protocol URL.

        For example, on Android, we redirect all path request to assets/path.

      • resolve

        public URLConnection resolve​(String path)
                              throws IOException
        Resolving path to a URL sub protocol and return it's open URLConnection

        This implementation attempts to resolve path in the following order:

        1. as a valid URL: new URL(path), use sub-protocol if asset URL
        2. via ClassLoader: getClassLoader().getResource(path), use sub-protocol if asset URL
        3. as a File: new File(path).toURI().toURL()

        In case of using the ClassLoader (2) and if running on Android, the assets_folder is being prepended to path if missing.

        Specified by:
        resolve in interface PiggybackURLContext
        Throws:
        IOException