diff --git a/jdk/src/java.desktop/share/classes/com/sun/media/sound/AiffFileWriter.java b/jdk/src/java.desktop/share/classes/com/sun/media/sound/AiffFileWriter.java index b31871f3bb5..5e9e3c45cce 100644 --- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/AiffFileWriter.java +++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/AiffFileWriter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2015, 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 @@ -37,6 +37,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.RandomAccessFile; import java.io.SequenceInputStream; +import java.util.Objects; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioInputStream; @@ -84,6 +85,9 @@ public final class AiffFileWriter extends SunFileWriter { public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException { + Objects.requireNonNull(stream); + Objects.requireNonNull(fileType); + Objects.requireNonNull(out); //$$fb the following check must come first ! Otherwise // the next frame length check may throw an IOException and @@ -103,6 +107,9 @@ public final class AiffFileWriter extends SunFileWriter { public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException { + Objects.requireNonNull(stream); + Objects.requireNonNull(fileType); + Objects.requireNonNull(out); // throws IllegalArgumentException if not supported AiffFileFormat aiffFileFormat = (AiffFileFormat)getAudioFileFormat(fileType, stream); diff --git a/jdk/src/java.desktop/share/classes/com/sun/media/sound/AlawCodec.java b/jdk/src/java.desktop/share/classes/com/sun/media/sound/AlawCodec.java index fbe63f6ff16..fcc0e709f2b 100644 --- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/AlawCodec.java +++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/AlawCodec.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2015, 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 @@ -26,6 +26,7 @@ package com.sun.media.sound; import java.io.IOException; +import java.util.Objects; import java.util.Vector; import javax.sound.sampled.AudioFormat; @@ -119,6 +120,7 @@ public final class AlawCodec extends SunCodec { /** */ public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){ + Objects.requireNonNull(sourceFormat); if( (targetEncoding.equals( AudioFormat.Encoding.PCM_SIGNED ) && sourceFormat.getEncoding().equals( AudioFormat.Encoding.ALAW)) || (targetEncoding.equals( AudioFormat.Encoding.ALAW) && sourceFormat.getEncoding().equals( AudioFormat.Encoding.PCM_SIGNED)) ) { return getOutputFormats( sourceFormat ); diff --git a/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileWriter.java b/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileWriter.java index 3020c348953..1c2ccf5b74c 100644 --- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileWriter.java +++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileWriter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2015, 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 @@ -37,6 +37,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.RandomAccessFile; import java.io.SequenceInputStream; +import java.util.Objects; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioInputStream; @@ -83,6 +84,9 @@ public final class AuFileWriter extends SunFileWriter { public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException { + Objects.requireNonNull(stream); + Objects.requireNonNull(fileType); + Objects.requireNonNull(out); // we must know the total data length to calculate the file length //$$fb 2001-07-13: fix for bug 4351296: do not throw an exception @@ -100,6 +104,9 @@ public final class AuFileWriter extends SunFileWriter { public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException { + Objects.requireNonNull(stream); + Objects.requireNonNull(fileType); + Objects.requireNonNull(out); // throws IllegalArgumentException if not supported AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream); diff --git a/jdk/src/java.desktop/share/classes/com/sun/media/sound/AudioFloatFormatConverter.java b/jdk/src/java.desktop/share/classes/com/sun/media/sound/AudioFloatFormatConverter.java index a161fc5db42..a1e522afb40 100644 --- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/AudioFloatFormatConverter.java +++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/AudioFloatFormatConverter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2015, 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 @@ -28,6 +28,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; +import java.util.Objects; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; @@ -538,6 +539,7 @@ public final class AudioFloatFormatConverter extends FormatConversionProvider { public AudioFormat[] getTargetFormats(Encoding targetEncoding, AudioFormat sourceFormat) { + Objects.requireNonNull(targetEncoding); if (AudioFloatConverter.getConverter(sourceFormat) == null) return new AudioFormat[0]; int channels = sourceFormat.getChannels(); @@ -592,6 +594,7 @@ public final class AudioFloatFormatConverter extends FormatConversionProvider { public boolean isConversionSupported(AudioFormat targetFormat, AudioFormat sourceFormat) { + Objects.requireNonNull(targetFormat); if (AudioFloatConverter.getConverter(sourceFormat) == null) return false; if (AudioFloatConverter.getConverter(targetFormat) == null) @@ -605,6 +608,7 @@ public final class AudioFloatFormatConverter extends FormatConversionProvider { public boolean isConversionSupported(Encoding targetEncoding, AudioFormat sourceFormat) { + Objects.requireNonNull(targetEncoding); if (AudioFloatConverter.getConverter(sourceFormat) == null) return false; for (int i = 0; i < formats.length; i++) { diff --git a/jdk/src/java.desktop/share/classes/com/sun/media/sound/DirectAudioDeviceProvider.java b/jdk/src/java.desktop/share/classes/com/sun/media/sound/DirectAudioDeviceProvider.java index c306dc78c66..274571917ea 100644 --- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/DirectAudioDeviceProvider.java +++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/DirectAudioDeviceProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2015, 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 @@ -121,7 +121,8 @@ public final class DirectAudioDeviceProvider extends MixerProvider { } } } - throw new IllegalArgumentException("Mixer " + info.toString() + " not supported by this provider."); + throw new IllegalArgumentException( + String.format("Mixer %s not supported by this provider", info)); } diff --git a/jdk/src/java.desktop/share/classes/com/sun/media/sound/PCMtoPCMCodec.java b/jdk/src/java.desktop/share/classes/com/sun/media/sound/PCMtoPCMCodec.java index dbed920e6fa..6ef8268f34c 100644 --- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/PCMtoPCMCodec.java +++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/PCMtoPCMCodec.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2015, 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 @@ -26,6 +26,7 @@ package com.sun.media.sound; import java.io.IOException; +import java.util.Objects; import java.util.Vector; import javax.sound.sampled.AudioFormat; @@ -87,6 +88,7 @@ public final class PCMtoPCMCodec extends SunCodec { /** */ public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){ + Objects.requireNonNull(targetEncoding); // filter out targetEncoding from the old getOutputFormats( sourceFormat ) method diff --git a/jdk/src/java.desktop/share/classes/com/sun/media/sound/PortMixerProvider.java b/jdk/src/java.desktop/share/classes/com/sun/media/sound/PortMixerProvider.java index a8ef40f8a0f..ce7f3596a48 100644 --- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/PortMixerProvider.java +++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/PortMixerProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2015, 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 @@ -101,7 +101,6 @@ public final class PortMixerProvider extends MixerProvider { } } - public Mixer getMixer(Mixer.Info info) { synchronized (PortMixerProvider.class) { for (int i = 0; i < infos.length; i++) { @@ -110,8 +109,8 @@ public final class PortMixerProvider extends MixerProvider { } } } - throw new IllegalArgumentException("Mixer " + info.toString() - + " not supported by this provider."); + throw new IllegalArgumentException( + String.format("Mixer %s not supported by this provider", info)); } diff --git a/jdk/src/java.desktop/share/classes/com/sun/media/sound/UlawCodec.java b/jdk/src/java.desktop/share/classes/com/sun/media/sound/UlawCodec.java index 5dde45b734b..dc088ee459c 100644 --- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/UlawCodec.java +++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/UlawCodec.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2015, 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 @@ -27,6 +27,7 @@ package com.sun.media.sound; import java.io.IOException; +import java.util.Objects; import java.util.Vector; import javax.sound.sampled.AudioFormat; @@ -106,6 +107,8 @@ public final class UlawCodec extends SunCodec { /** */ public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){ + Objects.requireNonNull(targetEncoding); + Objects.requireNonNull(sourceFormat); if( (AudioFormat.Encoding.PCM_SIGNED.equals(targetEncoding) && AudioFormat.Encoding.ULAW.equals(sourceFormat.getEncoding())) || diff --git a/jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFileWriter.java b/jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFileWriter.java index b9becd23d5a..199364c94f4 100644 --- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFileWriter.java +++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/WaveFileWriter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2015, 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 @@ -37,6 +37,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.RandomAccessFile; import java.io.SequenceInputStream; +import java.util.Objects; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioInputStream; @@ -106,6 +107,9 @@ public final class WaveFileWriter extends SunFileWriter { public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException { + Objects.requireNonNull(stream); + Objects.requireNonNull(fileType); + Objects.requireNonNull(out); //$$fb the following check must come first ! Otherwise // the next frame length check may throw an IOException and @@ -127,6 +131,9 @@ public final class WaveFileWriter extends SunFileWriter { public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException { + Objects.requireNonNull(stream); + Objects.requireNonNull(fileType); + Objects.requireNonNull(out); // throws IllegalArgumentException if not supported WaveFileFormat waveFileFormat = (WaveFileFormat)getAudioFileFormat(fileType, stream); diff --git a/jdk/src/java.desktop/share/classes/javax/sound/sampled/AudioSystem.java b/jdk/src/java.desktop/share/classes/javax/sound/sampled/AudioSystem.java index bb5e72666ba..2a6d98dd158 100644 --- a/jdk/src/java.desktop/share/classes/javax/sound/sampled/AudioSystem.java +++ b/jdk/src/java.desktop/share/classes/javax/sound/sampled/AudioSystem.java @@ -33,6 +33,7 @@ import java.net.URL; import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Objects; import java.util.Properties; import java.util.Set; import java.util.Vector; @@ -191,50 +192,21 @@ public class AudioSystem { * mixer installed on the system * @see #getMixerInfo */ - public static Mixer getMixer(Mixer.Info info) { - - Mixer mixer = null; - List providers = getMixerProviders(); - - for(int i = 0; i < providers.size(); i++ ) { - + public static Mixer getMixer(final Mixer.Info info) { + for (final MixerProvider provider : getMixerProviders()) { try { - return providers.get(i).getMixer(info); - - } catch (IllegalArgumentException e) { - } catch (NullPointerException e) { - // $$jb 08.20.99: If the strings in the info object aren't - // set, then Netscape (using jdk1.1.5) tends to throw - // NPE's when doing some string manipulation. This is - // probably not the best fix, but is solves the problem - // of the NPE in Netscape using local classes - // $$jb 11.01.99: Replacing this patch. + return provider.getMixer(info); + } catch (IllegalArgumentException | NullPointerException ignored) { + // The MixerProvider.getMixer(null) should return default Mixer, + // This behaviour was assumed from the beginning, but strictly + // specified only in the jdk9. Since the jdk1.1.5 we skipped + // NPE for some reason and therefore skipped some + // implementations of MixerProviders, which throw NPE. To keep + // support of such implementations, we still ignore NPE. } } - - //$$fb if looking for default mixer, and not found yet, add a round of looking - if (info == null) { - for(int i = 0; i < providers.size(); i++ ) { - try { - MixerProvider provider = providers.get(i); - Mixer.Info[] infos = provider.getMixerInfo(); - // start from 0 to last device (do not reverse this order) - for (int ii = 0; ii < infos.length; ii++) { - try { - return provider.getMixer(infos[ii]); - } catch (IllegalArgumentException e) { - // this is not a good default device :) - } - } - } catch (IllegalArgumentException e) { - } catch (NullPointerException e) { - } - } - } - - - throw new IllegalArgumentException("Mixer not supported: " - + (info!=null?info.toString():"null")); + throw new IllegalArgumentException( + String.format("Mixer not supported: %s", info)); } //$$fb 2002-11-26: fix for 4757930: DOC: AudioSystem.getTarget/SourceLineInfo() is ambiguous @@ -696,8 +668,10 @@ public class AudioSystem { * array of length 0 is returned. Otherwise, the array will have a * length of at least 1, representing {@code sourceEncoding} * (no conversion). + * @throws NullPointerException if {@code sourceEncoding} is {@code null} */ public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat.Encoding sourceEncoding) { + Objects.requireNonNull(sourceEncoding); List codecs = getFormatConversionProviders(); Vector encodings = new Vector<>(); @@ -730,9 +704,10 @@ public class AudioSystem { * array of length 0 is returned. Otherwise, the array will have a * length of at least 1, representing the encoding of * {@code sourceFormat} (no conversion). + * @throws NullPointerException if {@code sourceFormat} is {@code null} */ public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat) { - + Objects.requireNonNull(sourceFormat); List codecs = getFormatConversionProviders(); Vector encodings = new Vector<>(); @@ -769,9 +744,12 @@ public class AudioSystem { * @param sourceFormat the audio format before conversion * @return {@code true} if the conversion is supported, otherwise * {@code false} + * @throws NullPointerException if {@code targetEncoding} or + * {@code sourceFormat} are {@code null} */ public static boolean isConversionSupported(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat) { - + Objects.requireNonNull(targetEncoding); + Objects.requireNonNull(sourceFormat); List codecs = getFormatConversionProviders(); @@ -792,6 +770,8 @@ public class AudioSystem { * @param sourceStream the stream to be converted * @return an audio input stream of the indicated encoding * @throws IllegalArgumentException if the conversion is not supported + * @throws NullPointerException if {@code targetEncoding} or + * {@code sourceStream} are {@code null} * @see #getTargetEncodings(AudioFormat.Encoding) * @see #getTargetEncodings(AudioFormat) * @see #isConversionSupported(AudioFormat.Encoding, AudioFormat) @@ -799,6 +779,8 @@ public class AudioSystem { */ public static AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream) { + Objects.requireNonNull(targetEncoding); + Objects.requireNonNull(sourceStream); List codecs = getFormatConversionProviders(); @@ -821,8 +803,12 @@ public class AudioSystem { * @param sourceFormat the audio format before conversion * @return array of formats. If no formats of the specified encoding are * supported, an array of length 0 is returned. + * @throws NullPointerException if {@code targetEncoding} or + * {@code sourceFormat} are {@code null} */ public static AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat) { + Objects.requireNonNull(targetEncoding); + Objects.requireNonNull(sourceFormat); List codecs = getFormatConversionProviders(); Vector formats = new Vector<>(); @@ -860,8 +846,12 @@ public class AudioSystem { * @param sourceFormat the audio format before conversion * @return {@code true} if the conversion is supported, otherwise * {@code false} + * @throws NullPointerException if {@code targetFormat} or + * {@code sourceFormat} are {@code null} */ public static boolean isConversionSupported(AudioFormat targetFormat, AudioFormat sourceFormat) { + Objects.requireNonNull(targetFormat); + Objects.requireNonNull(sourceFormat); List codecs = getFormatConversionProviders(); @@ -882,6 +872,8 @@ public class AudioSystem { * @param sourceStream the stream to be converted * @return an audio input stream of the indicated format * @throws IllegalArgumentException if the conversion is not supported + * @throws NullPointerException if {@code targetFormat} or + * {@code sourceStream} are {@code null} * @see #getTargetEncodings(AudioFormat) * @see #getTargetFormats(AudioFormat.Encoding, AudioFormat) * @see #isConversionSupported(AudioFormat, AudioFormat) @@ -889,7 +881,6 @@ public class AudioSystem { */ public static AudioInputStream getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream) { - if (sourceStream.getFormat().matches(targetFormat)) { return sourceStream; } @@ -924,11 +915,13 @@ public class AudioSystem { * @throws UnsupportedAudioFileException if the stream does not point to * valid audio file data recognized by the system * @throws IOException if an input/output exception occurs + * @throws NullPointerException if {@code stream} is {@code null} * @see InputStream#markSupported * @see InputStream#mark */ public static AudioFileFormat getAudioFileFormat(InputStream stream) - throws UnsupportedAudioFileException, IOException { + throws UnsupportedAudioFileException, IOException { + Objects.requireNonNull(stream); List providers = getAudioFileReaders(); AudioFileFormat format = null; @@ -961,9 +954,11 @@ public class AudioSystem { * @throws UnsupportedAudioFileException if the URL does not point to valid * audio file data recognized by the system * @throws IOException if an input/output exception occurs + * @throws NullPointerException if {@code url} is {@code null} */ public static AudioFileFormat getAudioFileFormat(URL url) - throws UnsupportedAudioFileException, IOException { + throws UnsupportedAudioFileException, IOException { + Objects.requireNonNull(url); List providers = getAudioFileReaders(); AudioFileFormat format = null; @@ -996,9 +991,11 @@ public class AudioSystem { * @throws UnsupportedAudioFileException if the {@code File} does not point * to valid audio file data recognized by the system * @throws IOException if an I/O exception occurs + * @throws NullPointerException if {@code file} is {@code null} */ public static AudioFileFormat getAudioFileFormat(File file) - throws UnsupportedAudioFileException, IOException { + throws UnsupportedAudioFileException, IOException { + Objects.requireNonNull(file); List providers = getAudioFileReaders(); AudioFileFormat format = null; @@ -1037,11 +1034,13 @@ public class AudioSystem { * @throws UnsupportedAudioFileException if the stream does not point to * valid audio file data recognized by the system * @throws IOException if an I/O exception occurs + * @throws NullPointerException if {@code stream} is {@code null} * @see InputStream#markSupported * @see InputStream#mark */ public static AudioInputStream getAudioInputStream(InputStream stream) - throws UnsupportedAudioFileException, IOException { + throws UnsupportedAudioFileException, IOException { + Objects.requireNonNull(stream); List providers = getAudioFileReaders(); AudioInputStream audioStream = null; @@ -1074,9 +1073,11 @@ public class AudioSystem { * @throws UnsupportedAudioFileException if the URL does not point to valid * audio file data recognized by the system * @throws IOException if an I/O exception occurs + * @throws NullPointerException if {@code url} is {@code null} */ public static AudioInputStream getAudioInputStream(URL url) - throws UnsupportedAudioFileException, IOException { + throws UnsupportedAudioFileException, IOException { + Objects.requireNonNull(url); List providers = getAudioFileReaders(); AudioInputStream audioStream = null; @@ -1109,9 +1110,11 @@ public class AudioSystem { * @throws UnsupportedAudioFileException if the {@code File} does not point * to valid audio file data recognized by the system * @throws IOException if an I/O exception occurs + * @throws NullPointerException if {@code file} is {@code null} */ public static AudioInputStream getAudioInputStream(File file) - throws UnsupportedAudioFileException, IOException { + throws UnsupportedAudioFileException, IOException { + Objects.requireNonNull(file); List providers = getAudioFileReaders(); AudioInputStream audioStream = null; @@ -1163,9 +1166,10 @@ public class AudioSystem { * @param fileType the file type for which write capabilities are queried * @return {@code true} if the file type is supported, otherwise * {@code false} + * @throws NullPointerException if {@code fileType} is {@code null} */ public static boolean isFileTypeSupported(AudioFileFormat.Type fileType) { - + Objects.requireNonNull(fileType); List providers = getAudioFileWriters(); for(int i=0; i < providers.size(); i++) { @@ -1185,8 +1189,10 @@ public class AudioSystem { * support is queried * @return array of file types. If no file types are supported, an array of * length 0 is returned. + * @throws NullPointerException if {@code stream} is {@code null} */ public static AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) { + Objects.requireNonNull(stream); List providers = getAudioFileWriters(); Set returnTypesSet = new HashSet<>(); @@ -1210,10 +1216,13 @@ public class AudioSystem { * @param stream the stream for which file-writing support is queried * @return {@code true} if the file type is supported for this audio input * stream, otherwise {@code false} + * @throws NullPointerException if {@code fileType} or {@code stream} are + * {@code null} */ public static boolean isFileTypeSupported(AudioFileFormat.Type fileType, AudioInputStream stream) { - + Objects.requireNonNull(fileType); + Objects.requireNonNull(stream); List providers = getAudioFileWriters(); for(int i=0; i < providers.size(); i++) { @@ -1241,11 +1250,16 @@ public class AudioSystem { * @throws IOException if an input/output exception occurs * @throws IllegalArgumentException if the file type is not supported by the * system + * @throws NullPointerException if {@code stream} or {@code fileType} or + * {@code out} are {@code null} * @see #isFileTypeSupported * @see #getAudioFileTypes */ public static int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException { + Objects.requireNonNull(stream); + Objects.requireNonNull(fileType); + Objects.requireNonNull(out); List providers = getAudioFileWriters(); int bytesWritten = 0; @@ -1281,11 +1295,16 @@ public class AudioSystem { * @throws IOException if an I/O exception occurs * @throws IllegalArgumentException if the file type is not supported by the * system + * @throws NullPointerException if {@code stream} or {@code fileType} or + * {@code out} are {@code null} * @see #isFileTypeSupported * @see #getAudioFileTypes */ public static int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException { + Objects.requireNonNull(stream); + Objects.requireNonNull(fileType); + Objects.requireNonNull(out); List providers = getAudioFileWriters(); int bytesWritten = 0; diff --git a/jdk/src/java.desktop/share/classes/javax/sound/sampled/spi/AudioFileReader.java b/jdk/src/java.desktop/share/classes/javax/sound/sampled/spi/AudioFileReader.java index 7df8f998bc9..f130dba477f 100644 --- a/jdk/src/java.desktop/share/classes/javax/sound/sampled/spi/AudioFileReader.java +++ b/jdk/src/java.desktop/share/classes/javax/sound/sampled/spi/AudioFileReader.java @@ -60,6 +60,7 @@ public abstract class AudioFileReader { * @throws UnsupportedAudioFileException if the stream does not point to * valid audio file data recognized by the system * @throws IOException if an I/O exception occurs + * @throws NullPointerException if {@code stream} is {@code null} * @see InputStream#markSupported * @see InputStream#mark */ @@ -77,6 +78,7 @@ public abstract class AudioFileReader { * @throws UnsupportedAudioFileException if the URL does not point to valid * audio file data recognized by the system * @throws IOException if an I/O exception occurs + * @throws NullPointerException if {@code url} is {@code null} */ public abstract AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException; @@ -92,6 +94,7 @@ public abstract class AudioFileReader { * @throws UnsupportedAudioFileException if the {@code File} does not point * to valid audio file data recognized by the system * @throws IOException if an I/O exception occurs + * @throws NullPointerException if {@code file} is {@code null} */ public abstract AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException; @@ -112,6 +115,7 @@ public abstract class AudioFileReader { * @throws UnsupportedAudioFileException if the stream does not point to * valid audio file data recognized by the system * @throws IOException if an I/O exception occurs + * @throws NullPointerException if {@code stream} is {@code null} * @see InputStream#markSupported * @see InputStream#mark */ @@ -129,6 +133,7 @@ public abstract class AudioFileReader { * @throws UnsupportedAudioFileException if the URL does not point to valid * audio file data recognized by the system * @throws IOException if an I/O exception occurs + * @throws NullPointerException if {@code url} is {@code null} */ public abstract AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException; @@ -144,6 +149,7 @@ public abstract class AudioFileReader { * @throws UnsupportedAudioFileException if the {@code File} does not point * to valid audio file data recognized by the system * @throws IOException if an I/O exception occurs + * @throws NullPointerException if {@code file} is {@code null} */ public abstract AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException; diff --git a/jdk/src/java.desktop/share/classes/javax/sound/sampled/spi/AudioFileWriter.java b/jdk/src/java.desktop/share/classes/javax/sound/sampled/spi/AudioFileWriter.java index 03a44bbc084..7ce970b2508 100644 --- a/jdk/src/java.desktop/share/classes/javax/sound/sampled/spi/AudioFileWriter.java +++ b/jdk/src/java.desktop/share/classes/javax/sound/sampled/spi/AudioFileWriter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2015, 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 @@ -28,6 +28,7 @@ package javax.sound.sampled.spi; import java.io.File; import java.io.IOException; import java.io.OutputStream; +import java.util.Objects; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; @@ -60,8 +61,10 @@ public abstract class AudioFileWriter { * @param fileType the file type for which write capabilities are queried * @return {@code true} if the file type is supported, otherwise * {@code false} + * @throws NullPointerException if {@code fileType} is {@code null} */ public boolean isFileTypeSupported(Type fileType) { + Objects.requireNonNull(fileType); Type types[] = getAudioFileTypes(); @@ -81,6 +84,7 @@ public abstract class AudioFileWriter { * is queried * @return array of file types. If no file types are supported, an array of * length 0 is returned. + * @throws NullPointerException if {@code stream} is {@code null} */ public abstract Type[] getAudioFileTypes(AudioInputStream stream); @@ -92,9 +96,11 @@ public abstract class AudioFileWriter { * @param stream for which file writing support is queried * @return {@code true} if the file type is supported for this audio input * stream, otherwise {@code false} + * @throws NullPointerException if {@code fileType} or {@code stream} are + * {@code null} */ public boolean isFileTypeSupported(Type fileType, AudioInputStream stream) { - + Objects.requireNonNull(fileType); Type types[] = getAudioFileTypes( stream ); for(int i=0; i * The full set of the mixer info objects that represent the mixers * supported by this {@code MixerProvider} may be obtained through the * {@code getMixerInfo} method. Use the {@code isMixerSupported} method to * test whether this {@code MixerProvider} supports a particular mixer. * - * @param info an info object that describes the desired mixer + * @param info an info object that describes the desired mixer, or + * {@code null} for the default mixer * @return mixer instance * @throws IllegalArgumentException if the info object specified does not - * match the info object for a mixer supported by this MixerProvider + * match the info object for a mixer supported by this + * {@code MixerProvider}, or if this {@code MixerProvider} does not + * have default mixer, but default mixer has been requested * @see #getMixerInfo() * @see #isMixerSupported(Mixer.Info) */ diff --git a/jdk/test/javax/sound/sampled/FileReader/AudioFileClose.java b/jdk/test/javax/sound/sampled/spi/AudioFileReader/AudioFileClose.java similarity index 100% rename from jdk/test/javax/sound/sampled/FileReader/AudioFileClose.java rename to jdk/test/javax/sound/sampled/spi/AudioFileReader/AudioFileClose.java diff --git a/jdk/test/javax/sound/sampled/spi/AudioFileReader/ExpectedNPEOnNull.java b/jdk/test/javax/sound/sampled/spi/AudioFileReader/ExpectedNPEOnNull.java new file mode 100644 index 00000000000..c0a89c926c1 --- /dev/null +++ b/jdk/test/javax/sound/sampled/spi/AudioFileReader/ExpectedNPEOnNull.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2015, 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.io.File; +import java.io.InputStream; +import java.net.URL; + +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.spi.AudioFileReader; + +import static java.util.ServiceLoader.load; + +/** + * @test + * @bug 8135100 + * @author Sergey Bylokhov + */ +public final class ExpectedNPEOnNull { + + public static void main(final String[] args) throws Exception { + testAS(); + testAFR(); + } + + /** + * Tests the part of AudioSystem API, which implemented via AudioFileReader. + */ + private static void testAS() throws Exception { + + try { + AudioSystem.getAudioFileFormat((InputStream) null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + try { + AudioSystem.getAudioFileFormat((URL) null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + try { + AudioSystem.getAudioFileFormat((File) null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + try { + AudioSystem.getAudioInputStream((InputStream) null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + try { + AudioSystem.getAudioInputStream((URL) null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + try { + AudioSystem.getAudioInputStream((File) null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + + /** + * Tests the AudioFileReader API directly. + */ + private static void testAFR() throws Exception { + + for (final AudioFileReader afr : load(AudioFileReader.class)) { + try { + afr.getAudioFileFormat((InputStream) null); + throw new RuntimeException("NPE is expected: " + afr); + } catch (final NullPointerException ignored) { + } + try { + afr.getAudioFileFormat((URL) null); + throw new RuntimeException("NPE is expected: " + afr); + } catch (final NullPointerException ignored) { + } + try { + afr.getAudioFileFormat((File) null); + throw new RuntimeException("NPE is expected: " + afr); + } catch (final NullPointerException ignored) { + } + try { + afr.getAudioInputStream((InputStream) null); + throw new RuntimeException("NPE is expected: " + afr); + } catch (final NullPointerException ignored) { + } + try { + afr.getAudioInputStream((URL) null); + throw new RuntimeException("NPE is expected: " + afr); + } catch (final NullPointerException ignored) { + } + try { + afr.getAudioInputStream((File) null); + throw new RuntimeException("NPE is expected: " + afr); + } catch (final NullPointerException ignored) { + } + } + } +} diff --git a/jdk/test/javax/sound/sampled/FileReader/ReadersExceptions.java b/jdk/test/javax/sound/sampled/spi/AudioFileReader/ReadersExceptions.java similarity index 100% rename from jdk/test/javax/sound/sampled/FileReader/ReadersExceptions.java rename to jdk/test/javax/sound/sampled/spi/AudioFileReader/ReadersExceptions.java diff --git a/jdk/test/javax/sound/sampled/FileReader/RepeatedFormatReader.java b/jdk/test/javax/sound/sampled/spi/AudioFileReader/RepeatedFormatReader.java similarity index 100% rename from jdk/test/javax/sound/sampled/FileReader/RepeatedFormatReader.java rename to jdk/test/javax/sound/sampled/spi/AudioFileReader/RepeatedFormatReader.java diff --git a/jdk/test/javax/sound/sampled/FileWriter/AlawEncoderSync.java b/jdk/test/javax/sound/sampled/spi/AudioFileWriter/AlawEncoderSync.java similarity index 100% rename from jdk/test/javax/sound/sampled/FileWriter/AlawEncoderSync.java rename to jdk/test/javax/sound/sampled/spi/AudioFileWriter/AlawEncoderSync.java diff --git a/jdk/test/javax/sound/sampled/spi/AudioFileWriter/ExpectedNPEOnNull.java b/jdk/test/javax/sound/sampled/spi/AudioFileWriter/ExpectedNPEOnNull.java new file mode 100644 index 00000000000..1e128444235 --- /dev/null +++ b/jdk/test/javax/sound/sampled/spi/AudioFileWriter/ExpectedNPEOnNull.java @@ -0,0 +1,310 @@ +/* + * Copyright (c) 2015, 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.io.ByteArrayInputStream; +import java.io.File; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import javax.sound.sampled.AudioFileFormat; +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.spi.AudioFileWriter; + +import static java.util.ServiceLoader.load; +import static javax.sound.sampled.AudioFileFormat.Type; +import static javax.sound.sampled.AudioFormat.*; + +/** + * @test + * @bug 8135100 + * @author Sergey Bylokhov + */ +public final class ExpectedNPEOnNull { + + /** + * We will try to use all encoding, in this case all our providers will be + * covered by supported/unsupported encoding. + */ + private static final Encoding[] encodings = {Encoding.PCM_SIGNED, + Encoding.PCM_UNSIGNED, + Encoding.PCM_FLOAT, + Encoding.ULAW, Encoding.ALAW, + new Encoding("test")}; + + /** + * We will try to use all types, in this case all our providers will be + * covered by supported/unsupported types. + */ + private static final Type[] types = {Type.WAVE, Type.AU, Type.AIFF, + Type.AIFC, Type.SND, + new Type("MIDI", "mid"), + new Type("test", "test")}; + + /** + * We will try to use all supported AudioInputStream, in this case all our + * providers will be covered by supported/unsupported streams. + */ + private static final List aiss = new ArrayList<>(); + + static { + for (final Encoding encoding : encodings) { + for (final Type type : types) { + aiss.add(getAIS(type, encoding)); + } + } + } + + public static void main(final String[] args) throws Exception { + testAS(); + for (final AudioFileWriter afw : load(AudioFileWriter.class)) { + testAFW(afw); + } + testAFW(customAFW); + } + + /** + * Tests the part of AudioSystem API, which implemented via AudioFileWriter. + */ + private static void testAS() throws Exception { + + // AudioSystem#getAudioFileTypes(AudioInputStream) + try { + AudioSystem.getAudioFileTypes(null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + + // AudioSystem#isFileTypeSupported(Type) + try { + AudioSystem.isFileTypeSupported(null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + + // AudioSystem#isFileTypeSupported(Type, AudioInputStream) + for (final Type type : types) { + try { + AudioSystem.isFileTypeSupported(type, null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + for (final AudioInputStream stream : aiss) { + try { + AudioSystem.isFileTypeSupported(null, stream); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + + // AudioSystem#write(AudioInputStream, Type, OutputStream) + for (final Type type : types) { + try { + AudioSystem.write(null, type, new NullOutputStream()); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + for (final AudioInputStream stream : aiss) { + try { + AudioSystem.write(stream, null, new NullOutputStream()); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + for (final Type type : types) { + for (final AudioInputStream stream : aiss) { + try { + AudioSystem.write(stream, type, (OutputStream) null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + } + + // AudioSystem#write(AudioInputStream, Type, File) + for (final Type type : types) { + try { + AudioSystem.write(null, type, new File("test.sound")); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + + for (final AudioInputStream stream : aiss) { + try { + AudioSystem.write(stream, null, new File("test.sound")); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + for (final AudioInputStream stream : aiss) { + for (final Type type : types) { + try { + AudioSystem.write(stream, type, (File) null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + } + } + + /** + * Tests the AudioFileWriter API directly. + */ + private static void testAFW(final AudioFileWriter afw) throws Exception { + + // AudioFileWriter#isFileTypeSupported(Type) + try { + afw.isFileTypeSupported(null); + throw new RuntimeException("NPE is expected: " + afw); + } catch (final NullPointerException ignored) { + } + + // AudioFileWriter#getAudioFileTypes(AudioInputStream) + try { + afw.getAudioFileTypes(null); + throw new RuntimeException("NPE is expected: " + afw); + } catch (final NullPointerException ignored) { + } + + // AudioFileWriter#isFileTypeSupported(Type, AudioInputStream) + for (final Type type : types) { + try { + afw.isFileTypeSupported(type, null); + throw new RuntimeException("NPE is expected: " + afw); + } catch (final NullPointerException ignored) { + } + } + for (final AudioInputStream stream : aiss) { + try { + afw.isFileTypeSupported(null, stream); + throw new RuntimeException("NPE is expected: " + afw); + } catch (final NullPointerException ignored) { + } + } + + // AudioFileWriter#write(AudioInputStream, Type, OutputStream) + for (final Type type : types) { + try { + afw.write(null, type, new NullOutputStream()); + throw new RuntimeException("NPE is expected: " + afw); + } catch (final NullPointerException ignored) { + } + } + for (final AudioInputStream stream : aiss) { + try { + afw.write(stream, null, new NullOutputStream()); + throw new RuntimeException("NPE is expected: " + afw); + } catch (final NullPointerException ignored) { + } + } + for (final AudioInputStream stream : aiss) { + for (final Type type : types) { + try { + afw.write(stream, type, (OutputStream) null); + throw new RuntimeException("NPE is expected: " + afw); + } catch (final NullPointerException ignored) { + } + } + } + + // AudioFileWriter#write(AudioInputStream, Type, File) + for (final Type type : types) { + try { + afw.write(null, type, new File("test.sound")); + throw new RuntimeException("NPE is expected: " + afw); + } catch (final NullPointerException ignored) { + } + } + for (final AudioInputStream stream : aiss) { + try { + afw.write(stream, null, new File("test.sound")); + throw new RuntimeException("NPE is expected: " + afw); + } catch (final NullPointerException ignored) { + } + } + for (final AudioInputStream stream : aiss) { + for (final Type type : types) { + try { + afw.write(stream, type, (File) null); + throw new RuntimeException("NPE is expected: " + afw); + } catch (final NullPointerException ignored) { + } + } + } + } + + /** + * Tests some default implementation of AudioFileWriter API, using the + * custom {@code AudioFileWriter}, which support nothing. + */ + static final AudioFileWriter customAFW = new AudioFileWriter() { + @Override + public Type[] getAudioFileTypes() { + return new Type[0]; + } + + @Override + public Type[] getAudioFileTypes(final AudioInputStream stream) { + Objects.requireNonNull(stream); + return new Type[0]; + } + + @Override + public int write(AudioInputStream stream, Type fileType, + OutputStream out) { + Objects.requireNonNull(stream); + Objects.requireNonNull(fileType); + Objects.requireNonNull(out); + return 0; + } + + @Override + public int write(AudioInputStream stream, Type fileType, File out) { + Objects.requireNonNull(stream); + Objects.requireNonNull(fileType); + Objects.requireNonNull(out); + return 0; + } + }; + + private static AudioInputStream getAIS(final Type type, Encoding encoding) { + AudioFormat af = new AudioFormat(encoding, 44100.0f, 16, 2, 1, 1, true); + AudioFileFormat aif = new AudioFileFormat(type, af, 0); + ByteArrayInputStream bais = new ByteArrayInputStream(new byte[1024]); + return new AudioInputStream(bais, aif.getFormat(), 0); + } + + private static final class NullOutputStream extends OutputStream { + + @Override + public void write(final int b) { + //do nothing + } + } +} diff --git a/jdk/test/javax/sound/sampled/FileWriter/WriterCloseInput.java b/jdk/test/javax/sound/sampled/spi/AudioFileWriter/WriterCloseInput.java similarity index 100% rename from jdk/test/javax/sound/sampled/FileWriter/WriterCloseInput.java rename to jdk/test/javax/sound/sampled/spi/AudioFileWriter/WriterCloseInput.java diff --git a/jdk/test/javax/sound/sampled/spi/FormatConversionProvider/ExpectedNPEOnNull.java b/jdk/test/javax/sound/sampled/spi/FormatConversionProvider/ExpectedNPEOnNull.java new file mode 100644 index 00000000000..ce7125c7320 --- /dev/null +++ b/jdk/test/javax/sound/sampled/spi/FormatConversionProvider/ExpectedNPEOnNull.java @@ -0,0 +1,356 @@ +/* + * Copyright (c) 2015, 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.io.ByteArrayInputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import javax.sound.sampled.AudioFileFormat; +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioFormat.Encoding; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.spi.FormatConversionProvider; + +import static java.util.ServiceLoader.load; +import static javax.sound.sampled.AudioFileFormat.*; + +/** + * @test + * @bug 8135100 + * @author Sergey Bylokhov + */ +public final class ExpectedNPEOnNull { + + /** + * We will try to use all encoding, in this case all our providers will be + * covered by supported/unsupported encoding. + */ + private static final Encoding[] encodings = {Encoding.PCM_SIGNED, + Encoding.PCM_UNSIGNED, + Encoding.PCM_FLOAT, + Encoding.ULAW, Encoding.ALAW, + new Encoding("test")}; + + /** + * We will try to use all types, in this case all our providers will be + * covered by supported/unsupported types. + */ + private static final Type[] types = {Type.WAVE, Type.AU, Type.AIFF, + Type.AIFC, Type.SND, + new Type("MIDI", "mid"), + new Type("test", "test")}; + + /** + * We will try to use all supported AudioInputStream, in this case all our + * providers will be covered by supported/unsupported streams. + */ + private static final List aiss = new ArrayList<>(); + + static { + for (final Encoding encoding : encodings) { + for (final Type type : types) { + aiss.add(getAIS(type, encoding)); + } + } + } + + public static void main(final String[] args) throws Exception { + testAS(); + for (final FormatConversionProvider fcp : load( + FormatConversionProvider.class)) { + testFCP(fcp); + } + testFCP(customFCP); + } + + /** + * Tests the part of AudioSystem API, which implemented via + * FormatConversionProvider. + */ + private static void testAS() throws Exception { + + // AudioSystem#getAudioInputStream(Encoding, AudioInputStream) + for (final Encoding encoding : encodings) { + try { + AudioSystem.getAudioInputStream(encoding, null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + for (final AudioInputStream stream : aiss) { + try { + AudioSystem.getAudioInputStream((Encoding) null, stream); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + // AudioSystem#getAudioInputStream(AudioFormat, AudioInputStream) + for (final AudioInputStream stream : aiss) { + try { + AudioSystem.getAudioInputStream((AudioFormat) null, stream); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + for (final Encoding encoding : encodings) { + try { + AudioSystem.getAudioInputStream(getAFF(encoding), null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + + // AudioSystem#getTargetEncodings(AudioFormat) + try { + AudioSystem.getTargetEncodings((AudioFormat) null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + + // AudioSystem#getTargetEncodings(AudioFormat.Encoding) + try { + AudioSystem.getTargetEncodings((Encoding) null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + + // AudioSystem#getTargetFormats(AudioFormat.Encoding, AudioFormat) + for (final Encoding encoding : encodings) { + try { + AudioSystem.getTargetFormats(null, getAFF(encoding)); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + for (final Encoding encoding : encodings) { + try { + AudioSystem.getTargetFormats(encoding, null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + + // AudioSystem#isConversionSupported(AudioFormat, AudioFormat) + for (final Encoding encoding : encodings) { + try { + AudioSystem.isConversionSupported((AudioFormat) null, + getAFF(encoding)); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + for (final Encoding encoding : encodings) { + try { + AudioSystem.isConversionSupported(getAFF(encoding), null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + + // AudioSystem#isConversionSupported(AudioFormat.Encoding, AudioFormat) + for (final Encoding encoding : encodings) { + try { + AudioSystem.isConversionSupported((Encoding) null, + getAFF(encoding)); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + for (final Encoding encoding : encodings) { + try { + AudioSystem.isConversionSupported(encoding, null); + throw new RuntimeException("NPE is expected"); + } catch (final NullPointerException ignored) { + } + } + } + + /** + * Tests the FormatConversionProvider API directly. + */ + private static void testFCP(FormatConversionProvider fcp) throws Exception { + + // FormatConversionProvider#isSourceEncodingSupported(Encoding) + try { + fcp.isSourceEncodingSupported(null); + throw new RuntimeException("NPE is expected: " + fcp); + } catch (final NullPointerException ignored) { + } + + // FormatConversionProvider#isTargetEncodingSupported(Encoding) + try { + fcp.isTargetEncodingSupported(null); + throw new RuntimeException("NPE is expected: " + fcp); + } catch (final NullPointerException ignored) { + } + + // FormatConversionProvider#getTargetEncodings() + try { + fcp.getTargetEncodings(null); + throw new RuntimeException("NPE is expected: " + fcp); + } catch (final NullPointerException ignored) { + } + + // FormatConversionProvider#isConversionSupported(Encoding, AudioFormat) + for (final Encoding encoding : encodings) { + try { + fcp.isConversionSupported((Encoding) null, getAFF(encoding)); + throw new RuntimeException("NPE is expected: " + fcp); + } catch (final NullPointerException ignored) { + } + } + for (final Encoding encoding : encodings) { + try { + fcp.isConversionSupported(encoding, null); + throw new RuntimeException("NPE is expected: " + fcp); + } catch (final NullPointerException ignored) { + } + } + + // FormatConversionProvider#getTargetFormats(Encoding, AudioFormat) + for (final Encoding encoding : encodings) { + try { + fcp.getTargetFormats(null, getAFF(encoding)); + throw new RuntimeException("NPE is expected: " + fcp); + } catch (final NullPointerException ignored) { + } + } + for (final Encoding encoding : encodings) { + try { + fcp.getTargetFormats(encoding, null); + throw new RuntimeException("NPE is expected: " + fcp); + } catch (final NullPointerException ignored) { + } + } + + // FormatConversionProvider#isConversionSupported(AudioFormat, + // AudioFormat) + for (final Encoding encoding : encodings) { + try { + fcp.isConversionSupported((AudioFormat) null, getAFF(encoding)); + throw new RuntimeException("NPE is expected: " + fcp); + } catch (final NullPointerException ignored) { + } + } + for (final Encoding encoding : encodings) { + try { + fcp.isConversionSupported(getAFF(encoding), null); + throw new RuntimeException("NPE is expected: " + fcp); + } catch (final NullPointerException ignored) { + } + } + + // FormatConversionProvider#getAudioInputStream(Encoding, + // AudioInputStream) + for (final AudioInputStream stream : aiss) { + try { + fcp.getAudioInputStream((Encoding) null, stream); + throw new RuntimeException("NPE is expected: " + fcp); + } catch (final NullPointerException ignored) { + } + } + for (final Encoding encoding : encodings) { + try { + fcp.getAudioInputStream(encoding, null); + throw new RuntimeException("NPE is expected: " + fcp); + } catch (final NullPointerException ignored) { + } + } + + // FormatConversionProvider#getAudioInputStream(AudioFormat, + // AudioInputStream) + for (final AudioInputStream stream : aiss) { + try { + fcp.getAudioInputStream((AudioFormat) null, stream); + throw new RuntimeException("NPE is expected: " + fcp); + } catch (final NullPointerException ignored) { + } + } + for (final Encoding encoding : encodings) { + try { + fcp.getAudioInputStream(getAFF(encoding), null); + throw new RuntimeException("NPE is expected: " + fcp); + } catch (final NullPointerException ignored) { + } + } + } + + /** + * Tests some default implementation of FormatConversionProvider API, using + * the custom {@code FormatConversionProvider}, which support nothing. + */ + static FormatConversionProvider customFCP = new FormatConversionProvider() { + + @Override + public Encoding[] getSourceEncodings() { + return new Encoding[0]; + } + + @Override + public Encoding[] getTargetEncodings() { + return new Encoding[0]; + } + + @Override + public Encoding[] getTargetEncodings(AudioFormat sourceFormat) { + Objects.requireNonNull(sourceFormat); + return new Encoding[0]; + } + + @Override + public AudioFormat[] getTargetFormats(Encoding enc, AudioFormat frmt) { + Objects.requireNonNull(enc); + Objects.requireNonNull(frmt); + return new AudioFormat[0]; + } + + @Override + public AudioInputStream getAudioInputStream(Encoding encoding, + AudioInputStream stream) { + Objects.requireNonNull(encoding); + Objects.requireNonNull(stream); + return null; + } + + @Override + public AudioInputStream getAudioInputStream(AudioFormat format, + AudioInputStream stream) { + Objects.requireNonNull(format); + Objects.requireNonNull(stream); + return null; + } + }; + + private static AudioFormat getAFF(final Encoding encoding) { + return new AudioFormat(encoding, 44100.0f, 16, 2, 1, 1, true); + } + + private static AudioInputStream getAIS(final Type type, Encoding encoding) { + AudioFormat af = new AudioFormat(encoding, 44100.0f, 16, 2, 1, 1, true); + AudioFileFormat aif = new AudioFileFormat(type, af, 0); + ByteArrayInputStream bais = new ByteArrayInputStream(new byte[1024]); + return new AudioInputStream(bais, aif.getFormat(), 0); + } +} diff --git a/jdk/test/javax/sound/sampled/spi/MixerProvider/ExpectedNPEOnNull.java b/jdk/test/javax/sound/sampled/spi/MixerProvider/ExpectedNPEOnNull.java new file mode 100644 index 00000000000..8f88472bd2c --- /dev/null +++ b/jdk/test/javax/sound/sampled/spi/MixerProvider/ExpectedNPEOnNull.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2015, 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 javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Mixer; +import javax.sound.sampled.spi.MixerProvider; + +import static java.util.ServiceLoader.load; + +/** + * @test + * @bug 8135100 + * @author Sergey Bylokhov + */ +public final class ExpectedNPEOnNull { + + public static void main(final String[] args) throws Exception { + testAS(); + for (final MixerProvider mp : load(MixerProvider.class)) { + testMP(mp); + } + testMP(customMP); + } + + /** + * Tests the part of AudioSystem API, which implemented via MixerProvider. + */ + private static void testAS() { + try { + AudioSystem.getMixer(null); // null should be accepted + } catch (final SecurityException | IllegalArgumentException ignored) { + // skip the specified exceptions only + } + } + + /** + * Tests the MixerProvider API directly. + */ + private static void testMP(MixerProvider mp) { + try { + mp.isMixerSupported(null); + throw new RuntimeException("NPE is expected: " + mp); + } catch (final NullPointerException ignored) { + + } + try { + mp.getMixer(null); // null should be accepted + } catch (SecurityException | IllegalArgumentException e) { + // skip the specified exceptions only + } + } + + /** + * Tests some default implementation of MixerProvider API, using the + * custom {@code MixerProvider}, which support nothing. + */ + static final MixerProvider customMP = new MixerProvider() { + @Override + public Mixer.Info[] getMixerInfo() { + return new Mixer.Info[0]; + } + + @Override + public Mixer getMixer(Mixer.Info info) { + return null; + } + }; +}