Path: blob/master/test/jdk/javax/sound/midi/Soundbanks/ExtraCharInSoundbank.java
41154 views
/*1* Copyright (c) 2002, 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.midi.Instrument;24import javax.sound.midi.MidiSystem;25import javax.sound.midi.Soundbank;26import javax.sound.midi.Synthesizer;27import javax.sound.sampled.AudioSystem;28import javax.sound.sampled.Mixer;2930/**31* @test32* @bug 442976233* @summary Some instrument names in some soundbanks include bad extra characters34*/35public class ExtraCharInSoundbank {3637private static void printName(String loadedName)38{39System.out.println("Loaded Name: " + loadedName);40byte[] theLoadedNameByteArray = loadedName.getBytes();4142System.out.print("Name Bytes: ");43for(int i = 0; i < theLoadedNameByteArray.length; i++)44System.out.print((Integer.toHexString((int)theLoadedNameByteArray[i]).toUpperCase()) + " ");45System.out.println("");46System.out.println("");47}4849private static boolean containsControlChar(String name) {50byte[] bytes = name.getBytes();51for (int i = 0; i < bytes.length; i++) {52if (bytes[i] < 32) {53return true;54}55}56return false;57}5859public static boolean checkInstrumentNames(Synthesizer theSynthesizer)60{61boolean containsControlCharacters = false;6263Instrument[] theLoadedInstruments = theSynthesizer.getLoadedInstruments();6465System.out.println("Checking soundbank...");66for(int theInstrumentIndex = 0; theInstrumentIndex < theLoadedInstruments.length; theInstrumentIndex++) {67String name = theLoadedInstruments[theInstrumentIndex].getName();68if (containsControlChar(name)) {69containsControlCharacters = true;70System.out.print("Instrument[" + theInstrumentIndex + "] contains unexpected control characters: ");71printName(name);72}73}74return !containsControlCharacters;75}7677public static void main(String[] args) throws Exception {78// the internal synthesizer needs a soundcard to work properly79if (!isSoundcardInstalled()) {80return;81}82Synthesizer theSynth = MidiSystem.getSynthesizer();83System.out.println("Got synth: "+theSynth);84theSynth.open();85try {86Soundbank theSoundbank = theSynth.getDefaultSoundbank();87System.out.println("Got soundbank: "+theSoundbank);88theSynth.loadAllInstruments(theSoundbank);89try {90if (!checkInstrumentNames(theSynth)) {91throw new Exception("Test failed");92}93} finally {94theSynth.unloadAllInstruments(theSoundbank);95}96} finally {97theSynth.close();98}99System.out.println("Test passed.");100}101102/**103* Returns true if at least one soundcard is correctly installed104* on the system.105*/106public static boolean isSoundcardInstalled() {107boolean result = false;108try {109Mixer.Info[] mixers = AudioSystem.getMixerInfo();110if (mixers.length > 0) {111result = AudioSystem.getSourceDataLine(null) != null;112}113} catch (Exception e) {114System.err.println("Exception occured: "+e);115}116if (!result) {117System.err.println("Soundcard does not exist or sound drivers not installed!");118System.err.println("This test requires sound drivers for execution.");119}120return result;121}122}123124125