FileDocCategorySizeDatePackage
Support_Resources.javaAPI DocAndroid 1.5 API7100Wed May 06 22:41:06 BST 2009tests.support.resource

Support_Resources

public class Support_Resources extends Object

Fields Summary
public static final String
RESOURCE_PACKAGE
public static final String
RESOURCE_PACKAGE_NAME
Constructors Summary
Methods Summary
public static java.io.FilecopyFile(java.io.File root, java.lang.String folder, java.lang.String file)

        File f;
        if (folder != null) {
            f = new File(root.toString() + "/" + folder);
            if (!f.exists()) {
                f.mkdirs();
                f.deleteOnExit();
            }
        } else {
            f = root;
        }

        File dest = new File(f.toString() + "/" + file);

        InputStream in = Support_Resources.getStream(folder == null ? file
                : folder + "/" + file);
        try {
            copyLocalFileto(dest, in);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return dest;
    
public static voidcopyLocalFileto(java.io.File dest, java.io.InputStream in)

        if (!dest.exists()) {
            FileOutputStream out = new FileOutputStream(dest);
            int result;
            byte[] buf = new byte[4096];
            while ((result = in.read(buf)) != -1) {
                out.write(buf, 0, result);
            }
            in.close();
            out.close();
            dest.deleteOnExit();
        }
    
public static java.io.FilecreateTempFile(java.lang.String suffix)

        return File.createTempFile("hyts_", suffix, null);
    
public static java.io.FilecreateTempFolder()


        File folder = null;
        try {
            folder = File.createTempFile("hyts_resources", "", null);
            folder.delete();
            folder.mkdirs();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        folder.deleteOnExit();
        return folder;
    
public static java.lang.StringgetAbsoluteResourcePath(java.lang.String name)
Util method to get absolute path to resource file

param
name - name of resource file
return
- path to resource


        URL url = ClassLoader.getSystemClassLoader().getResource(name);
        if (url == null) {
            throw new RuntimeException("Failed to load resource: " + name);
        }

        try {
            return new File(url.toURI()).getAbsolutePath();
        } catch (URISyntaxException e) {
            throw new RuntimeException("Failed to load resource: " + name);
        }
    
public static java.io.FilegetExternalLocalFile(java.lang.String url)

        File resources = createTempFolder();
        InputStream in = new URL(url).openStream();
        File temp = new File(resources.toString() + "/local.tmp");
        copyLocalFileto(temp, in);
        return temp;
    
public static java.io.InputStreamgetResourceStream(java.lang.String name)
Util method to load resource files

param
name - name of resource file
return
- resource input stream

//ATTENTION:
//    Against class.getResourceStream(name) the name can start with a "/".
//    Against classLoader.getResourceStream NOT!
        
        InputStream is;
//        is = Support_Resources.class.getClassLoader().getResourceAsStream(name); This would work without leading "/"
        is = Support_Resources.class.getResourceAsStream(name);
//        is = ClassLoader.getSystemClassLoader().getResourceAsStream(name); This would work without leading "/"

        if (is == null) {
            name = "/tests/resources/" + name;
            is = Support_Resources.class.getResourceAsStream(name);
            if (is == null) {
                throw new RuntimeException("Failed to load resource: " + name);
            }
        }
        
        return is;
    
public static java.lang.StringgetResourceURL(java.lang.String resource)

        return "http://" + Support_Configuration.TestResources + resource;
    
public static java.io.InputStreamgetStream(java.lang.String name)


         
        return Support_Resources.class.getResourceAsStream(RESOURCE_PACKAGE
                + name);
    
public static java.lang.StringgetURL(java.lang.String name)

        String folder = null;
        String fileName = name;
        File resources = createTempFolder();
        int index = name.lastIndexOf("/");
        if (index != -1) {
            folder = name.substring(0, index);
            name = name.substring(index + 1);
        }
        copyFile(resources, folder, name);
        URL url = null;
        String resPath = resources.toString();
        if (resPath.charAt(0) == '/" || resPath.charAt(0) == '\\") {
            resPath = resPath.substring(1);
        }
        try {
            url = new URL("file:/" + resPath + "/" + fileName);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return url.toString();
    
public static intwriteResourceToStream(java.lang.String name, java.io.OutputStream out)
Util method to write resource files directly to an OutputStream.

param
name - name of resource file.
param
out - OutputStream to write to.
return
- number of bytes written to out.

        InputStream input = getResourceStream(name);
        byte[] buffer = new byte[512];
        int total = 0;
        int count;
        try {
            count = input.read(buffer);
            while (count != -1) {
                out.write(buffer, 0, count);
                total = total + count;
                count = input.read(buffer);
            }
            return total;
        } catch (IOException e) {
            throw new RuntimeException("Failed to write to passed stream.", e);
        }