Java 文件和 ByteArray 或 InputStream

发布于 2024-08-30 06:40:24 字数 561 浏览 7 评论 0原文

我想使用 jFugue 在小程序中播放一些 MIDI 音乐。有一个用于 MIDI 模式的类 - Pattern - 加载模式的唯一方法是从文件中加载。现在,我不知道小程序如何加载文件以及不加载文件,但我正在使用一个框架(PulpCore),它使加载资源成为一项简单的任务。如果我需要从 ZIP 目录中获取资产,我可以使用提供 get()getAsStream() 方法的 Assets 类。 get()ByteArray 形式返回给定资源,另一个以 InputStream 形式返回。

我需要 jFugue 从 ByteArrayInputStream 加载模式。 在伪代码中,我想这样做:

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

但是没有需要 ByteArray 的 File 构造函数。请提出建议?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独行侠 2024-09-06 06:40:26

如果我没记错的话,模式文件包含纯文本。使用 getAsStream() 加载文件,然后使用

BufferedReader br = new BufferedReader(new InputStreamReader(yourStream));
//...
String pattern = convertToString(br); // you should implement convertToString yourself. It's easy. Read the java.io APIs.

其中 yourStream 是 getAsStream() 返回的 InputStream 将其转换为字符串。然后使用 add(String...patterns) 方法加载模式:

add(pattern);

If I am not wrong, the Pattern files contain plain text. Load the file using getAsStream(), then convert it into a string using

BufferedReader br = new BufferedReader(new InputStreamReader(yourStream));
//...
String pattern = convertToString(br); // you should implement convertToString yourself. It's easy. Read the java.io APIs.

Where yourStream is the InputStream returned by getAsStream(). Then use the add(String... patterns) method to load the pattern:

add(pattern);
素食主义者 2024-09-06 06:40:26

您可以使用以下代码(取自 Pattern.loadPattern() 方法的实现):

    InputStream is = ...; // Get a stream from the Asset object

    // Prepare a pattern object
    Pattern pattern = new Pattern();

    // Now start reaing from the stream
    StringBuffer buffy = new StringBuffer();
    BufferedReader bread = new BufferedReader(new InputStreamReader(is));
    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);
                }
            }
        }
    }
    bread.close();
    pattern.setMusicString(buffy.toString());

    // Your pattern is now ready

You can use this code (taken from the implementation of the Pattern.loadPattern() method):

    InputStream is = ...; // Get a stream from the Asset object

    // Prepare a pattern object
    Pattern pattern = new Pattern();

    // Now start reaing from the stream
    StringBuffer buffy = new StringBuffer();
    BufferedReader bread = new BufferedReader(new InputStreamReader(is));
    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);
                }
            }
        }
    }
    bread.close();
    pattern.setMusicString(buffy.toString());

    // Your pattern is now ready
静若繁花 2024-09-06 06:40:26

您可以读取字节数组并将其转换为字符串。

问题将出在输入流上。有一个 StringBufferInputStream,但它已被弃用,取而代之的是 StringReader。

byte [] b = Assets.get();
InputStream is = new StringBufferInputStream(new String(b));
Pattern.load(is);

You could read the byte array and turn it into a String.

The problem will be the InputStream. There's a StringBufferInputStream, but it's deprecated in favor of StringReader.

byte [] b = Assets.get();
InputStream is = new StringBufferInputStream(new String(b));
Pattern.load(is);
一片旧的回忆 2024-09-06 06:40:26

您不想使用File,您想使用java.io.ByteArrayInputStream

You don't want to use File, you want to use java.io.ByteArrayInputStream

电影里的梦 2024-09-06 06:40:25

确实,jFugue 不允许加载除文件之外的任何内容,这是一种耻辱,因为没有什么可以阻止使用任何其他类型的流:

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;
}

更新(对于 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文