Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/sound/sampled/spi/FormatConversionProvider/ExpectedNPEOnNull.java
41159 views
1
/*
2
* Copyright (c) 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.ByteArrayInputStream;
25
import java.util.ArrayList;
26
import java.util.List;
27
import java.util.Objects;
28
29
import javax.sound.sampled.AudioFileFormat;
30
import javax.sound.sampled.AudioFormat;
31
import javax.sound.sampled.AudioFormat.Encoding;
32
import javax.sound.sampled.AudioInputStream;
33
import javax.sound.sampled.AudioSystem;
34
import javax.sound.sampled.spi.FormatConversionProvider;
35
36
import static java.util.ServiceLoader.load;
37
import static javax.sound.sampled.AudioFileFormat.*;
38
39
/**
40
* @test
41
* @bug 8135100
42
* @author Sergey Bylokhov
43
*/
44
public final class ExpectedNPEOnNull {
45
46
/**
47
* We will try to use all encoding, in this case all our providers will be
48
* covered by supported/unsupported encoding.
49
*/
50
private static final Encoding[] encodings = {Encoding.PCM_SIGNED,
51
Encoding.PCM_UNSIGNED,
52
Encoding.PCM_FLOAT,
53
Encoding.ULAW, Encoding.ALAW,
54
new Encoding("test")};
55
56
/**
57
* We will try to use all types, in this case all our providers will be
58
* covered by supported/unsupported types.
59
*/
60
private static final Type[] types = {Type.WAVE, Type.AU, Type.AIFF,
61
Type.AIFC, Type.SND,
62
new Type("MIDI", "mid"),
63
new Type("test", "test")};
64
65
/**
66
* We will try to use all supported AudioInputStream, in this case all our
67
* providers will be covered by supported/unsupported streams.
68
*/
69
private static final List<AudioInputStream> aiss = new ArrayList<>();
70
71
static {
72
for (final Encoding encoding : encodings) {
73
for (final Type type : types) {
74
aiss.add(getAIS(type, encoding));
75
}
76
}
77
}
78
79
public static void main(final String[] args) throws Exception {
80
testAS();
81
for (final FormatConversionProvider fcp : load(
82
FormatConversionProvider.class)) {
83
testFCP(fcp);
84
}
85
testFCP(customFCP);
86
}
87
88
/**
89
* Tests the part of AudioSystem API, which implemented via
90
* FormatConversionProvider.
91
*/
92
private static void testAS() throws Exception {
93
94
// AudioSystem#getAudioInputStream(Encoding, AudioInputStream)
95
for (final Encoding encoding : encodings) {
96
try {
97
AudioSystem.getAudioInputStream(encoding, null);
98
throw new RuntimeException("NPE is expected");
99
} catch (final NullPointerException ignored) {
100
}
101
}
102
for (final AudioInputStream stream : aiss) {
103
try {
104
AudioSystem.getAudioInputStream((Encoding) null, stream);
105
throw new RuntimeException("NPE is expected");
106
} catch (final NullPointerException ignored) {
107
}
108
}
109
// AudioSystem#getAudioInputStream(AudioFormat, AudioInputStream)
110
for (final AudioInputStream stream : aiss) {
111
try {
112
AudioSystem.getAudioInputStream((AudioFormat) null, stream);
113
throw new RuntimeException("NPE is expected");
114
} catch (final NullPointerException ignored) {
115
}
116
}
117
for (final Encoding encoding : encodings) {
118
try {
119
AudioSystem.getAudioInputStream(getAFF(encoding), null);
120
throw new RuntimeException("NPE is expected");
121
} catch (final NullPointerException ignored) {
122
}
123
}
124
125
// AudioSystem#getTargetEncodings(AudioFormat)
126
try {
127
AudioSystem.getTargetEncodings((AudioFormat) null);
128
throw new RuntimeException("NPE is expected");
129
} catch (final NullPointerException ignored) {
130
}
131
132
// AudioSystem#getTargetEncodings(AudioFormat.Encoding)
133
try {
134
AudioSystem.getTargetEncodings((Encoding) null);
135
throw new RuntimeException("NPE is expected");
136
} catch (final NullPointerException ignored) {
137
}
138
139
// AudioSystem#getTargetFormats(AudioFormat.Encoding, AudioFormat)
140
for (final Encoding encoding : encodings) {
141
try {
142
AudioSystem.getTargetFormats(null, getAFF(encoding));
143
throw new RuntimeException("NPE is expected");
144
} catch (final NullPointerException ignored) {
145
}
146
}
147
for (final Encoding encoding : encodings) {
148
try {
149
AudioSystem.getTargetFormats(encoding, null);
150
throw new RuntimeException("NPE is expected");
151
} catch (final NullPointerException ignored) {
152
}
153
}
154
155
// AudioSystem#isConversionSupported(AudioFormat, AudioFormat)
156
for (final Encoding encoding : encodings) {
157
try {
158
AudioSystem.isConversionSupported((AudioFormat) null,
159
getAFF(encoding));
160
throw new RuntimeException("NPE is expected");
161
} catch (final NullPointerException ignored) {
162
}
163
}
164
for (final Encoding encoding : encodings) {
165
try {
166
AudioSystem.isConversionSupported(getAFF(encoding), null);
167
throw new RuntimeException("NPE is expected");
168
} catch (final NullPointerException ignored) {
169
}
170
}
171
172
// AudioSystem#isConversionSupported(AudioFormat.Encoding, AudioFormat)
173
for (final Encoding encoding : encodings) {
174
try {
175
AudioSystem.isConversionSupported((Encoding) null,
176
getAFF(encoding));
177
throw new RuntimeException("NPE is expected");
178
} catch (final NullPointerException ignored) {
179
}
180
}
181
for (final Encoding encoding : encodings) {
182
try {
183
AudioSystem.isConversionSupported(encoding, null);
184
throw new RuntimeException("NPE is expected");
185
} catch (final NullPointerException ignored) {
186
}
187
}
188
}
189
190
/**
191
* Tests the FormatConversionProvider API directly.
192
*/
193
private static void testFCP(FormatConversionProvider fcp) throws Exception {
194
195
// FormatConversionProvider#isSourceEncodingSupported(Encoding)
196
try {
197
fcp.isSourceEncodingSupported(null);
198
throw new RuntimeException("NPE is expected: " + fcp);
199
} catch (final NullPointerException ignored) {
200
}
201
202
// FormatConversionProvider#isTargetEncodingSupported(Encoding)
203
try {
204
fcp.isTargetEncodingSupported(null);
205
throw new RuntimeException("NPE is expected: " + fcp);
206
} catch (final NullPointerException ignored) {
207
}
208
209
// FormatConversionProvider#getTargetEncodings()
210
try {
211
fcp.getTargetEncodings(null);
212
throw new RuntimeException("NPE is expected: " + fcp);
213
} catch (final NullPointerException ignored) {
214
}
215
216
// FormatConversionProvider#isConversionSupported(Encoding, AudioFormat)
217
for (final Encoding encoding : encodings) {
218
try {
219
fcp.isConversionSupported((Encoding) null, getAFF(encoding));
220
throw new RuntimeException("NPE is expected: " + fcp);
221
} catch (final NullPointerException ignored) {
222
}
223
}
224
for (final Encoding encoding : encodings) {
225
try {
226
fcp.isConversionSupported(encoding, null);
227
throw new RuntimeException("NPE is expected: " + fcp);
228
} catch (final NullPointerException ignored) {
229
}
230
}
231
232
// FormatConversionProvider#getTargetFormats(Encoding, AudioFormat)
233
for (final Encoding encoding : encodings) {
234
try {
235
fcp.getTargetFormats(null, getAFF(encoding));
236
throw new RuntimeException("NPE is expected: " + fcp);
237
} catch (final NullPointerException ignored) {
238
}
239
}
240
for (final Encoding encoding : encodings) {
241
try {
242
fcp.getTargetFormats(encoding, null);
243
throw new RuntimeException("NPE is expected: " + fcp);
244
} catch (final NullPointerException ignored) {
245
}
246
}
247
248
// FormatConversionProvider#isConversionSupported(AudioFormat,
249
// AudioFormat)
250
for (final Encoding encoding : encodings) {
251
try {
252
fcp.isConversionSupported((AudioFormat) null, getAFF(encoding));
253
throw new RuntimeException("NPE is expected: " + fcp);
254
} catch (final NullPointerException ignored) {
255
}
256
}
257
for (final Encoding encoding : encodings) {
258
try {
259
fcp.isConversionSupported(getAFF(encoding), null);
260
throw new RuntimeException("NPE is expected: " + fcp);
261
} catch (final NullPointerException ignored) {
262
}
263
}
264
265
// FormatConversionProvider#getAudioInputStream(Encoding,
266
// AudioInputStream)
267
for (final AudioInputStream stream : aiss) {
268
try {
269
fcp.getAudioInputStream((Encoding) null, stream);
270
throw new RuntimeException("NPE is expected: " + fcp);
271
} catch (final NullPointerException ignored) {
272
}
273
}
274
for (final Encoding encoding : encodings) {
275
try {
276
fcp.getAudioInputStream(encoding, null);
277
throw new RuntimeException("NPE is expected: " + fcp);
278
} catch (final NullPointerException ignored) {
279
}
280
}
281
282
// FormatConversionProvider#getAudioInputStream(AudioFormat,
283
// AudioInputStream)
284
for (final AudioInputStream stream : aiss) {
285
try {
286
fcp.getAudioInputStream((AudioFormat) null, stream);
287
throw new RuntimeException("NPE is expected: " + fcp);
288
} catch (final NullPointerException ignored) {
289
}
290
}
291
for (final Encoding encoding : encodings) {
292
try {
293
fcp.getAudioInputStream(getAFF(encoding), null);
294
throw new RuntimeException("NPE is expected: " + fcp);
295
} catch (final NullPointerException ignored) {
296
}
297
}
298
}
299
300
/**
301
* Tests some default implementation of FormatConversionProvider API, using
302
* the custom {@code FormatConversionProvider}, which support nothing.
303
*/
304
static FormatConversionProvider customFCP = new FormatConversionProvider() {
305
306
@Override
307
public Encoding[] getSourceEncodings() {
308
return new Encoding[0];
309
}
310
311
@Override
312
public Encoding[] getTargetEncodings() {
313
return new Encoding[0];
314
}
315
316
@Override
317
public Encoding[] getTargetEncodings(AudioFormat sourceFormat) {
318
Objects.requireNonNull(sourceFormat);
319
return new Encoding[0];
320
}
321
322
@Override
323
public AudioFormat[] getTargetFormats(Encoding enc, AudioFormat frmt) {
324
Objects.requireNonNull(enc);
325
Objects.requireNonNull(frmt);
326
return new AudioFormat[0];
327
}
328
329
@Override
330
public AudioInputStream getAudioInputStream(Encoding encoding,
331
AudioInputStream stream) {
332
Objects.requireNonNull(encoding);
333
Objects.requireNonNull(stream);
334
return null;
335
}
336
337
@Override
338
public AudioInputStream getAudioInputStream(AudioFormat format,
339
AudioInputStream stream) {
340
Objects.requireNonNull(format);
341
Objects.requireNonNull(stream);
342
return null;
343
}
344
};
345
346
private static AudioFormat getAFF(final Encoding encoding) {
347
return new AudioFormat(encoding, 44100.0f, 16, 2, 1, 1, true);
348
}
349
350
private static AudioInputStream getAIS(final Type type, Encoding encoding) {
351
AudioFormat af = new AudioFormat(encoding, 44100.0f, 16, 2, 1, 1, true);
352
AudioFileFormat aif = new AudioFileFormat(type, af, 0);
353
ByteArrayInputStream bais = new ByteArrayInputStream(new byte[1024]);
354
return new AudioInputStream(bais, aif.getFormat(), 0);
355
}
356
}
357
358