Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/media/sound/JARSoundbankReader.java
38924 views
1
/*
2
* Copyright (c) 2007, 2013, 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
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
import sun.reflect.misc.ReflectUtil;
40
41
/**
42
* JarSoundbankReader is used to read soundbank object from jar files.
43
*
44
* @author Karl Helgason
45
*/
46
public final class JARSoundbankReader extends SoundbankReader {
47
48
private static boolean isZIP(URL url) {
49
boolean ok = false;
50
try {
51
InputStream stream = url.openStream();
52
try {
53
byte[] buff = new byte[4];
54
ok = stream.read(buff) == 4;
55
if (ok) {
56
ok = (buff[0] == 0x50
57
&& buff[1] == 0x4b
58
&& buff[2] == 0x03
59
&& buff[3] == 0x04);
60
}
61
} finally {
62
stream.close();
63
}
64
} catch (IOException e) {
65
}
66
return ok;
67
}
68
69
public Soundbank getSoundbank(URL url)
70
throws InvalidMidiDataException, IOException {
71
if (!isZIP(url))
72
return null;
73
ArrayList<Soundbank> soundbanks = new ArrayList<Soundbank>();
74
URLClassLoader ucl = URLClassLoader.newInstance(new URL[]{url});
75
InputStream stream = ucl.getResourceAsStream(
76
"META-INF/services/javax.sound.midi.Soundbank");
77
if (stream == null)
78
return null;
79
try
80
{
81
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
82
String line = r.readLine();
83
while (line != null) {
84
if (!line.startsWith("#")) {
85
try {
86
Class<?> c = Class.forName(line.trim(), false, ucl);
87
if (Soundbank.class.isAssignableFrom(c)) {
88
Object o = ReflectUtil.newInstance(c);
89
soundbanks.add((Soundbank) o);
90
}
91
} catch (ClassNotFoundException ignored) {
92
} catch (InstantiationException ignored) {
93
} catch (IllegalAccessException ignored) {
94
}
95
}
96
line = r.readLine();
97
}
98
}
99
finally
100
{
101
stream.close();
102
}
103
if (soundbanks.size() == 0)
104
return null;
105
if (soundbanks.size() == 1)
106
return soundbanks.get(0);
107
SimpleSoundbank sbk = new SimpleSoundbank();
108
for (Soundbank soundbank : soundbanks)
109
sbk.addAllInstruments(soundbank);
110
return sbk;
111
}
112
113
public Soundbank getSoundbank(InputStream stream)
114
throws InvalidMidiDataException, IOException {
115
return null;
116
}
117
118
public Soundbank getSoundbank(File file)
119
throws InvalidMidiDataException, IOException {
120
return getSoundbank(file.toURI().toURL());
121
}
122
}
123
124