47973
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, 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.BufferedInputStream;
|
|
25 |
import java.io.ByteArrayInputStream;
|
|
26 |
import java.io.ByteArrayOutputStream;
|
|
27 |
import java.io.IOException;
|
|
28 |
import java.io.InputStream;
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import java.util.List;
|
|
31 |
|
|
32 |
import javax.sound.sampled.AudioFileFormat;
|
|
33 |
import javax.sound.sampled.AudioFormat;
|
|
34 |
import javax.sound.sampled.AudioInputStream;
|
|
35 |
import javax.sound.sampled.UnsupportedAudioFileException;
|
|
36 |
import javax.sound.sampled.spi.AudioFileReader;
|
|
37 |
import javax.sound.sampled.spi.AudioFileWriter;
|
|
38 |
|
|
39 |
import static java.util.ServiceLoader.load;
|
|
40 |
import static javax.sound.sampled.AudioFileFormat.Type.AIFC;
|
|
41 |
import static javax.sound.sampled.AudioFileFormat.Type.AIFF;
|
|
42 |
import static javax.sound.sampled.AudioFileFormat.Type.AU;
|
|
43 |
import static javax.sound.sampled.AudioFileFormat.Type.SND;
|
|
44 |
import static javax.sound.sampled.AudioFileFormat.Type.WAVE;
|
|
45 |
import static javax.sound.sampled.AudioSystem.NOT_SPECIFIED;
|
|
46 |
|
|
47 |
/**
|
|
48 |
* @test
|
|
49 |
* @bug 8191384
|
|
50 |
* @summary the stream returned by AudioFileReader should close its data stream
|
|
51 |
*/
|
|
52 |
public final class AudioInputStreamClose {
|
|
53 |
|
|
54 |
static final class StreamWrapper extends BufferedInputStream {
|
|
55 |
|
|
56 |
private boolean open = true;
|
|
57 |
|
|
58 |
StreamWrapper(final InputStream in) {
|
|
59 |
super(in);
|
|
60 |
}
|
|
61 |
|
|
62 |
@Override
|
|
63 |
public void close() throws IOException {
|
|
64 |
super.close();
|
|
65 |
open = false;
|
|
66 |
}
|
|
67 |
|
|
68 |
boolean isOpen() {
|
|
69 |
return open;
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
|
74 |
* We will try to use all formats, in this case all our providers will be
|
|
75 |
* covered by supported/unsupported formats.
|
|
76 |
*/
|
|
77 |
private static final List<AudioFormat> formats = new ArrayList<>(23000);
|
|
78 |
|
|
79 |
private static final AudioFormat.Encoding[] encodings = {
|
|
80 |
AudioFormat.Encoding.ALAW, AudioFormat.Encoding.ULAW,
|
|
81 |
AudioFormat.Encoding.PCM_SIGNED, AudioFormat.Encoding.PCM_UNSIGNED,
|
|
82 |
AudioFormat.Encoding.PCM_FLOAT, new AudioFormat.Encoding("Test")
|
|
83 |
};
|
|
84 |
|
|
85 |
private static final int[] sampleBits = {1, 4, 8, 11, 16, 20, 24, 32};
|
|
86 |
|
|
87 |
private static final int[] channels = {1, 2, 3, 4, 5};
|
|
88 |
|
|
89 |
private static final AudioFileFormat.Type[] types = {
|
|
90 |
WAVE, AU, AIFF, AIFC, SND,
|
|
91 |
new AudioFileFormat.Type("TestName", "TestExt")
|
|
92 |
};
|
|
93 |
|
|
94 |
private static final int FRAME_LENGTH = 10;
|
|
95 |
|
|
96 |
static {
|
|
97 |
for (final int sampleSize : sampleBits) {
|
|
98 |
for (final int channel : channels) {
|
|
99 |
for (final AudioFormat.Encoding enc : encodings) {
|
|
100 |
final int frameSize = ((sampleSize + 7) / 8) * channel;
|
|
101 |
formats.add(new AudioFormat(enc, 44100, sampleSize, channel,
|
|
102 |
frameSize, 44100, true));
|
|
103 |
formats.add(new AudioFormat(enc, 44100, sampleSize, channel,
|
|
104 |
frameSize, 44100, false));
|
|
105 |
}
|
|
106 |
}
|
|
107 |
}
|
|
108 |
}
|
|
109 |
|
|
110 |
public static void main(final String[] args) throws IOException {
|
|
111 |
for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
|
|
112 |
for (final AudioFileReader afr : load(AudioFileReader.class)) {
|
|
113 |
for (final AudioFileFormat.Type type : types) {
|
|
114 |
for (final AudioFormat from : formats) {
|
|
115 |
test(afw, afr, type, getStream(from, true));
|
|
116 |
test(afw, afr, type, getStream(from, false));
|
|
117 |
}
|
|
118 |
}
|
|
119 |
}
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
/**
|
|
124 |
* Writes and reads the data to/from the stream.
|
|
125 |
*/
|
|
126 |
private static void test(final AudioFileWriter afw,
|
|
127 |
final AudioFileReader afr,
|
|
128 |
final AudioFileFormat.Type type,
|
|
129 |
final AudioInputStream ais)
|
|
130 |
throws IOException {
|
|
131 |
try {
|
|
132 |
final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
133 |
afw.write(ais, type, out);
|
|
134 |
final InputStream input = new ByteArrayInputStream(out.toByteArray());
|
|
135 |
final StreamWrapper wrapper = new StreamWrapper(input);
|
|
136 |
|
|
137 |
// the wrapper should be closed as well
|
|
138 |
afr.getAudioInputStream(wrapper).close();
|
|
139 |
|
|
140 |
if (wrapper.isOpen()) {
|
|
141 |
System.err.println("Writer = " + afw);
|
|
142 |
System.err.println("Reader = " + afr);
|
|
143 |
throw new RuntimeException("Stream was not closed");
|
|
144 |
}
|
|
145 |
} catch (IOException | IllegalArgumentException |
|
|
146 |
UnsupportedAudioFileException ignored) {
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
private static AudioInputStream getStream(final AudioFormat format,
|
|
151 |
final boolean frameLength) {
|
|
152 |
final int dataSize = FRAME_LENGTH * format.getFrameSize();
|
|
153 |
byte[] buf = new byte[dataSize];
|
|
154 |
final InputStream in = new ByteArrayInputStream(buf);
|
|
155 |
if (frameLength) {
|
|
156 |
return new AudioInputStream(in, format, FRAME_LENGTH);
|
|
157 |
} else {
|
|
158 |
return new AudioInputStream(in, format, NOT_SPECIFIED);
|
|
159 |
}
|
|
160 |
}
|
|
161 |
}
|