Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/com/sun/media/sound/JDK13Services.java
41161 views
1
/*
2
* Copyright (c) 1999, 2021, 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.security.AccessController;
29
import java.security.PrivilegedAction;
30
import java.util.ArrayList;
31
import java.util.Collections;
32
import java.util.List;
33
import java.util.Properties;
34
35
import javax.sound.midi.Receiver;
36
import javax.sound.midi.Sequencer;
37
import javax.sound.midi.Synthesizer;
38
import javax.sound.midi.Transmitter;
39
import javax.sound.midi.spi.MidiDeviceProvider;
40
import javax.sound.midi.spi.MidiFileReader;
41
import javax.sound.midi.spi.MidiFileWriter;
42
import javax.sound.midi.spi.SoundbankReader;
43
import javax.sound.sampled.Clip;
44
import javax.sound.sampled.Port;
45
import javax.sound.sampled.SourceDataLine;
46
import javax.sound.sampled.TargetDataLine;
47
import javax.sound.sampled.spi.AudioFileReader;
48
import javax.sound.sampled.spi.AudioFileWriter;
49
import javax.sound.sampled.spi.FormatConversionProvider;
50
import javax.sound.sampled.spi.MixerProvider;
51
52
53
/**
54
* JDK13Services uses the Service class in JDK 1.3 to discover a list of service
55
* providers installed in the system.
56
* <p>
57
* This class is public because it is called from javax.sound.midi.MidiSystem
58
* and javax.sound.sampled.AudioSystem. The alternative would be to make
59
* JSSecurityManager public, which is considered worse.
60
*
61
* @author Matthias Pfisterer
62
*/
63
public final class JDK13Services {
64
65
/**
66
* Properties loaded from the properties file for default provider
67
* properties.
68
*/
69
private static Properties properties;
70
71
/**
72
* Private, no-args constructor to ensure against instantiation.
73
*/
74
private JDK13Services() {
75
}
76
77
/**
78
* Obtains a List containing installed instances of the providers for the
79
* requested service. The returned List is immutable.
80
*
81
* @param serviceClass The type of providers requested. This should be one
82
* of AudioFileReader.class, AudioFileWriter.class,
83
* FormatConversionProvider.class, MixerProvider.class,
84
* MidiDeviceProvider.class, MidiFileReader.class,
85
* MidiFileWriter.class or SoundbankReader.class.
86
*
87
* @return A List of providers of the requested type. This List is
88
* immutable.
89
*/
90
public static List<?> getProviders(final Class<?> serviceClass) {
91
final List<?> providers;
92
if (!MixerProvider.class.equals(serviceClass)
93
&& !FormatConversionProvider.class.equals(serviceClass)
94
&& !AudioFileReader.class.equals(serviceClass)
95
&& !AudioFileWriter.class.equals(serviceClass)
96
&& !MidiDeviceProvider.class.equals(serviceClass)
97
&& !SoundbankReader.class.equals(serviceClass)
98
&& !MidiFileWriter.class.equals(serviceClass)
99
&& !MidiFileReader.class.equals(serviceClass)) {
100
providers = new ArrayList<>(0);
101
} else {
102
providers = JSSecurityManager.getProviders(serviceClass);
103
}
104
return Collections.unmodifiableList(providers);
105
}
106
107
/** Obtain the provider class name part of a default provider property.
108
@param typeClass The type of the default provider property. This
109
should be one of Receiver.class, Transmitter.class, Sequencer.class,
110
Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
111
Clip.class or Port.class.
112
@return The value of the provider class name part of the property
113
(the part before the hash sign), if available. If the property is
114
not set or the value has no provider class name part, null is returned.
115
*/
116
public static synchronized String getDefaultProviderClassName(Class<?> typeClass) {
117
String value = null;
118
String defaultProviderSpec = getDefaultProvider(typeClass);
119
if (defaultProviderSpec != null) {
120
int hashpos = defaultProviderSpec.indexOf('#');
121
if (hashpos == 0) {
122
// instance name only; leave value as null
123
} else if (hashpos > 0) {
124
value = defaultProviderSpec.substring(0, hashpos);
125
} else {
126
value = defaultProviderSpec;
127
}
128
}
129
return value;
130
}
131
132
/** Obtain the instance name part of a default provider property.
133
@param typeClass The type of the default provider property. This
134
should be one of Receiver.class, Transmitter.class, Sequencer.class,
135
Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
136
Clip.class or Port.class.
137
@return The value of the instance name part of the property (the
138
part after the hash sign), if available. If the property is not set
139
or the value has no instance name part, null is returned.
140
*/
141
public static synchronized String getDefaultInstanceName(Class<?> typeClass) {
142
String value = null;
143
String defaultProviderSpec = getDefaultProvider(typeClass);
144
if (defaultProviderSpec != null) {
145
int hashpos = defaultProviderSpec.indexOf('#');
146
if (hashpos >= 0 && hashpos < defaultProviderSpec.length() - 1) {
147
value = defaultProviderSpec.substring(hashpos + 1);
148
}
149
}
150
return value;
151
}
152
153
/** Obtain the value of a default provider property.
154
@param typeClass The type of the default provider property. This
155
should be one of Receiver.class, Transmitter.class, Sequencer.class,
156
Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
157
Clip.class or Port.class.
158
@return The complete value of the property, if available.
159
If the property is not set, null is returned.
160
*/
161
private static synchronized String getDefaultProvider(Class<?> typeClass) {
162
if (!SourceDataLine.class.equals(typeClass)
163
&& !TargetDataLine.class.equals(typeClass)
164
&& !Clip.class.equals(typeClass)
165
&& !Port.class.equals(typeClass)
166
&& !Receiver.class.equals(typeClass)
167
&& !Transmitter.class.equals(typeClass)
168
&& !Synthesizer.class.equals(typeClass)
169
&& !Sequencer.class.equals(typeClass)) {
170
return null;
171
}
172
String name = typeClass.getName();
173
@SuppressWarnings("removal")
174
String value = AccessController.doPrivileged(
175
(PrivilegedAction<String>) () -> System.getProperty(name));
176
if (value == null) {
177
value = getProperties().getProperty(name);
178
}
179
if ("".equals(value)) {
180
value = null;
181
}
182
return value;
183
}
184
185
/** Obtain a properties bundle containing property values from the
186
properties file. If the properties file could not be loaded,
187
the properties bundle is empty.
188
*/
189
private static synchronized Properties getProperties() {
190
if (properties == null) {
191
properties = new Properties();
192
JSSecurityManager.loadProperties(properties);
193
}
194
return properties;
195
}
196
}
197
198