--- a/src/java.desktop/share/classes/com/sun/media/sound/JavaSoundAudioClip.java Tue Jun 12 12:18:57 2018 -0700
+++ b/src/java.desktop/share/classes/com/sun/media/sound/JavaSoundAudioClip.java Tue Jun 12 14:53:57 2018 -0700
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MetaEventListener;
@@ -76,6 +78,7 @@
private Sequencer sequencer = null;
private Sequence sequence = null;
private boolean sequencerloop = false;
+ private volatile boolean success;
/**
* used for determining how many samples is the
@@ -91,12 +94,31 @@
//private final static long CLIP_THRESHOLD = 1;
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>");
BufferedInputStream bis = new BufferedInputStream(in, STREAM_BUFFER_SIZE);
bis.mark(STREAM_BUFFER_SIZE);
- boolean success = false;
try {
AudioInputStream as = AudioSystem.getAudioInputStream(bis);
// load the stream data into memory
@@ -120,18 +142,21 @@
success = false;
}
}
- if (!success) {
- throw new IOException("Unable to create AudioClip from input stream");
- }
}
@Override
public synchronized void play() {
+ if (!success) {
+ return;
+ }
startImpl(false);
}
@Override
public synchronized void loop() {
+ if (!success) {
+ return;
+ }
startImpl(true);
}
@@ -206,6 +231,9 @@
@Override
public synchronized void stop() {
+ if (!success) {
+ return;
+ }
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip->stop()");
lastPlayCall = 0;
--- a/src/java.desktop/share/classes/java/applet/Applet.java Tue Jun 12 12:18:57 2018 -0700
+++ b/src/java.desktop/share/classes/java/applet/Applet.java Tue Jun 12 14:53:57 2018 -0700
@@ -42,6 +42,8 @@
import javax.accessibility.AccessibleState;
import javax.accessibility.AccessibleStateSet;
+import com.sun.media.sound.JavaSoundAudioClip;
+
/**
* An applet is a small program that is intended not to be run on
* its own, but rather to be embedded inside another application.
@@ -322,7 +324,7 @@
* @since 1.2
*/
public static final AudioClip newAudioClip(URL url) {
- return new sun.applet.AppletAudioClip(url);
+ return JavaSoundAudioClip.create(url);
}
/**
--- a/src/java.desktop/share/classes/sun/applet/AppletAudioClip.java Tue Jun 12 12:18:57 2018 -0700
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -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();
- }
-}
--- a/src/java.desktop/share/classes/sun/awt/www/content/audio/aiff.java Tue Jun 12 12:18:57 2018 -0700
+++ b/src/java.desktop/share/classes/sun/awt/www/content/audio/aiff.java Tue Jun 12 14:53:57 2018 -0700
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@@ -23,21 +23,21 @@
* questions.
*/
-/**
- * Basic .aiff audio handler.
- * @author Jeff Nisewanger
- */
package sun.awt.www.content.audio;
-import java.net.*;
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 Object getContent(URLConnection uc) throws IOException {
- return new AppletAudioClip(uc);
+ return JavaSoundAudioClip.create(uc);
}
}
--- a/src/java.desktop/share/classes/sun/awt/www/content/audio/basic.java Tue Jun 12 12:18:57 2018 -0700
+++ b/src/java.desktop/share/classes/sun/awt/www/content/audio/basic.java Tue Jun 12 14:53:57 2018 -0700
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@@ -23,23 +23,24 @@
* questions.
*/
-/**
- * Basic .au and .snd audio handler.
- * @author Jeff Nisewanger
- */
package sun.awt.www.content.audio;
-import java.net.*;
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
* of ClassLoader.getResource().getContent() on JDK1.1.
+ *
+ * @author Jeff Nisewanger
*/
public class basic extends ContentHandler {
public Object getContent(URLConnection uc) throws IOException {
- return new AppletAudioClip(uc);
+ return JavaSoundAudioClip.create(uc);
}
}
--- a/src/java.desktop/share/classes/sun/awt/www/content/audio/wav.java Tue Jun 12 12:18:57 2018 -0700
+++ b/src/java.desktop/share/classes/sun/awt/www/content/audio/wav.java Tue Jun 12 14:53:57 2018 -0700
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@@ -23,21 +23,21 @@
* questions.
*/
-/**
- * Basic .wav audio handler.
- * @author Jeff Nisewanger
- */
package sun.awt.www.content.audio;
-import java.net.*;
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 Object getContent(URLConnection uc) throws IOException {
- return new AppletAudioClip(uc);
+ return JavaSoundAudioClip.create(uc);
}
}
--- a/src/java.desktop/share/classes/sun/awt/www/content/audio/x_aiff.java Tue Jun 12 12:18:57 2018 -0700
+++ b/src/java.desktop/share/classes/sun/awt/www/content/audio/x_aiff.java Tue Jun 12 14:53:57 2018 -0700
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@@ -23,21 +23,21 @@
* questions.
*/
-/**
- * Basic .aiff audio handler.
- * @author Jeff Nisewanger
- */
package sun.awt.www.content.audio;
-import java.net.*;
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 Object getContent(URLConnection uc) throws IOException {
- return new AppletAudioClip(uc);
+ return JavaSoundAudioClip.create(uc);
}
}
--- a/src/java.desktop/share/classes/sun/awt/www/content/audio/x_wav.java Tue Jun 12 12:18:57 2018 -0700
+++ b/src/java.desktop/share/classes/sun/awt/www/content/audio/x_wav.java Tue Jun 12 14:53:57 2018 -0700
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@@ -23,21 +23,21 @@
* questions.
*/
-/**
- * Basic .wav audio handler.
- * @author Jeff Nisewanger
- */
package sun.awt.www.content.audio;
-import java.net.*;
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 Object getContent(URLConnection uc) throws IOException {
- return new AppletAudioClip(uc);
+ return JavaSoundAudioClip.create(uc);
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/sound/sampled/Clip/AudioContentHandlers.java Tue Jun 12 14:53:57 2018 -0700
@@ -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);
+ }
+}