1 /*
   2  * Copyright (c) 2007, 2015, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.media.sound;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.File;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 import java.io.InputStreamReader;
  33 import java.net.URL;
  34 import java.net.URLClassLoader;
  35 import java.util.ArrayList;
  36 import java.util.Objects;
  37 
  38 import javax.sound.midi.InvalidMidiDataException;
  39 import javax.sound.midi.Soundbank;
  40 import javax.sound.midi.spi.SoundbankReader;
  41 
  42 import sun.reflect.misc.ReflectUtil;
  43 
  44 /**
  45  * JarSoundbankReader is used to read soundbank object from jar files.
  46  *
  47  * @author Karl Helgason
  48  */
  49 public final class JARSoundbankReader extends SoundbankReader {
  50 
  51     private static boolean isZIP(URL url) {
  52         boolean ok = false;
  53         try {
  54             InputStream stream = url.openStream();
  55             try {
  56                 byte[] buff = new byte[4];
  57                 ok = stream.read(buff) == 4;
  58                 if (ok) {
  59                     ok =  (buff[0] == 0x50
  60                         && buff[1] == 0x4b
  61                         && buff[2] == 0x03
  62                         && buff[3] == 0x04);
  63                 }
  64             } finally {
  65                 stream.close();
  66             }
  67         } catch (IOException e) {
  68         }
  69         return ok;
  70     }
  71 
  72     public Soundbank getSoundbank(URL url)
  73             throws InvalidMidiDataException, IOException {
  74         if (!isZIP(url))
  75             return null;
  76         ArrayList<Soundbank> soundbanks = new ArrayList<Soundbank>();
  77         URLClassLoader ucl = URLClassLoader.newInstance(new URL[]{url});
  78         InputStream stream = ucl.getResourceAsStream(
  79                 "META-INF/services/javax.sound.midi.Soundbank");
  80         if (stream == null)
  81             return null;
  82         try
  83         {
  84             BufferedReader r = new BufferedReader(new InputStreamReader(stream));
  85             String line = r.readLine();
  86             while (line != null) {
  87                 if (!line.startsWith("#")) {
  88                     try {
  89                         Class<?> c = Class.forName(line.trim(), false, ucl);
  90                         if (Soundbank.class.isAssignableFrom(c)) {
  91                             ReflectUtil.checkPackageAccess(c);
  92                             @SuppressWarnings("deprecation")
  93                             Object o = c.newInstance();
  94                             soundbanks.add((Soundbank) o);
  95                         }
  96                     } catch (ClassNotFoundException |
  97                              InstantiationException |
  98                              IllegalAccessException ignored) {
  99                     }
 100                 }
 101                 line = r.readLine();
 102             }
 103         }
 104         finally
 105         {
 106             stream.close();
 107         }
 108         if (soundbanks.size() == 0)
 109             return null;
 110         if (soundbanks.size() == 1)
 111             return soundbanks.get(0);
 112         SimpleSoundbank sbk = new SimpleSoundbank();
 113         for (Soundbank soundbank : soundbanks)
 114             sbk.addAllInstruments(soundbank);
 115         return sbk;
 116     }
 117 
 118     public Soundbank getSoundbank(InputStream stream)
 119             throws InvalidMidiDataException, IOException {
 120         Objects.requireNonNull(stream);
 121         return null;
 122     }
 123 
 124     public Soundbank getSoundbank(File file)
 125             throws InvalidMidiDataException, IOException {
 126         return getSoundbank(file.toURI().toURL());
 127     }
 128 }