21596
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 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 |
|
11893
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 7088367
|
|
27 |
* @summary SourceDataLine.write and TargetDataLine.read don't throw ArrayIndexOutOfBoundsException
|
|
28 |
* @author Alex Menkov
|
|
29 |
*/
|
|
30 |
|
|
31 |
import javax.sound.sampled.AudioSystem;
|
|
32 |
import javax.sound.sampled.DataLine;
|
|
33 |
import javax.sound.sampled.Line;
|
|
34 |
import javax.sound.sampled.LineUnavailableException;
|
|
35 |
import javax.sound.sampled.Mixer;
|
|
36 |
import javax.sound.sampled.SourceDataLine;
|
|
37 |
import javax.sound.sampled.TargetDataLine;
|
|
38 |
|
|
39 |
public class DataLine_ArrayIndexOutOfBounds {
|
|
40 |
|
|
41 |
static int total = 0;
|
|
42 |
static int failed = 0;
|
|
43 |
|
|
44 |
// shared buffer for all tests
|
|
45 |
static final byte[] buffer = new byte[5000000];
|
|
46 |
|
|
47 |
// the class describes different test scenarios (buffer properties)
|
|
48 |
static abstract class Scenario {
|
|
49 |
abstract int getBufferOffset(DataLine line);
|
|
50 |
abstract int getBufferLength(DataLine line);
|
|
51 |
}
|
|
52 |
|
|
53 |
// scenarios to tests
|
|
54 |
static Scenario[] scenarios = new Scenario[]{
|
|
55 |
new Scenario() {
|
|
56 |
public String toString() {
|
|
57 |
return "offset is near Integer.MAX_VALUE";
|
|
58 |
}
|
|
59 |
public int getBufferOffset(DataLine line) {
|
|
60 |
return Integer.MAX_VALUE - 4096;
|
|
61 |
}
|
|
62 |
public int getBufferLength(DataLine line) {
|
|
63 |
return 65536;
|
|
64 |
}
|
|
65 |
},
|
|
66 |
new Scenario() {
|
|
67 |
public String toString() {
|
|
68 |
return "offset is less than buffer.length, length is large";
|
|
69 |
}
|
|
70 |
int getBufferOffset(DataLine line) {
|
|
71 |
return buffer.length / 10;
|
|
72 |
}
|
|
73 |
int getBufferLength(DataLine line) {
|
|
74 |
return Integer.MAX_VALUE - getBufferOffset(line) + 4096;
|
|
75 |
}
|
|
76 |
}
|
|
77 |
};
|
|
78 |
|
|
79 |
public static void main(String[] args) throws Exception {
|
|
80 |
Mixer.Info[] infos = AudioSystem.getMixerInfo();
|
|
81 |
log("" + infos.length + " mixers detected");
|
|
82 |
for (int i=0; i<infos.length; i++) {
|
|
83 |
Mixer mixer = AudioSystem.getMixer(infos[i]);
|
|
84 |
log("Mixer " + (i+1) + ": " + infos[i]);
|
|
85 |
try {
|
|
86 |
mixer.open();
|
|
87 |
for (Scenario scenario: scenarios) {
|
|
88 |
testSDL(mixer, scenario);
|
|
89 |
testTDL(mixer, scenario);
|
|
90 |
}
|
|
91 |
mixer.close();
|
|
92 |
} catch (LineUnavailableException ex) {
|
|
93 |
log("LineUnavailableException: " + ex);
|
|
94 |
}
|
|
95 |
}
|
|
96 |
if (failed == 0) {
|
|
97 |
log("PASSED (" + total + " tests)");
|
|
98 |
} else {
|
|
99 |
log("FAILED (" + failed + " of " + total + " tests)");
|
|
100 |
throw new Exception("Test FAILED");
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
final static int STOPPER_DELAY = 5000; // 1 sec
|
|
105 |
|
|
106 |
static class AsyncLineStopper implements Runnable {
|
|
107 |
private final DataLine line;
|
|
108 |
private final long delayMS; // delay before stop the line
|
|
109 |
private final Thread thread;
|
|
110 |
private final Object readyEvent = new Object();
|
|
111 |
private final Object startEvent = new Object();
|
|
112 |
|
|
113 |
public AsyncLineStopper(DataLine line, long delayMS) {
|
|
114 |
this.line = line;
|
|
115 |
this.delayMS = delayMS;
|
|
116 |
thread = new Thread(this);
|
|
117 |
thread.setDaemon(true);
|
|
118 |
// starts the thread and waits until it becomes ready
|
|
119 |
synchronized (readyEvent) {
|
|
120 |
thread.start();
|
|
121 |
try {
|
|
122 |
readyEvent.wait();
|
|
123 |
} catch (InterruptedException ex) { }
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
// makes the delay and then stops the line
|
|
128 |
public void schedule() {
|
|
129 |
synchronized(startEvent) {
|
|
130 |
startEvent.notifyAll();
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
// force stop/close the line
|
|
135 |
public void force() {
|
|
136 |
thread.interrupt();
|
|
137 |
try {
|
|
138 |
thread.join();
|
|
139 |
} catch (InterruptedException ex) {
|
|
140 |
log("join exception: " + ex);
|
|
141 |
}
|
|
142 |
}
|
|
143 |
|
|
144 |
// Runnable implementation
|
|
145 |
public void run() {
|
|
146 |
try {
|
|
147 |
synchronized(readyEvent) {
|
|
148 |
readyEvent.notifyAll();
|
|
149 |
}
|
|
150 |
synchronized(startEvent) {
|
|
151 |
startEvent.wait();
|
|
152 |
}
|
|
153 |
// delay
|
|
154 |
Thread.sleep(delayMS);
|
|
155 |
} catch (InterruptedException ex) {
|
|
156 |
log(" AsyncLineStopper has been interrupted: " + ex);
|
|
157 |
}
|
|
158 |
// and flush
|
|
159 |
log(" stop...");
|
|
160 |
line.stop();
|
|
161 |
log(" close...");
|
|
162 |
line.close();
|
|
163 |
}
|
|
164 |
}
|
|
165 |
|
|
166 |
static void testSDL(Mixer mixer, Scenario scenario) {
|
|
167 |
log(" Testing SDL (scenario: " + scenario + ")...");
|
|
168 |
Line.Info linfo = new Line.Info(SourceDataLine.class);
|
|
169 |
SourceDataLine line = null;
|
|
170 |
try {
|
|
171 |
line = (SourceDataLine)mixer.getLine(linfo);
|
|
172 |
log(" got line: " + line);
|
|
173 |
log(" open...");
|
|
174 |
line.open();
|
|
175 |
} catch (IllegalArgumentException ex) {
|
|
176 |
log(" unsupported (IllegalArgumentException)");
|
|
177 |
return;
|
|
178 |
} catch (LineUnavailableException ex) {
|
|
179 |
log(" unavailable: " + ex);
|
|
180 |
return;
|
|
181 |
}
|
|
182 |
|
|
183 |
total++;
|
|
184 |
|
|
185 |
log(" start...");
|
|
186 |
line.start();
|
|
187 |
|
|
188 |
AsyncLineStopper lineStopper = new AsyncLineStopper(line, STOPPER_DELAY);
|
|
189 |
int offset = scenario.getBufferOffset(line);
|
|
190 |
int len = scenario.getBufferLength(line);
|
|
191 |
// ensure len represents integral number of frames
|
|
192 |
len -= len % line.getFormat().getFrameSize();
|
|
193 |
|
|
194 |
log(" write...");
|
|
195 |
lineStopper.schedule();
|
|
196 |
try {
|
|
197 |
line.write(buffer, offset, len);
|
|
198 |
log(" ERROR: didn't get ArrayIndexOutOfBoundsException");
|
|
199 |
failed++;
|
|
200 |
} catch (ArrayIndexOutOfBoundsException ex) {
|
|
201 |
log(" OK: got ArrayIndexOutOfBoundsException: " + ex);
|
|
202 |
}
|
|
203 |
lineStopper.force();
|
|
204 |
}
|
|
205 |
|
|
206 |
static void testTDL(Mixer mixer, Scenario scenario) {
|
|
207 |
log(" Testing TDL (scenario: " + scenario + ")...");
|
|
208 |
Line.Info linfo = new Line.Info(TargetDataLine.class);
|
|
209 |
TargetDataLine line = null;
|
|
210 |
try {
|
|
211 |
line = (TargetDataLine)mixer.getLine(linfo);
|
|
212 |
log(" got line: " + line);
|
|
213 |
log(" open...");
|
|
214 |
line.open();
|
|
215 |
} catch (IllegalArgumentException ex) {
|
|
216 |
log(" unsupported (IllegalArgumentException)");
|
|
217 |
return;
|
|
218 |
} catch (LineUnavailableException ex) {
|
|
219 |
log(" unavailable: " + ex);
|
|
220 |
return;
|
|
221 |
}
|
|
222 |
|
|
223 |
total++;
|
|
224 |
|
|
225 |
log(" start...");
|
|
226 |
line.start();
|
|
227 |
|
|
228 |
AsyncLineStopper lineStopper = new AsyncLineStopper(line, STOPPER_DELAY);
|
|
229 |
int offset = scenario.getBufferOffset(line);
|
|
230 |
int len = scenario.getBufferLength(line);
|
|
231 |
// ensure len represents integral number of frames
|
|
232 |
len -= len % line.getFormat().getFrameSize();
|
|
233 |
|
|
234 |
log(" read...");
|
|
235 |
try {
|
|
236 |
line.read(buffer, offset, len);
|
|
237 |
log(" ERROR: didn't get ArrayIndexOutOfBoundsException");
|
|
238 |
failed++;
|
|
239 |
} catch (ArrayIndexOutOfBoundsException ex) {
|
|
240 |
log(" OK: got ArrayIndexOutOfBoundsException: " + ex);
|
|
241 |
}
|
|
242 |
lineStopper.force();
|
|
243 |
}
|
|
244 |
|
|
245 |
static void log(String s) {
|
|
246 |
System.out.println(s);
|
|
247 |
System.out.flush();
|
|
248 |
}
|
|
249 |
}
|