javafilearraysjfuguepulpcore

Java File and ByteArray or InputStream


I want to use jFugue to play some MIDI music in an applet. There's a class for the MIDI pattern - Pattern - and the only method to load the pattern is from a File. Now, I don't know how applets load files and what not, but I am using a framework (PulpCore) that makes loading assets a simple task. If I need to grab an asset from a ZIP catalogue, I can use the Assets class which provides get() and getAsStream() methods. get() returns the given asset as a ByteArray, the other as an InputStream.

I need jFugue to load the pattern from either ByteArray or InputStream. In pseudo-code, I would like to do this:

Pattern.load(new File(Assets.get("mymidifile.midi")));

However there is no File constructor that would take a ByteArray. Suggestions, please?


Solution

  • It's true that jFugue doesn't allow to load anything but a file, which is a shame because nothing prevents from using any other kind of stream:

    public static final String TITLE = "Title";
    
    public static Pattern loadPattern(File file) throws IOException {
        InputStream in = new FileInputStream(file);
        try {
            return loadPattern(in);
        } finally {
            in.close();
        }
    }
    
    public static Pattern loadPattern(URL url) throws IOException {
        InputStream in = url.openStream();
        try {
            return loadPattern(in);
        } finally {
            in.close();
        }
    }
    
    public static Pattern loadPattern(InputStream in) throws IOException {
        return loadPattern(new InputStreamReader(in, "UTF-8")); // or ISO-8859-1 ?
    }
    
    public static Pattern loadPattern(Reader reader) throws IOException {
        if (reader instanceof BufferedReader) {
            return loadPattern(reader);
        } else {
            return loadPattern(new BufferedReader(reader));
        }
    }
    
    public static Pattern loadPattern(BufferedReader bread) throws IOException {
        StringBuffer buffy = new StringBuffer();
    
        Pattern pattern = new Pattern();
        while (bread.ready()) {
            String s = bread.readLine();
            if ((s != null) && (s.length() > 1)) {
                if (s.charAt(0) != '#') {
                    buffy.append(" ");
                    buffy.append(s);
                } else {
                    String key = s.substring(1, s.indexOf(':')).trim();
                    String value = s.substring(s.indexOf(':')+1, s.length()).trim();
                    if (key.equalsIgnoreCase(TITLE)) {
                        pattern.setTitle(value);
                    } else {
                        pattern.setProperty(key, value);
                    }
                }
            }
        }
        return pattern;
    }
    

    UPDATE (for loadMidi)

    public static Pattern loadMidi(InputStream in) throws IOException, InvalidMidiDataException
    {
        MidiParser parser = new MidiParser();
        MusicStringRenderer renderer = new MusicStringRenderer();
        parser.addParserListener(renderer);
        parser.parse(MidiSystem.getSequence(in));
        Pattern pattern = new Pattern(renderer.getPattern().getMusicString());
        return pattern;
    }
    
    public static Pattern loadMidi(URL url) throws IOException, InvalidMidiDataException
    {
        MidiParser parser = new MidiParser();
        MusicStringRenderer renderer = new MusicStringRenderer();
        parser.addParserListener(renderer);
        parser.parse(MidiSystem.getSequence(url));
        Pattern pattern = new Pattern(renderer.getPattern().getMusicString());
        return pattern;
    }