Path: blob/master/src/java.desktop/share/classes/com/sun/media/sound/JARSoundbankReader.java
41161 views
/*1* Copyright (c) 2007, 2015, 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*/2425package com.sun.media.sound;2627import java.io.BufferedReader;28import java.io.File;29import java.io.IOException;30import java.io.InputStream;31import java.io.InputStreamReader;32import java.net.URL;33import java.net.URLClassLoader;34import java.util.ArrayList;35import java.util.Objects;3637import javax.sound.midi.InvalidMidiDataException;38import javax.sound.midi.Soundbank;39import javax.sound.midi.spi.SoundbankReader;4041import sun.reflect.misc.ReflectUtil;4243/**44* JarSoundbankReader is used to read soundbank object from jar files.45*46* @author Karl Helgason47*/48public final class JARSoundbankReader extends SoundbankReader {4950private static boolean isZIP(URL url) {51boolean ok = false;52try {53InputStream stream = url.openStream();54try {55byte[] buff = new byte[4];56ok = stream.read(buff) == 4;57if (ok) {58ok = (buff[0] == 0x5059&& buff[1] == 0x4b60&& buff[2] == 0x0361&& buff[3] == 0x04);62}63} finally {64stream.close();65}66} catch (IOException e) {67}68return ok;69}7071@Override72@SuppressWarnings("deprecation")73public Soundbank getSoundbank(URL url)74throws InvalidMidiDataException, IOException {75if (!isZIP(url))76return null;77ArrayList<Soundbank> soundbanks = new ArrayList<>();78URLClassLoader ucl = URLClassLoader.newInstance(new URL[]{url});79InputStream stream = ucl.getResourceAsStream(80"META-INF/services/javax.sound.midi.Soundbank");81if (stream == null)82return null;83try84{85BufferedReader r = new BufferedReader(new InputStreamReader(stream));86String line = r.readLine();87while (line != null) {88if (!line.startsWith("#")) {89try {90Class<?> c = Class.forName(line.trim(), false, ucl);91if (Soundbank.class.isAssignableFrom(c)) {92ReflectUtil.checkPackageAccess(c);93Object o = c.newInstance();94soundbanks.add((Soundbank) o);95}96} catch (ReflectiveOperationException ignored) {97}98}99line = r.readLine();100}101}102finally103{104stream.close();105}106if (soundbanks.size() == 0)107return null;108if (soundbanks.size() == 1)109return soundbanks.get(0);110SimpleSoundbank sbk = new SimpleSoundbank();111for (Soundbank soundbank : soundbanks)112sbk.addAllInstruments(soundbank);113return sbk;114}115116@Override117public Soundbank getSoundbank(InputStream stream)118throws InvalidMidiDataException, IOException {119Objects.requireNonNull(stream);120return null;121}122123@Override124public Soundbank getSoundbank(File file)125throws InvalidMidiDataException, IOException {126return getSoundbank(file.toURI().toURL());127}128}129130131