import javax.sound.midi.spi.MidiFileReader; //导入方法依赖的package包/类
/**
* Obtains the MIDI file format of the data in the specified URL. The URL
* must point to valid MIDI file data for a file type recognized by the
* system.
*
* This operation can only succeed for files of a type which can be parsed
* by an installed file reader. It may fail with an
* {@code InvalidMidiDataException} even for valid files if no compatible
* file reader is installed. It will also fail with an
* {@code InvalidMidiDataException} if a compatible file reader is
* installed, but encounters errors while determining the file format.
*
* @param url the URL from which file format information should be
* extracted
* @return a {@code MidiFileFormat} object describing the MIDI file format
* @throws InvalidMidiDataException if the URL does not point to valid MIDI
* file data recognized by the system
* @throws IOException if an I/O exception occurs while accessing the URL
* @throws NullPointerException if {@code url} is {@code null}
* @see #getMidiFileFormat(InputStream)
* @see #getMidiFileFormat(File)
*/
public static MidiFileFormat getMidiFileFormat(final URL url)
throws InvalidMidiDataException, IOException {
Objects.requireNonNull(url);
List providers = getMidiFileReaders();
MidiFileFormat format = null;
for(int i = 0; i < providers.size(); i++) {
MidiFileReader reader = providers.get(i);
try {
format = reader.getMidiFileFormat( url ); // throws IOException
break;
} catch (InvalidMidiDataException e) {
continue;
}
}
if( format==null ) {
throw new InvalidMidiDataException("url is not a supported file type");
} else {
return format;
}
}