1 /* 2 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Sun designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Sun in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 22 * CA 95054 USA or visit www.sun.com if you need additional information or 23 * have any questions. 24 */ 25 package com.sun.media.sound; 26 27 import java.io.BufferedReader; 28 import java.io.File; 29 import java.io.IOException; 30 import java.io.InputStream; 31 import java.io.InputStreamReader; 32 import java.net.URL; 33 import java.net.URLClassLoader; 34 import java.util.ArrayList; 35 import javax.sound.midi.InvalidMidiDataException; 36 import javax.sound.midi.Soundbank; 37 import javax.sound.midi.spi.SoundbankReader; 38 39 /** 40 * JarSoundbankReader is used to read sounbank object from jar files. 41 * 42 * @author Karl Helgason 43 */ 44 public class JARSoundbankReader extends SoundbankReader { 45 isZIP(URL url)46 public boolean isZIP(URL url) { 47 boolean ok = false; 48 try { 49 InputStream stream = url.openStream(); 50 try { 51 byte[] buff = new byte[4]; 52 ok = stream.read(buff) == 4; 53 if (ok) { 54 ok = (buff[0] == 0x50 55 && buff[1] == 0x4b 56 && buff[2] == 0x03 57 && buff[3] == 0x04); 58 } 59 } finally { 60 stream.close(); 61 } 62 } catch (IOException e) { 63 } 64 return ok; 65 } 66 getSoundbank(URL url)67 public Soundbank getSoundbank(URL url) 68 throws InvalidMidiDataException, IOException { 69 if (!isZIP(url)) 70 return null; 71 ArrayList<Soundbank> soundbanks = new ArrayList<Soundbank>(); 72 URLClassLoader ucl = URLClassLoader.newInstance(new URL[]{url}); 73 InputStream stream = ucl.getResourceAsStream( 74 "META-INF/services/javax.sound.midi.Soundbank"); 75 if (stream == null) 76 return null; 77 try 78 { 79 BufferedReader r = new BufferedReader(new InputStreamReader(stream)); 80 String line = r.readLine(); 81 while (line != null) { 82 if (!line.startsWith("#")) { 83 try { 84 Class c = Class.forName(line.trim(), true, ucl); 85 Object o = c.newInstance(); 86 if (o instanceof Soundbank) { 87 soundbanks.add((Soundbank) o); 88 } 89 } catch (ClassNotFoundException e) { 90 } catch (InstantiationException e) { 91 } catch (IllegalAccessException e) { 92 } 93 } 94 line = r.readLine(); 95 } 96 } 97 finally 98 { 99 stream.close(); 100 } 101 if (soundbanks.size() == 0) 102 return null; 103 if (soundbanks.size() == 1) 104 return soundbanks.get(0); 105 SimpleSoundbank sbk = new SimpleSoundbank(); 106 for (Soundbank soundbank : soundbanks) 107 sbk.addAllInstruments(soundbank); 108 return sbk; 109 } 110 getSoundbank(InputStream stream)111 public Soundbank getSoundbank(InputStream stream) 112 throws InvalidMidiDataException, IOException { 113 return null; 114 } 115 getSoundbank(File file)116 public Soundbank getSoundbank(File file) 117 throws InvalidMidiDataException, IOException { 118 return getSoundbank(file.toURI().toURL()); 119 } 120 } 121