Path: blob/master/test/jdk/javax/sound/sampled/Clip/AudioContentHandlers.java
41153 views
/*1* Copyright (c) 2018, 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 java.applet.AudioClip;24import java.io.ByteArrayInputStream;25import java.io.File;26import java.io.IOException;27import java.io.InputStream;28import java.nio.file.Files;29import java.util.ArrayList;30import java.util.LinkedList;31import java.util.List;3233import javax.sound.sampled.AudioFileFormat;34import javax.sound.sampled.AudioFormat;35import javax.sound.sampled.AudioInputStream;36import javax.sound.sampled.AudioSystem;3738import static javax.sound.sampled.AudioFileFormat.Type.AIFC;39import static javax.sound.sampled.AudioFileFormat.Type.AIFF;40import static javax.sound.sampled.AudioFileFormat.Type.AU;41import static javax.sound.sampled.AudioFileFormat.Type.SND;42import static javax.sound.sampled.AudioFileFormat.Type.WAVE;4344/**45* @test46* @bug 820445447* @summary URL.getContent() should return AudioClip for supported formats48* @run main/othervm -mx128m AudioContentHandlers49*/50public final class AudioContentHandlers {5152private static final List<AudioFormat> formats = new ArrayList<>();5354private static final AudioFormat.Encoding[] encodings =55{AudioFormat.Encoding.ALAW, AudioFormat.Encoding.ULAW,56AudioFormat.Encoding.PCM_SIGNED,57AudioFormat.Encoding.PCM_UNSIGNED,58AudioFormat.Encoding.PCM_FLOAT};5960private static final AudioFileFormat.Type[] types =61{WAVE, AU, AIFF, AIFC, SND};6263static {64for (final AudioFormat.Encoding enc : encodings) {65formats.add(new AudioFormat(enc, 44100, 8, 1, 1, 44100, true));66formats.add(new AudioFormat(enc, 44100, 8, 1, 1, 44100, false));67}68}6970public static void main(final String[] args) throws Exception {71for (final AudioFileFormat.Type type : types) {72for (final AudioFormat format : formats) {73File file = new File("audio." + type.getExtension());74try {75AudioSystem.write(getStream(format), type, file);76} catch (IOException | IllegalArgumentException ignored) {77continue;78}79AudioClip content;80try {81content = (AudioClip) file.toURL().getContent();82// We need to generate OOM because the stream in AudioClip83// will be closed in finalize().84generateOOME();85} finally {86Files.delete(file.toPath());87}88if (content == null) {89throw new RuntimeException("Content is null");90}91}92}93}9495private static AudioInputStream getStream(final AudioFormat format) {96final InputStream in = new ByteArrayInputStream(new byte[100]);97return new AudioInputStream(in, format, 10);98}99100private static void generateOOME() throws Exception {101List<Object> leak = new LinkedList<>();102try {103while (true) {104leak.add(new byte[1024 * 1024]);105}106} catch (OutOfMemoryError ignored) {107}108// Give the GC a chance at that weakref in case of slow systems109Thread.sleep(2000);110}111}112113114