Search in sources :

Example 1 with BufferedInputStream

use of com.xenoage.utils.io.BufferedInputStream in project Zong by Xenoage.

the class CompressedFileInput method loadScore.

/**
 * Loads and returns the {@link com.xenoage.zong.core.Score} at the given path.
 */
public com.xenoage.zong.core.Score loadScore(String path) throws InvalidFormatException, IOException {
    BufferedInputStream bis = new BufferedInputStream(zip.openFile(path));
    // XML or compressed?
    bis.mark();
    FileType fileType = FileTypeReader.getFileType(bis);
    bis.reset();
    bis.unmark();
    if (fileType == null)
        throw new InvalidFormatException("Score has invalid format: " + path);
    com.xenoage.zong.core.Score ret = null;
    switch(fileType) {
        case Compressed:
            ret = loadCompressedScore(path);
            break;
        case XMLScorePartwise:
            ret = new MusicXmlScoreFileInput().read(bis, path);
            break;
        case XMLScoreTimewise:
            throw new IllegalStateException("score-timewise is currently not implemented");
        default:
            throw new InvalidFormatException("Score has invalid format: " + path);
    }
    bis.close();
    return ret;
}
Also used : Score(com.xenoage.zong.io.musicxml.opus.Score) BufferedInputStream(com.xenoage.utils.io.BufferedInputStream) FileType(com.xenoage.zong.io.musicxml.FileType) InvalidFormatException(com.xenoage.utils.exceptions.InvalidFormatException)

Example 2 with BufferedInputStream

use of com.xenoage.utils.io.BufferedInputStream in project Zong by Xenoage.

the class FileTypeReader method getFileType.

@MaybeNull
public static FileType getFileType(InputStream inputStream) throws IOException {
    // create buffered stream for reuse
    BufferedInputStream bis = new BufferedInputStream(inputStream);
    bis.mark();
    // read first two characters. if "PK", we have a compressed MusicXML file.
    int[] bytes = new int[] { bis.read(), bis.read() };
    if (// P, K
    bytes[0] == 80 && bytes[1] == 75) {
        return FileType.Compressed;
    }
    bis.reset();
    bis.unmark();
    // otherwise, try to parse as XML up to the root element (using StAX)
    try {
        XmlReader reader = platformUtils().createXmlReader(bis);
        if (reader.openNextChildElement()) {
            String n = reader.getElementName();
            switch(n) {
                case "score-partwise":
                    return FileType.XMLScorePartwise;
                case "score-timewise":
                    return FileType.XMLScoreTimewise;
                case "opus":
                    return FileType.XMLOpus;
            }
            reader.closeElement();
        }
    } catch (XmlException ex) {
        // unknown (no XML)
        return null;
    }
    // unknown
    return null;
}
Also used : BufferedInputStream(com.xenoage.utils.io.BufferedInputStream) XmlException(com.xenoage.utils.xml.XmlException) XmlReader(com.xenoage.utils.xml.XmlReader) MaybeNull(com.xenoage.utils.annotations.MaybeNull)

Example 3 with BufferedInputStream

use of com.xenoage.utils.io.BufferedInputStream in project Zong by Xenoage.

the class MusicXmlFileReader method produce.

@Override
public void produce(final AsyncResult<List<Score>> callback) {
    final List<Score> ret = alist();
    // open stream
    BufferedInputStream bis = new BufferedInputStream(in);
    try {
        bis.mark();
        // file type
        FileType fileType = FileTypeReader.getFileType(bis);
        bis.reset();
        bis.unmark();
        // open file
        if (fileType == FileType.XMLScorePartwise) {
            Score score = new MusicXmlScoreFileInput().read(bis, path);
            ret.add(score);
            callback.onSuccess(ret);
        } else if (fileType == FileType.XMLOpus) {
            // opus
            if (path == null) {
                // no path is given. we can not read the linked files.
                callback.onSuccess(ret);
            } else {
                // read files
                final String directory = FileUtils.getDirectoryName(path);
                OpusFileInput opusInput = new OpusFileInput();
                Opus opus = opusInput.readOpusFile(bis);
                new OpusLinkResolver(opus, null, directory).produce(new AsyncResult<Opus>() {

                    @Override
                    public void onSuccess(Opus opus) {
                        try {
                            List<String> filePaths = scoreFileFilter.filter(opus.getScoreFilenames());
                            processNextScore(directory, filePaths, scoreFileFilter, ret, callback);
                        } catch (IOException ex) {
                            callback.onFailure(ex);
                        }
                    }

                    @Override
                    public void onFailure(Exception ex) {
                        callback.onFailure(ex);
                    }
                });
            }
        } else if (fileType == FileType.Compressed) {
            CompressedFileInput zip = new CompressedFileInput(bis);
            List<String> filePaths = scoreFileFilter.filter(zip.getScoreFilenames());
            for (String filePath : filePaths) {
                Score score = zip.loadScore(filePath);
                ret.add(score);
            }
            zip.close();
            callback.onSuccess(ret);
        } else {
            callback.onFailure(new IOException("Unknown file type"));
        }
    } catch (IOException ex) {
        // try to close input stream
        bis.close();
        // return failure
        callback.onFailure(ex);
    }
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) Score(com.xenoage.zong.core.Score) BufferedInputStream(com.xenoage.utils.io.BufferedInputStream) FileType(com.xenoage.zong.io.musicxml.FileType) Opus(com.xenoage.zong.io.musicxml.opus.Opus) AsyncResult(com.xenoage.utils.async.AsyncResult)

Aggregations

BufferedInputStream (com.xenoage.utils.io.BufferedInputStream)3 FileType (com.xenoage.zong.io.musicxml.FileType)2 MaybeNull (com.xenoage.utils.annotations.MaybeNull)1 AsyncResult (com.xenoage.utils.async.AsyncResult)1 InvalidFormatException (com.xenoage.utils.exceptions.InvalidFormatException)1 XmlException (com.xenoage.utils.xml.XmlException)1 XmlReader (com.xenoage.utils.xml.XmlReader)1 Score (com.xenoage.zong.core.Score)1 Opus (com.xenoage.zong.io.musicxml.opus.Opus)1 Score (com.xenoage.zong.io.musicxml.opus.Score)1 IOException (java.io.IOException)1