Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/media/sound/JARSoundbankReader.java
38924 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.media.sound;2526import java.io.BufferedReader;27import java.io.File;28import java.io.IOException;29import java.io.InputStream;30import java.io.InputStreamReader;31import java.net.URL;32import java.net.URLClassLoader;33import java.util.ArrayList;34import javax.sound.midi.InvalidMidiDataException;35import javax.sound.midi.Soundbank;36import javax.sound.midi.spi.SoundbankReader;3738import sun.reflect.misc.ReflectUtil;3940/**41* JarSoundbankReader is used to read soundbank object from jar files.42*43* @author Karl Helgason44*/45public final class JARSoundbankReader extends SoundbankReader {4647private static boolean isZIP(URL url) {48boolean ok = false;49try {50InputStream stream = url.openStream();51try {52byte[] buff = new byte[4];53ok = stream.read(buff) == 4;54if (ok) {55ok = (buff[0] == 0x5056&& buff[1] == 0x4b57&& buff[2] == 0x0358&& buff[3] == 0x04);59}60} finally {61stream.close();62}63} catch (IOException e) {64}65return ok;66}6768public Soundbank getSoundbank(URL url)69throws InvalidMidiDataException, IOException {70if (!isZIP(url))71return null;72ArrayList<Soundbank> soundbanks = new ArrayList<Soundbank>();73URLClassLoader ucl = URLClassLoader.newInstance(new URL[]{url});74InputStream stream = ucl.getResourceAsStream(75"META-INF/services/javax.sound.midi.Soundbank");76if (stream == null)77return null;78try79{80BufferedReader r = new BufferedReader(new InputStreamReader(stream));81String line = r.readLine();82while (line != null) {83if (!line.startsWith("#")) {84try {85Class<?> c = Class.forName(line.trim(), false, ucl);86if (Soundbank.class.isAssignableFrom(c)) {87Object o = ReflectUtil.newInstance(c);88soundbanks.add((Soundbank) o);89}90} catch (ClassNotFoundException ignored) {91} catch (InstantiationException ignored) {92} catch (IllegalAccessException ignored) {93}94}95line = r.readLine();96}97}98finally99{100stream.close();101}102if (soundbanks.size() == 0)103return null;104if (soundbanks.size() == 1)105return soundbanks.get(0);106SimpleSoundbank sbk = new SimpleSoundbank();107for (Soundbank soundbank : soundbanks)108sbk.addAllInstruments(soundbank);109return sbk;110}111112public Soundbank getSoundbank(InputStream stream)113throws InvalidMidiDataException, IOException {114return null;115}116117public Soundbank getSoundbank(File file)118throws InvalidMidiDataException, IOException {119return getSoundbank(file.toURI().toURL());120}121}122123124