829N/A/*
6321N/A * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
829N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
829N/A *
829N/A * This code is free software; you can redistribute it and/or modify it
829N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
829N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
829N/A *
829N/A * This code is distributed in the hope that it will be useful, but WITHOUT
829N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
829N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
829N/A * version 2 for more details (a copy is included in the LICENSE file that
829N/A * accompanied this code).
829N/A *
829N/A * You should have received a copy of the GNU General Public License version
829N/A * 2 along with this work; if not, write to the Free Software Foundation,
829N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
829N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
829N/A */
829N/Apackage com.sun.media.sound;
829N/A
829N/Aimport java.io.BufferedReader;
829N/Aimport java.io.File;
829N/Aimport java.io.IOException;
829N/Aimport java.io.InputStream;
829N/Aimport java.io.InputStreamReader;
829N/Aimport java.net.URL;
829N/Aimport java.net.URLClassLoader;
829N/Aimport java.util.ArrayList;
829N/Aimport javax.sound.midi.InvalidMidiDataException;
829N/Aimport javax.sound.midi.Soundbank;
829N/Aimport javax.sound.midi.spi.SoundbankReader;
829N/A
6321N/Aimport sun.reflect.misc.ReflectUtil;
6321N/A
829N/A/**
6321N/A * JarSoundbankReader is used to read soundbank object from jar files.
829N/A *
829N/A * @author Karl Helgason
829N/A */
6321N/Apublic final class JARSoundbankReader extends SoundbankReader {
829N/A
6321N/A private static boolean isZIP(URL url) {
829N/A boolean ok = false;
829N/A try {
829N/A InputStream stream = url.openStream();
829N/A try {
829N/A byte[] buff = new byte[4];
829N/A ok = stream.read(buff) == 4;
829N/A if (ok) {
829N/A ok = (buff[0] == 0x50
829N/A && buff[1] == 0x4b
829N/A && buff[2] == 0x03
829N/A && buff[3] == 0x04);
829N/A }
829N/A } finally {
829N/A stream.close();
829N/A }
829N/A } catch (IOException e) {
829N/A }
829N/A return ok;
829N/A }
829N/A
829N/A public Soundbank getSoundbank(URL url)
829N/A throws InvalidMidiDataException, IOException {
829N/A if (!isZIP(url))
829N/A return null;
829N/A ArrayList<Soundbank> soundbanks = new ArrayList<Soundbank>();
829N/A URLClassLoader ucl = URLClassLoader.newInstance(new URL[]{url});
829N/A InputStream stream = ucl.getResourceAsStream(
829N/A "META-INF/services/javax.sound.midi.Soundbank");
829N/A if (stream == null)
829N/A return null;
829N/A try
829N/A {
829N/A BufferedReader r = new BufferedReader(new InputStreamReader(stream));
829N/A String line = r.readLine();
829N/A while (line != null) {
829N/A if (!line.startsWith("#")) {
829N/A try {
6321N/A Class<?> c = Class.forName(line.trim(), false, ucl);
6321N/A if (Soundbank.class.isAssignableFrom(c)) {
6321N/A Object o = ReflectUtil.newInstance(c);
829N/A soundbanks.add((Soundbank) o);
829N/A }
6321N/A } catch (ClassNotFoundException ignored) {
6321N/A } catch (InstantiationException ignored) {
6321N/A } catch (IllegalAccessException ignored) {
829N/A }
829N/A }
829N/A line = r.readLine();
829N/A }
829N/A }
829N/A finally
829N/A {
829N/A stream.close();
829N/A }
829N/A if (soundbanks.size() == 0)
829N/A return null;
829N/A if (soundbanks.size() == 1)
829N/A return soundbanks.get(0);
829N/A SimpleSoundbank sbk = new SimpleSoundbank();
829N/A for (Soundbank soundbank : soundbanks)
829N/A sbk.addAllInstruments(soundbank);
829N/A return sbk;
829N/A }
829N/A
829N/A public Soundbank getSoundbank(InputStream stream)
829N/A throws InvalidMidiDataException, IOException {
829N/A return null;
829N/A }
829N/A
829N/A public Soundbank getSoundbank(File file)
829N/A throws InvalidMidiDataException, IOException {
829N/A return getSoundbank(file.toURI().toURL());
829N/A }
829N/A}