8204454: Remove of sun.applet.AppletAudioClip
Reviewed-by: prr
This commit is contained in:
parent
fe746c5d2a
commit
89204c3373
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -30,6 +30,8 @@ import java.io.BufferedInputStream;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
|
||||||
import javax.sound.midi.InvalidMidiDataException;
|
import javax.sound.midi.InvalidMidiDataException;
|
||||||
import javax.sound.midi.MetaEventListener;
|
import javax.sound.midi.MetaEventListener;
|
||||||
@ -76,6 +78,7 @@ public final class JavaSoundAudioClip implements AudioClip, MetaEventListener, L
|
|||||||
private Sequencer sequencer = null;
|
private Sequencer sequencer = null;
|
||||||
private Sequence sequence = null;
|
private Sequence sequence = null;
|
||||||
private boolean sequencerloop = false;
|
private boolean sequencerloop = false;
|
||||||
|
private volatile boolean success;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* used for determining how many samples is the
|
* used for determining how many samples is the
|
||||||
@ -91,12 +94,31 @@ public final class JavaSoundAudioClip implements AudioClip, MetaEventListener, L
|
|||||||
//private final static long CLIP_THRESHOLD = 1;
|
//private final static long CLIP_THRESHOLD = 1;
|
||||||
private static final int STREAM_BUFFER_SIZE = 1024;
|
private static final int STREAM_BUFFER_SIZE = 1024;
|
||||||
|
|
||||||
public JavaSoundAudioClip(InputStream in) throws IOException {
|
public static JavaSoundAudioClip create(final URLConnection uc) {
|
||||||
|
JavaSoundAudioClip clip = new JavaSoundAudioClip();
|
||||||
|
try {
|
||||||
|
clip.init(uc.getInputStream());
|
||||||
|
} catch (final Exception ignored) {
|
||||||
|
// AudioClip will be no-op if some exception will occurred
|
||||||
|
}
|
||||||
|
return clip;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JavaSoundAudioClip create(final URL url) {
|
||||||
|
JavaSoundAudioClip clip = new JavaSoundAudioClip();
|
||||||
|
try {
|
||||||
|
clip.init(url.openStream());
|
||||||
|
} catch (final Exception ignored) {
|
||||||
|
// AudioClip will be no-op if some exception will occurred
|
||||||
|
}
|
||||||
|
return clip;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init(InputStream in) throws IOException {
|
||||||
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.<init>");
|
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.<init>");
|
||||||
|
|
||||||
BufferedInputStream bis = new BufferedInputStream(in, STREAM_BUFFER_SIZE);
|
BufferedInputStream bis = new BufferedInputStream(in, STREAM_BUFFER_SIZE);
|
||||||
bis.mark(STREAM_BUFFER_SIZE);
|
bis.mark(STREAM_BUFFER_SIZE);
|
||||||
boolean success = false;
|
|
||||||
try {
|
try {
|
||||||
AudioInputStream as = AudioSystem.getAudioInputStream(bis);
|
AudioInputStream as = AudioSystem.getAudioInputStream(bis);
|
||||||
// load the stream data into memory
|
// load the stream data into memory
|
||||||
@ -120,18 +142,21 @@ public final class JavaSoundAudioClip implements AudioClip, MetaEventListener, L
|
|||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!success) {
|
|
||||||
throw new IOException("Unable to create AudioClip from input stream");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void play() {
|
public synchronized void play() {
|
||||||
|
if (!success) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
startImpl(false);
|
startImpl(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void loop() {
|
public synchronized void loop() {
|
||||||
|
if (!success) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
startImpl(true);
|
startImpl(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,6 +231,9 @@ public final class JavaSoundAudioClip implements AudioClip, MetaEventListener, L
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void stop() {
|
public synchronized void stop() {
|
||||||
|
if (!success) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip->stop()");
|
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip->stop()");
|
||||||
lastPlayCall = 0;
|
lastPlayCall = 0;
|
||||||
|
@ -42,6 +42,8 @@ import javax.accessibility.AccessibleRole;
|
|||||||
import javax.accessibility.AccessibleState;
|
import javax.accessibility.AccessibleState;
|
||||||
import javax.accessibility.AccessibleStateSet;
|
import javax.accessibility.AccessibleStateSet;
|
||||||
|
|
||||||
|
import com.sun.media.sound.JavaSoundAudioClip;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An applet is a small program that is intended not to be run on
|
* An applet is a small program that is intended not to be run on
|
||||||
* its own, but rather to be embedded inside another application.
|
* its own, but rather to be embedded inside another application.
|
||||||
@ -322,7 +324,7 @@ public class Applet extends Panel {
|
|||||||
* @since 1.2
|
* @since 1.2
|
||||||
*/
|
*/
|
||||||
public static final AudioClip newAudioClip(URL url) {
|
public static final AudioClip newAudioClip(URL url) {
|
||||||
return new sun.applet.AppletAudioClip(url);
|
return JavaSoundAudioClip.create(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,152 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 1995, 2003, 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. Oracle designates this
|
|
||||||
* particular file as subject to the "Classpath" exception as provided
|
|
||||||
* by Oracle in the LICENSE file that accompanied this code.
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package sun.applet;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLConnection;
|
|
||||||
import java.applet.AudioClip;
|
|
||||||
|
|
||||||
import com.sun.media.sound.JavaSoundAudioClip;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Applet audio clip;
|
|
||||||
*
|
|
||||||
* @author Arthur van Hoff, Kara Kytle
|
|
||||||
*/
|
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
public class AppletAudioClip implements AudioClip {
|
|
||||||
|
|
||||||
// url that this AudioClip is based on
|
|
||||||
private URL url = null;
|
|
||||||
|
|
||||||
// the audio clip implementation
|
|
||||||
private AudioClip audioClip = null;
|
|
||||||
|
|
||||||
boolean DEBUG = false /*true*/;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs an AppletAudioClip from an URL.
|
|
||||||
*/
|
|
||||||
public AppletAudioClip(URL url) {
|
|
||||||
|
|
||||||
// store the url
|
|
||||||
this.url = url;
|
|
||||||
|
|
||||||
try {
|
|
||||||
// create a stream from the url, and use it
|
|
||||||
// in the clip.
|
|
||||||
InputStream in = url.openStream();
|
|
||||||
createAppletAudioClip(in);
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
/* just quell it */
|
|
||||||
if (DEBUG) {
|
|
||||||
System.err.println("IOException creating AppletAudioClip" + e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs an AppletAudioClip from a URLConnection.
|
|
||||||
*/
|
|
||||||
public AppletAudioClip(URLConnection uc) {
|
|
||||||
|
|
||||||
try {
|
|
||||||
// create a stream from the url, and use it
|
|
||||||
// in the clip.
|
|
||||||
createAppletAudioClip(uc.getInputStream());
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
/* just quell it */
|
|
||||||
if (DEBUG) {
|
|
||||||
System.err.println("IOException creating AppletAudioClip" + e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* For constructing directly from Jar entries, or any other
|
|
||||||
* raw Audio data. Note that the data provided must include the format
|
|
||||||
* header.
|
|
||||||
*/
|
|
||||||
public AppletAudioClip(byte [] data) {
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
// construct a stream from the byte array
|
|
||||||
InputStream in = new ByteArrayInputStream(data);
|
|
||||||
|
|
||||||
createAppletAudioClip(in);
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
/* just quell it */
|
|
||||||
if (DEBUG) {
|
|
||||||
System.err.println("IOException creating AppletAudioClip " + e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Does the real work of creating an AppletAudioClip from an InputStream.
|
|
||||||
* This function is used by both constructors.
|
|
||||||
*/
|
|
||||||
void createAppletAudioClip(InputStream in) throws IOException {
|
|
||||||
|
|
||||||
try {
|
|
||||||
audioClip = new JavaSoundAudioClip(in);
|
|
||||||
} catch (Exception e3) {
|
|
||||||
// no matter what happened, we throw an IOException to avoid changing the interfaces....
|
|
||||||
throw new IOException("Failed to construct the AudioClip: " + e3);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public synchronized void play() {
|
|
||||||
|
|
||||||
if (audioClip != null)
|
|
||||||
audioClip.play();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public synchronized void loop() {
|
|
||||||
|
|
||||||
if (audioClip != null)
|
|
||||||
audioClip.loop();
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void stop() {
|
|
||||||
|
|
||||||
if (audioClip != null)
|
|
||||||
audioClip.stop();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -23,21 +23,21 @@
|
|||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* Basic .aiff audio handler.
|
|
||||||
* @author Jeff Nisewanger
|
|
||||||
*/
|
|
||||||
package sun.awt.www.content.audio;
|
package sun.awt.www.content.audio;
|
||||||
|
|
||||||
import java.net.*;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import sun.applet.AppletAudioClip;
|
import java.net.ContentHandler;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
|
||||||
|
import com.sun.media.sound.JavaSoundAudioClip;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an AppletAudioClip object.
|
* Basic .aiff audio handler returns an JavaSoundAudioClip object.
|
||||||
|
*
|
||||||
|
* @author Jeff Nisewanger
|
||||||
*/
|
*/
|
||||||
public class aiff extends ContentHandler {
|
public class aiff extends ContentHandler {
|
||||||
public Object getContent(URLConnection uc) throws IOException {
|
public Object getContent(URLConnection uc) throws IOException {
|
||||||
return new AppletAudioClip(uc);
|
return JavaSoundAudioClip.create(uc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -23,23 +23,24 @@
|
|||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* Basic .au and .snd audio handler.
|
|
||||||
* @author Jeff Nisewanger
|
|
||||||
*/
|
|
||||||
package sun.awt.www.content.audio;
|
package sun.awt.www.content.audio;
|
||||||
|
|
||||||
import java.net.*;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import sun.applet.AppletAudioClip;
|
import java.net.ContentHandler;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
|
||||||
|
import com.sun.media.sound.JavaSoundAudioClip;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an AppletAudioClip object.
|
* Basic .au and .snd audio handler returns an JavaSoundAudioClip object.
|
||||||
|
* <p>
|
||||||
* This provides backwards compatibility with the behavior
|
* This provides backwards compatibility with the behavior
|
||||||
* of ClassLoader.getResource().getContent() on JDK1.1.
|
* of ClassLoader.getResource().getContent() on JDK1.1.
|
||||||
|
*
|
||||||
|
* @author Jeff Nisewanger
|
||||||
*/
|
*/
|
||||||
public class basic extends ContentHandler {
|
public class basic extends ContentHandler {
|
||||||
public Object getContent(URLConnection uc) throws IOException {
|
public Object getContent(URLConnection uc) throws IOException {
|
||||||
return new AppletAudioClip(uc);
|
return JavaSoundAudioClip.create(uc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -23,21 +23,21 @@
|
|||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* Basic .wav audio handler.
|
|
||||||
* @author Jeff Nisewanger
|
|
||||||
*/
|
|
||||||
package sun.awt.www.content.audio;
|
package sun.awt.www.content.audio;
|
||||||
|
|
||||||
import java.net.*;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import sun.applet.AppletAudioClip;
|
import java.net.ContentHandler;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
|
||||||
|
import com.sun.media.sound.JavaSoundAudioClip;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an AppletAudioClip object.
|
* Basic .wav audio handler returns an JavaSoundAudioClip object.
|
||||||
|
*
|
||||||
|
* @author Jeff Nisewanger
|
||||||
*/
|
*/
|
||||||
public class wav extends ContentHandler {
|
public class wav extends ContentHandler {
|
||||||
public Object getContent(URLConnection uc) throws IOException {
|
public Object getContent(URLConnection uc) throws IOException {
|
||||||
return new AppletAudioClip(uc);
|
return JavaSoundAudioClip.create(uc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -23,21 +23,21 @@
|
|||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* Basic .aiff audio handler.
|
|
||||||
* @author Jeff Nisewanger
|
|
||||||
*/
|
|
||||||
package sun.awt.www.content.audio;
|
package sun.awt.www.content.audio;
|
||||||
|
|
||||||
import java.net.*;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import sun.applet.AppletAudioClip;
|
import java.net.ContentHandler;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
|
||||||
|
import com.sun.media.sound.JavaSoundAudioClip;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an AppletAudioClip object.
|
* Basic .aiff audio handler returns an JavaSoundAudioClip object.
|
||||||
|
*
|
||||||
|
* @author Jeff Nisewanger
|
||||||
*/
|
*/
|
||||||
public class x_aiff extends ContentHandler {
|
public class x_aiff extends ContentHandler {
|
||||||
public Object getContent(URLConnection uc) throws IOException {
|
public Object getContent(URLConnection uc) throws IOException {
|
||||||
return new AppletAudioClip(uc);
|
return JavaSoundAudioClip.create(uc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -23,21 +23,21 @@
|
|||||||
* questions.
|
* questions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* Basic .wav audio handler.
|
|
||||||
* @author Jeff Nisewanger
|
|
||||||
*/
|
|
||||||
package sun.awt.www.content.audio;
|
package sun.awt.www.content.audio;
|
||||||
|
|
||||||
import java.net.*;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import sun.applet.AppletAudioClip;
|
import java.net.ContentHandler;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
|
||||||
|
import com.sun.media.sound.JavaSoundAudioClip;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an AppletAudioClip object.
|
* Basic .wav audio handler returns an JavaSoundAudioClip object.
|
||||||
|
*
|
||||||
|
* @author Jeff Nisewanger
|
||||||
*/
|
*/
|
||||||
public class x_wav extends ContentHandler {
|
public class x_wav extends ContentHandler {
|
||||||
public Object getContent(URLConnection uc) throws IOException {
|
public Object getContent(URLConnection uc) throws IOException {
|
||||||
return new AppletAudioClip(uc);
|
return JavaSoundAudioClip.create(uc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
112
test/jdk/javax/sound/sampled/Clip/AudioContentHandlers.java
Normal file
112
test/jdk/javax/sound/sampled/Clip/AudioContentHandlers.java
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018, 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.applet.AudioClip;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.sound.sampled.AudioFileFormat;
|
||||||
|
import javax.sound.sampled.AudioFormat;
|
||||||
|
import javax.sound.sampled.AudioInputStream;
|
||||||
|
import javax.sound.sampled.AudioSystem;
|
||||||
|
|
||||||
|
import static javax.sound.sampled.AudioFileFormat.Type.AIFC;
|
||||||
|
import static javax.sound.sampled.AudioFileFormat.Type.AIFF;
|
||||||
|
import static javax.sound.sampled.AudioFileFormat.Type.AU;
|
||||||
|
import static javax.sound.sampled.AudioFileFormat.Type.SND;
|
||||||
|
import static javax.sound.sampled.AudioFileFormat.Type.WAVE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @bug 8204454
|
||||||
|
* @summary URL.getContent() should return AudioClip for supported formats
|
||||||
|
* @run main/othervm -mx128m AudioContentHandlers
|
||||||
|
*/
|
||||||
|
public final class AudioContentHandlers {
|
||||||
|
|
||||||
|
private static final List<AudioFormat> formats = new ArrayList<>();
|
||||||
|
|
||||||
|
private static final AudioFormat.Encoding[] encodings =
|
||||||
|
{AudioFormat.Encoding.ALAW, AudioFormat.Encoding.ULAW,
|
||||||
|
AudioFormat.Encoding.PCM_SIGNED,
|
||||||
|
AudioFormat.Encoding.PCM_UNSIGNED,
|
||||||
|
AudioFormat.Encoding.PCM_FLOAT};
|
||||||
|
|
||||||
|
private static final AudioFileFormat.Type[] types =
|
||||||
|
{WAVE, AU, AIFF, AIFC, SND};
|
||||||
|
|
||||||
|
static {
|
||||||
|
for (final AudioFormat.Encoding enc : encodings) {
|
||||||
|
formats.add(new AudioFormat(enc, 44100, 8, 1, 1, 44100, true));
|
||||||
|
formats.add(new AudioFormat(enc, 44100, 8, 1, 1, 44100, false));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(final String[] args) throws Exception {
|
||||||
|
for (final AudioFileFormat.Type type : types) {
|
||||||
|
for (final AudioFormat format : formats) {
|
||||||
|
File file = new File("audio." + type.getExtension());
|
||||||
|
try {
|
||||||
|
AudioSystem.write(getStream(format), type, file);
|
||||||
|
} catch (IOException | IllegalArgumentException ignored) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
AudioClip content;
|
||||||
|
try {
|
||||||
|
content = (AudioClip) file.toURL().getContent();
|
||||||
|
// We need to generate OOM because the stream in AudioClip
|
||||||
|
// will be closed in finalize().
|
||||||
|
generateOOME();
|
||||||
|
} finally {
|
||||||
|
Files.delete(file.toPath());
|
||||||
|
}
|
||||||
|
if (content == null) {
|
||||||
|
throw new RuntimeException("Content is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static AudioInputStream getStream(final AudioFormat format) {
|
||||||
|
final InputStream in = new ByteArrayInputStream(new byte[100]);
|
||||||
|
return new AudioInputStream(in, format, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void generateOOME() throws Exception {
|
||||||
|
List<Object> leak = new LinkedList<>();
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
leak.add(new byte[1024 * 1024]);
|
||||||
|
}
|
||||||
|
} catch (OutOfMemoryError ignored) {
|
||||||
|
}
|
||||||
|
// Give the GC a chance at that weakref in case of slow systems
|
||||||
|
Thread.sleep(2000);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user