Path: blob/master/test/jdk/javax/sound/sampled/DataLine/DataLineInfoNegBufferSize.java
41152 views
/*1* Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.sound.sampled.AudioFormat;24import javax.sound.sampled.AudioSystem;25import javax.sound.sampled.Clip;26import javax.sound.sampled.DataLine;27import javax.sound.sampled.LineUnavailableException;28import javax.sound.sampled.Mixer;2930/**31* @test32* @bug 502123433* @summary Using -2 for buffer size will fail retrieval of lines34*/35public class DataLineInfoNegBufferSize {3637/**38* returns:39* 0: OK40* 1: IAE41* 2: other exception42* 3: line not available43*/44public static int run(Mixer m, int bufferSize) {45int res;46int frameCount = 441000; // lets say 10 seconds47AudioFormat f = new AudioFormat(44100.0f, 16, 2, true, false);48Clip clip = null;49try {50System.out.println("Requesting clip from Mixer "51+(m==null?"default":m.toString())52+" with bufferSize"+bufferSize);53DataLine.Info info = new DataLine.Info(Clip.class, f, bufferSize);54if (m==null) {55clip = (Clip) AudioSystem.getLine(info);56} else {57clip = (Clip) m.getLine(info);58}59System.out.println("Got clip: "+clip+" with Buffer size "+clip.getBufferSize());6061res = 0;62} catch (LineUnavailableException luae) {63System.out.println(luae);64res = 3; // line not available65} catch (IllegalArgumentException iae) {66System.out.println(iae);67res = 1;68} catch (Throwable t) {69System.out.println("->Exception:"+t);70t.printStackTrace();71res=2; // other exception72}73return res;74}7576public static void main(String[] args) throws Exception {77if (isSoundcardInstalled()) {78int res=0;79int count = 0;80Mixer.Info[] infos = AudioSystem.getMixerInfo();81for (int i = -1; i<infos.length; i++) {82try {83Mixer m;84if (i == -1) {85m = null;86} else {87m = AudioSystem.getMixer(infos[i]);88}89int r = run(m, AudioSystem.NOT_SPECIFIED);90// only continue if successful91if (r == 0) {92count++;93r = run(m, -2);94if (r == 1) {95// only fail if IAE was thrown96System.out.println("#FAILED: using -2 for buffer size does not work!");97res = 1;98}99}100} catch (Exception e) {101}102}103if (res!=1) {104System.out.println("Test passed");105} else {106if (count == 0) {107System.err.println("Test could not execute -- no suitable mixers installed. NOT failed");108}109throw new Exception("Test FAILED!");110}111}112}113114/**115* Returns true if at least one soundcard is correctly installed116* on the system.117*/118public static boolean isSoundcardInstalled() {119boolean result = false;120try {121Mixer.Info[] mixers = AudioSystem.getMixerInfo();122if (mixers.length > 0) {123result = AudioSystem.getSourceDataLine(null) != null;124}125} catch (Exception e) {126System.err.println("Exception occured: "+e);127}128if (!result) {129System.err.println("Soundcard does not exist or sound drivers not installed!");130System.err.println("This test requires sound drivers for execution.");131}132return result;133}134}135136137