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