Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/sound/sampled/spi/AudioFileReader/RecognizeHugeWaveFiles.java
40527 views
1
/*
2
* Copyright (c) 2016, 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
26
import javax.sound.sampled.AudioFileFormat;
27
import javax.sound.sampled.AudioFormat;
28
import javax.sound.sampled.AudioInputStream;
29
import javax.sound.sampled.AudioSystem;
30
31
/**
32
* @test
33
* @bug 8132782 6729836
34
*/
35
public final class RecognizeHugeWaveFiles {
36
37
/**
38
* The maximum size in bytes per WAVE specification.
39
*/
40
private static final /*unsigned int */ long MAX_UNSIGNED_INT = 0xffffffffL;
41
42
/**
43
* The supported wave pcm_float format and sample size in bits.
44
*/
45
private static final byte[][] waveTypeBits = {
46
{0x0001/*WAVE_FORMAT_PCM*/,1},
47
{0x0001/*WAVE_FORMAT_PCM*/,2},
48
{0x0001/*WAVE_FORMAT_PCM*/,4},
49
{0x0001/*WAVE_FORMAT_PCM*/,8},
50
{0x0001/*WAVE_FORMAT_PCM*/,16},
51
{0x0001/*WAVE_FORMAT_PCM*/,20},
52
{0x0001/*WAVE_FORMAT_PCM*/,24},
53
{0x0001/*WAVE_FORMAT_PCM*/,32},
54
{0x0003/*WAVE_FORMAT_IEEE_FLOAT*/, 32},
55
{0x0006/*WAVE_FORMAT_ALAW*/, 8},
56
{0x0007/*WAVE_FORMAT_MULAW*/, 8}
57
};
58
59
/**
60
* The list of supported sample rates(stored as unsigned int).
61
*/
62
private static final int[] sampleRates = {
63
8000, 11025, 16000, 22050, 32000, 37800, 44056, 44100, 47250, 48000,
64
50000, 50400, 88200, 96000, 176400, 192000, 352800, 2822400,
65
5644800, Integer.MAX_VALUE
66
};
67
68
/**
69
* The list of supported channels (stored as unsigned int).
70
*/
71
private static final int[] channels = {
72
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
73
};
74
75
/**
76
* The list of supported size of data (stored as unsigned int).
77
* <p>
78
* The {@code MAX_UNSIGNED_INT} is a maximum size.
79
*/
80
private static final long[] dataSizes = {
81
0, 1, 2, 3, Integer.MAX_VALUE - 1, Integer.MAX_VALUE,
82
(long) Integer.MAX_VALUE + 1, MAX_UNSIGNED_INT - 1, MAX_UNSIGNED_INT
83
};
84
85
public static void main(final String[] args) throws Exception {
86
for (final byte[] type : waveTypeBits) {
87
for (final int sampleRate : sampleRates) {
88
for (final int channel : channels) {
89
for (final long dataSize : dataSizes) {
90
testAFF(type, sampleRate, channel, dataSize);
91
testAIS(type, sampleRate, channel, dataSize);
92
}
93
}
94
}
95
}
96
}
97
98
/**
99
* Tests the {@code AudioFileFormat} fetched from the fake header.
100
* <p>
101
* Note that the frameLength and byteLength are stored as int which means
102
* that {@code AudioFileFormat} will store the data above {@code MAX_INT} as
103
* NOT_SPECIFIED.
104
*/
105
private static void testAFF(final byte[] type, final int rate,
106
final int channel, final long size)
107
throws Exception {
108
final byte[] header = createHeader(type, rate, channel, size);
109
final ByteArrayInputStream fake = new ByteArrayInputStream(header);
110
final AudioFileFormat aff = AudioSystem.getAudioFileFormat(fake);
111
final AudioFormat format = aff.getFormat();
112
113
if (aff.getType() != AudioFileFormat.Type.WAVE) {
114
throw new RuntimeException("Error");
115
}
116
117
final long frameLength = size / format.getFrameSize();
118
if (frameLength <= Integer.MAX_VALUE) {
119
if (aff.getFrameLength() != frameLength) {
120
System.err.println("Expected: " + frameLength);
121
System.err.println("Actual: " + aff.getFrameLength());
122
throw new RuntimeException();
123
}
124
} else {
125
if (aff.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
126
System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
127
System.err.println("Actual: " + aff.getFrameLength());
128
throw new RuntimeException();
129
}
130
}
131
validateFormat(type[1], rate, channel, aff.getFormat());
132
}
133
134
/**
135
* Tests the {@code AudioInputStream} fetched from the fake header.
136
* <p>
137
* Note that the frameLength is stored as long which means that {@code
138
* AudioInputStream} must store all possible data from wave file.
139
*/
140
private static void testAIS(final byte[] type, final int rate,
141
final int channel, final long size)
142
throws Exception {
143
final byte[] header = createHeader(type, rate, channel, size);
144
final ByteArrayInputStream fake = new ByteArrayInputStream(header);
145
final AudioInputStream ais = AudioSystem.getAudioInputStream(fake);
146
final AudioFormat format = ais.getFormat();
147
final long frameLength = size / format.getFrameSize();
148
if (frameLength != ais.getFrameLength()) {
149
System.err.println("Expected: " + frameLength);
150
System.err.println("Actual: " + ais.getFrameLength());
151
throw new RuntimeException();
152
}
153
if (ais.available() < 0) {
154
System.err.println("available should be >=0: " + ais.available());
155
throw new RuntimeException();
156
}
157
158
validateFormat(type[1], rate, channel, format);
159
}
160
161
/**
162
* Tests that format contains the same data as were provided to the fake
163
* stream.
164
*/
165
private static void validateFormat(final byte bits, final int rate,
166
final int channel,
167
final AudioFormat format) {
168
169
if (Float.compare(format.getSampleRate(), rate) != 0) {
170
System.err.println("Expected: " + rate);
171
System.err.println("Actual: " + format.getSampleRate());
172
throw new RuntimeException();
173
}
174
if (format.getChannels() != channel) {
175
System.err.println("Expected: " + channel);
176
System.err.println("Actual: " + format.getChannels());
177
throw new RuntimeException();
178
}
179
int frameSize = ((bits + 7) / 8) * channel;
180
if (format.getFrameSize() != frameSize) {
181
System.err.println("Expected: " + frameSize);
182
System.err.println("Actual: " + format.getFrameSize());
183
throw new RuntimeException();
184
}
185
}
186
187
/**
188
* Creates the custom header of the WAVE file. It is expected that all
189
* passed data are supported.
190
*/
191
private static byte[] createHeader(final byte[] type, final int rate,
192
final int channel, final long size) {
193
final int frameSize = ((type[1] + 7) / 8) * channel;
194
return new byte[]{
195
// RIFF_MAGIC
196
0x52, 0x49, 0x46, 0x46,
197
// fileLength
198
-1, -1, -1, -1,
199
// waveMagic
200
0x57, 0x41, 0x56, 0x45,
201
// FMT_MAGIC
202
0x66, 0x6d, 0x74, 0x20,
203
// size
204
16, 0, 0, 0,
205
// wav_type WAVE_FORMAT_IEEE_FLOAT
206
type[0], 0,
207
// channels
208
(byte) (channel), (byte) (channel >> 8),
209
// samplerate
210
(byte) (rate), (byte) (rate >> 8), (byte) (rate >> 16),
211
(byte) (rate >> 24),
212
// framerate
213
1, 0, 0, 0,
214
// framesize
215
(byte) (frameSize), (byte) (frameSize >> 8),
216
// bits
217
type[1], 0,
218
// DATA_MAGIC
219
0x64, 0x61, 0x74, 0x61,
220
// data size
221
(byte) (size), (byte) (size >> 8), (byte) (size >> 16),
222
(byte) (size >> 24)
223
// data
224
, 0, 0, 0, 0, 0
225
};
226
}
227
}
228
229