8167435: IllegalArgumentException is not thrown by Clip.open(AudioFormat,byte[], int, int)

Reviewed-by: amenkov
This commit is contained in:
Sergey Bylokhov 2016-10-14 21:43:00 +03:00
parent f97849aa84
commit 07c0902eac
4 changed files with 124 additions and 6 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -1034,6 +1034,7 @@ final class DirectAudioDevice extends AbstractMixer {
// $$fb part of fix for 4679187: Clip.open() throws unexpected Exceptions
Toolkit.isFullySpecifiedAudioFormat(format);
Toolkit.validateBuffer(format.getFrameSize(), bufferSize);
byte[] newData = new byte[bufferSize];
System.arraycopy(data, offset, newData, 0, bufferSize);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -363,9 +363,7 @@ public final class SoftMixingClip extends SoftMixingDataLine implements Clip {
if (AudioFloatConverter.getConverter(format) == null)
throw new IllegalArgumentException("Invalid format : "
+ format.toString());
if (bufferSize % format.getFrameSize() != 0)
throw new IllegalArgumentException(
"Buffer size does not represent an integral number of sample frames!");
Toolkit.validateBuffer(format.getFrameSize(), bufferSize);
if (data != null) {
this.data = Arrays.copyOf(data, data.length);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -149,6 +149,20 @@ public final class Toolkit {
return (long) (((double) frames) / format.getFrameRate() * 1000000.0d);
}
/**
* Throws an exception if the buffer size does not represent an integral
* number of sample frames.
*/
static void validateBuffer(final int frameSize, final int bufferSize) {
if (bufferSize % frameSize == 0) {
return;
}
throw new IllegalArgumentException(String.format(
"Buffer size (%d) does not represent an integral number of "
+ "sample frames (%d)", bufferSize, frameSize));
}
static void isFullySpecifiedAudioFormat(AudioFormat format) {
if (!format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)
&& !format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)

View File

@ -0,0 +1,105 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.util.ArrayList;
import java.util.List;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFormat.Encoding;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
/**
* @test
* @bug 8167435
*/
public final class OpenNonIntegralNumberOfSampleframes {
/**
* We will try to use all formats, in this case all our providers will be
* covered by supported/unsupported formats.
*/
private static final List<AudioFormat> formats = new ArrayList<>(2900);
private static final Encoding[] encodings = {
Encoding.ALAW, Encoding.ULAW, Encoding.PCM_SIGNED,
Encoding.PCM_UNSIGNED, Encoding.PCM_FLOAT
};
private static final int[] sampleRates = {
8000, 11025, 16000, 32000, 44100
};
private static final int[] sampleBits = {
4, 8, 11, 16, 20, 24, 32, 48, 64, 128
};
private static final int[] channels = {
1, 2, 3, 4, 5, 6
};
static {
for (final Boolean end : new boolean[]{false, true}) {
for (final int sampleSize : sampleBits) {
for (final int sampleRate : sampleRates) {
for (final int channel : channels) {
final int frameSize = ((sampleSize + 7) / 8) * channel;
if (frameSize == 1) {
// frameSize=1 is ok for any buffers, skip it
continue;
}
for (final Encoding enc : encodings) {
formats.add(
new AudioFormat(enc, sampleRate, sampleSize,
channel, frameSize,
sampleRate, end));
}
}
}
}
}
}
public static void main(final String[] args) {
for (final AudioFormat af : formats) {
try (Clip clip = AudioSystem.getClip()) {
final int bufferSize = af.getFrameSize() + 1;
try {
clip.open(af, new byte[100], 0, bufferSize);
} catch (final IllegalArgumentException ignored) {
// expected exception
continue;
} catch (final LineUnavailableException e) {
// should not occur, we passed incorrect bufferSize
e.printStackTrace();
}
System.err.println("af = " + af);
System.err.println("bufferSize = " + bufferSize);
throw new RuntimeException("Expected exception is not thrown");
} catch (final LineUnavailableException ignored) {
// the test is not applicable
}
}
}
}