//
// $Id: ResourceManager.java,v 1.1 2001/07/18 21:16:12 mdb Exp $

package com.threerings.resource;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * The resource manager is responsible for maintaining a repository of
 * resources that are synchronized with a remote source. This is
 * accomplished in the form of a set of jar files that contain resources
 * and that are updated from a remote resource repository via HTTP.
 */
public class ResourceManager
{
    /**
     * Temporary means by which to construct a resource manager that loads
     * resources from the specified source.
     */
    public ResourceManager (URL source)
    {
        _source = source;
        _rootPath = _source.getPath();
        // make root path end with a slash
        if (!_rootPath.endsWith("/")) {
            _rootPath = _rootPath + "/";
        }
    }

    /**
     * Fetches a resource from the local repository.
     *
     * @param path the path to the resource
     * (ie. "config/miso.properties"). This should not begin with a slash.
     *
     * @exception IOException thrown if a problem occurs locating or
     * reading the resource.
     */
    public InputStream getResource (String path)
        throws IOException
    {
        try {
            String rpath = _rootPath + path;
            URL rurl = new URL(_source, rpath);
            return rurl.openStream();

        } catch (MalformedURLException mue) {
            Log.warning("Unable to construct path to resource " +
                        "[path=" + path + "].");
            return null;
        }
    }

    /**
     * Fetches the requested resource and loads its contents into a byte
     * array, which is returned. Note: this is hugely inefficient because
     * the data is copied twice while reading and then once entirely again
     * when a brand new byte array is returned for you, the caller (thanks
     * to the inflexibility of <code>ByteArrayOutputStream</code>). Anyone
     * reading a lot of resources should obtain an
     * <code>InputStream</code> and read the data directly into where it
     * needs to go.
     *
     * @exception IOException thrown if a problem occurs locating or
     * reading the resource.
     *
     * @see #getResource
     */
    public byte[] getResourceAsBytes (String path)
        throws IOException
    {
        InputStream in = getResource(path);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[512];

        // i loathe to while(1), but we need a non-trivial loop condition
        while (true) {
            int bytes = in.read(buffer, 0, buffer.length);
            if (bytes == 0) {
                throw new IOException("Read zero bytes!?");
            } else if (bytes < 0) {
                break;
            }
            out.write(buffer, 0, bytes);
        }

        return out.toByteArray();
    }

    public static void main (String[] args)
    {
        try {
            String root = System.getProperty("root", "");
            URL url = new URL("file:" + root + "/rsrc");
            ResourceManager rmgr = new ResourceManager(url);
            byte[] data = rmgr.getResourceAsBytes("config/miso.properties");
            System.out.println(new String(data));

        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }

    protected URL _source;
    protected String _rootPath;
}

@@ -33,29 +33,29 @@
 v 1.
-1
+2
  2001/0
-7/18 21:16:12
+8/15 02:12:46
  mdb
@@ -613,24 +613,163 @@
 fied source.
+ The resource root is prepended%0A     * to any path that is requested and that fully qualified path is%0A     * searched for in the classpath.
 %0A     */%0A   
@@ -797,18 +797,27 @@
 er (
-URL 
+String re
 source
+Root
 )%0A  
@@ -832,25 +832,38 @@
     
-_source = source;
+// keep track of our root path
 %0A   
@@ -883,26 +883,22 @@
 h = 
-_
+re
 source
-.getPath();
+Root;%0A
 %0A   
@@ -1019,32 +1019,126 @@
 + %22/%22;%0A        %7D
+%0A%0A        // use the classloader that loaded us%0A        _loader = getClass().getClassLoader();
 %0A    %7D%0A%0A    /**%0A
@@ -1511,34 +1511,16 @@
 n%0A    %7B%0A
-        try %7B%0A    
         
@@ -1564,95 +1564,69 @@
     
-    URL rurl = new URL(_source, rpath);%0A            return rurl.open
+InputStream in = _loader.getResourceAs
 Stream(
+rpath
 );%0A
-%0A
         
 %7D ca
@@ -1625,137 +1625,81 @@
     
-%7D catch (MalformedURLException mue) %7B%0A            Log.warning(%22Unable to construct path to resource %22 +%0A                        %22
+if (in == null) %7B%0A            String errmsg = %22Unable to locate resource 
 %5Bpat
@@ -1704,16 +1704,17 @@
 ath=%22 + 
+r
 path + %22
@@ -1718,11 +1718,9 @@
 + %22%5D
-.%22)
+%22
 ;%0A  
@@ -1733,30 +1733,77 @@
     
-return null;%0A        %7D
+throw new FileNotFoundException(errmsg);%0A        %7D%0A        return in;
 %0A   
@@ -3188,123 +3188,8 @@
 y %7B%0A
-            String root = System.getProperty(%22root%22, %22%22);%0A            URL url = new URL(%22file:%22 + root + %22/rsrc%22);%0A
     
@@ -3243,11 +3243,14 @@
 ger(
-url
+%22rsrc%22
 );%0A 
@@ -3298,16 +3298,33 @@
 AsBytes(
+%0A                
 %22config/
@@ -3319,32 +3319,37 @@
     %22config/miso
+/miso
 .properties%22);%0A 
@@ -3508,19 +3508,27 @@
 ted 
-URL _source
+ClassLoader _loader
 ;%0A  

Load resources from the classpath rather than from a base URL.