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