Not jPCT Related

Started by KittenKoder, November 21, 2012, 06:52:25 PM

Previous topic - Next topic

KittenKoder

But, I'm hoping someone here knows it. I completely forgot as I have not used the technique in a long time, but there's a specific trick to loading a file that could be from either an applet or application and within a jar or not, in other words, loading the file with just the base dir/url and the loader not caring which it is.

I use to have this documented but since I haven't used it in a long time I lost it.  If I remember correctly using new File(filename) doesn't work in all situations.
When life throws you lemons, make lemon juice, then drop life into a pile of razors and pour the lemon juice over it.

KittenKoder

This is based on what I can remember, but it just doesn't look right to me. I could swear I saw a simpler way.


    static public URL loadFile(String filename) {
        System.out.println(KittIO.baseurl + filename);
        URL returl;
        try {
            if(KittIO.baseurl != null)
                returl = ClassLoader.getSystemClassLoader().getResource(KittIO.baseurl + filename);
            else
                returl = ClassLoader.getSystemClassLoader().getResource(filename);
            if(returl == null) {
                File file = new File(filename);
                if(file.isFile())
                    returl = file.toURI().toURL();
            }
        } catch(Exception e) {
            returl = null;
        }
        return returl;
    }

When life throws you lemons, make lemon juice, then drop life into a pile of razors and pour the lemon juice over it.

KittenKoder

Actually, this should work better even. I am responding to myself, but someone let me know if you see a problem with this working for all cases. I need to figure out how to include jar file loading as well, without implementing too much junk.


    static public URL loadFile(String filename) {
        URL returl = null;
        try {
            if(KittIO.baseurl != null)
                returl = new URL(KittIO.baseurl + filename);
            else
                returl = new URL(filename);
        } catch(Exception e) {
            try {
                File file = new File(filename);
                if(file.isFile())
                    returl = file.toURI().toURL();
            } catch(Exception ex) {
                ex.printStackTrace();
                returl = null;
            }
        }
        return returl;
    }
When life throws you lemons, make lemon juice, then drop life into a pile of razors and pour the lemon juice over it.

EgonOlsen

I'm usually doing it this way, i.e trying to load it from the jar and it that fails, i'm trying to access it as a file. I somehow dislike the URL approach and prefer InputStreams.


package robombs.game.util;

import java.io.*;

/**
* A simple wrapper for InputStreams from the class path.
*/
public class SimpleStream {
    private InputStream stream;

    /**
     * Create a new stream based on a file name. The file has to be present in the class path.
     * @param file the file name.
     */
    public SimpleStream(String file) {
        stream = this.getClass().getClassLoader().getResourceAsStream(file);
       
        if (stream==null) {
        File fi=new File(file);
        try {
        stream=new FileInputStream(fi);
        } catch(Exception e) {
        throw new RuntimeException(e);
        }
        }
       
    }

    /**
     * Returns the actual stream.
     * @return InputStream the stream
     */
    public InputStream getStream() {
        return stream;
    }

    /**
     * Closes the stream.
     * @throws IOException
     */
    public void close() throws IOException {
        if (stream != null) {
            stream.close();
        }
    }
}


KittenKoder

Quote from: EgonOlsen on November 21, 2012, 08:23:54 PM
I'm usually doing it this way, i.e trying to load it from the jar and it that fails, i'm trying to access it as a file. I somehow dislike the URL approach and prefer InputStreams.

The more I look into it, your method may work best. It's been so long since I've tried to develop something for other programmers that I had forgotten all the tricks to making things work in multiple ways. I am wondering if new File would break in applets, if not, since the resources for the system I'm writing all use the built in IO methods, then I'm just going to leave it up to the coder to handle. I have a jar resource formatter to create a jar URL and a doc-base extractor for working from within jars that works with applets as well.
When life throws you lemons, make lemon juice, then drop life into a pile of razors and pour the lemon juice over it.

KittenKoder

Alright, quick technical question about the loaders for jPCT, do they use input streams from the filenames when you supply them? Such as Texture(java.lang.String filename), so if supplied a URL, fully qualified path including the proper jar path formatting, would it it error out?

I have no way to test jarred applets right now, the OS I use, FireFox, and Java seem to be having issues. I should switch browsers but I really like FireFox.
When life throws you lemons, make lemon juice, then drop life into a pile of razors and pour the lemon juice over it.

EgonOlsen

Quote from: KittenKoder on November 22, 2012, 03:18:34 AM
Alright, quick technical question about the loaders for jPCT, do they use input streams from the filenames when you supply them? Such as Texture(java.lang.String filename), so if supplied a URL, fully qualified path including the proper jar path formatting, would it it error out?
Yes, it would fail to load it that way.