Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/sound/sampled/spi/AudioFileWriter/WriterCloseInput.java
41159 views
1
/*
2
* Copyright (c) 2011, 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
/**
25
* @test
26
* @bug 7013521
27
* @summary AIFF/AU/WAVE writers close input audio stream
28
* @author Alex Menkov
29
*/
30
31
import java.io.ByteArrayInputStream;
32
import java.io.File;
33
import java.io.IOException;
34
import java.io.InputStream;
35
import java.io.OutputStream;
36
import java.nio.file.Files;
37
import java.nio.file.Paths;
38
39
import javax.sound.sampled.AudioFileFormat;
40
import javax.sound.sampled.AudioFormat;
41
import javax.sound.sampled.AudioInputStream;
42
import javax.sound.sampled.AudioSystem;
43
44
45
public class WriterCloseInput {
46
47
final static AudioFormat audioFormat = new AudioFormat(44100f, 16, 2, true, true);
48
//final static AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.ULAW, 44100f, 8, 2, 2, 44100f, true);
49
final static int frameLength = 44100 * 2; // 2 seconds
50
final static byte[] dataBuffer
51
= new byte[frameLength * (audioFormat.getSampleSizeInBits()/8)
52
* audioFormat.getChannels()];
53
54
static int testTotal = 0;
55
static int testFailed = 0;
56
57
public static void main(String[] args) throws Exception {
58
test(AudioFileFormat.Type.AIFF);
59
test(AudioFileFormat.Type.AU);
60
test(AudioFileFormat.Type.WAVE);
61
62
if (testFailed == 0) {
63
out("All tests passed.");
64
} else {
65
out("" + testFailed + " of " + testTotal + " tests FAILED.");
66
System.out.flush();
67
throw new RuntimeException("Test FAILED.");
68
}
69
}
70
71
static void test(AudioFileFormat.Type fileType) {
72
test(fileType, frameLength);
73
test(fileType, AudioSystem.NOT_SPECIFIED);
74
}
75
76
static void test(AudioFileFormat.Type fileType, int length) {
77
test(fileType, length, false);
78
test(fileType, length, true);
79
}
80
81
static void test(AudioFileFormat.Type fileType, int length, boolean isFile) {
82
testTotal++;
83
out("Testing fileType: " + fileType
84
+ ", frameLength: " + (length >= 0 ? length : "unspecified")
85
+ ", output: " + (isFile ? "File" : "OutputStream"));
86
AudioInputStream inStream = new ThrowAfterCloseStream(
87
new ByteArrayInputStream(dataBuffer), audioFormat, length);
88
89
AudioSystem.isFileTypeSupported(fileType, inStream);
90
91
try {
92
if (isFile) {
93
File f = File.createTempFile("WriterCloseInput" + testTotal, "tmp");
94
AudioSystem.write(inStream, fileType, f);
95
Files.delete(Paths.get(f.getAbsolutePath()));
96
} else {
97
OutputStream outStream = new NullOutputStream();
98
AudioSystem.write(inStream, fileType, outStream);
99
}
100
} catch (Exception ex) {
101
// this is not failure
102
out("SKIPPED (AudioSystem.write exception): " + ex.getMessage());
103
//out(ex);
104
inStream = null;
105
}
106
107
if (inStream != null) {
108
try {
109
// test if the stream is closed
110
inStream.available();
111
out("PASSED");
112
} catch (IOException ex) {
113
testFailed++;
114
out("FAILED: " + ex.getMessage());
115
//out(ex);
116
}
117
}
118
out("");
119
}
120
121
static class ThrowAfterCloseStream extends AudioInputStream {
122
private boolean closed = false;
123
public ThrowAfterCloseStream(InputStream in, AudioFormat format, long length) {
124
super(in, format, length);
125
}
126
@Override
127
public void close() {
128
closed = true;
129
}
130
@Override
131
public int available() throws IOException {
132
if (closed) {
133
throw new IOException("The stream has been closed");
134
}
135
return 1;
136
}
137
}
138
139
static class NullOutputStream extends OutputStream {
140
@Override
141
public void write(int b) throws IOException {
142
// nop
143
}
144
}
145
146
static void out(String s) {
147
System.out.println(s);
148
}
149
150
static void out(Exception ex) {
151
ex.printStackTrace(System.out);
152
}
153
}
154
155