From 07c0902eac084d824a9fc4ca79ae9aff1caa27ce Mon Sep 17 00:00:00 2001 From: Sergey Bylokhov Date: Fri, 14 Oct 2016 21:43:00 +0300 Subject: [PATCH] 8167435: IllegalArgumentException is not thrown by Clip.open(AudioFormat,byte[], int, int) Reviewed-by: amenkov --- .../sun/media/sound/DirectAudioDevice.java | 3 +- .../com/sun/media/sound/SoftMixingClip.java | 6 +- .../classes/com/sun/media/sound/Toolkit.java | 16 ++- .../OpenNonIntegralNumberOfSampleframes.java | 105 ++++++++++++++++++ 4 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 jdk/test/javax/sound/sampled/Clip/OpenNonIntegralNumberOfSampleframes.java diff --git a/jdk/src/java.desktop/share/classes/com/sun/media/sound/DirectAudioDevice.java b/jdk/src/java.desktop/share/classes/com/sun/media/sound/DirectAudioDevice.java index f65351e31c8..27c7e1bc7f6 100644 --- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/DirectAudioDevice.java +++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/DirectAudioDevice.java @@ -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); diff --git a/jdk/src/java.desktop/share/classes/com/sun/media/sound/SoftMixingClip.java b/jdk/src/java.desktop/share/classes/com/sun/media/sound/SoftMixingClip.java index af3577493fe..020d32ed6c4 100644 --- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/SoftMixingClip.java +++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/SoftMixingClip.java @@ -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); diff --git a/jdk/src/java.desktop/share/classes/com/sun/media/sound/Toolkit.java b/jdk/src/java.desktop/share/classes/com/sun/media/sound/Toolkit.java index cc1d0a91c49..1b903a3bcf1 100644 --- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/Toolkit.java +++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/Toolkit.java @@ -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) diff --git a/jdk/test/javax/sound/sampled/Clip/OpenNonIntegralNumberOfSampleframes.java b/jdk/test/javax/sound/sampled/Clip/OpenNonIntegralNumberOfSampleframes.java new file mode 100644 index 00000000000..15b26416a10 --- /dev/null +++ b/jdk/test/javax/sound/sampled/Clip/OpenNonIntegralNumberOfSampleframes.java @@ -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 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 + } + } + } +}