jdk/src/share/classes/javax/sound/sampled/AudioSystem.java
changeset 25131 49006bd0e69d
parent 25128 2dfdfa369071
child 26003 d630c97424bd
equal deleted inserted replaced
25130:adfaa02ea516 25131:49006bd0e69d
   173      *         returned.
   173      *         returned.
   174      * @see #getMixer
   174      * @see #getMixer
   175      */
   175      */
   176     public static Mixer.Info[] getMixerInfo() {
   176     public static Mixer.Info[] getMixerInfo() {
   177 
   177 
   178         List infos = getMixerInfoList();
   178         List<Mixer.Info> infos = getMixerInfoList();
   179         Mixer.Info[] allInfos = (Mixer.Info[]) infos.toArray(new Mixer.Info[infos.size()]);
   179         Mixer.Info[] allInfos = infos.toArray(new Mixer.Info[infos.size()]);
   180         return allInfos;
   180         return allInfos;
   181     }
   181     }
   182 
   182 
   183     /**
   183     /**
   184      * Obtains the requested audio mixer.
   184      * Obtains the requested audio mixer.
   193      * @see #getMixerInfo
   193      * @see #getMixerInfo
   194      */
   194      */
   195     public static Mixer getMixer(Mixer.Info info) {
   195     public static Mixer getMixer(Mixer.Info info) {
   196 
   196 
   197         Mixer mixer = null;
   197         Mixer mixer = null;
   198         List providers = getMixerProviders();
   198         List<MixerProvider> providers = getMixerProviders();
   199 
   199 
   200         for(int i = 0; i < providers.size(); i++ ) {
   200         for(int i = 0; i < providers.size(); i++ ) {
   201 
   201 
   202             try {
   202             try {
   203                 return ((MixerProvider)providers.get(i)).getMixer(info);
   203                 return providers.get(i).getMixer(info);
   204 
   204 
   205             } catch (IllegalArgumentException e) {
   205             } catch (IllegalArgumentException e) {
   206             } catch (NullPointerException e) {
   206             } catch (NullPointerException e) {
   207                 // $$jb 08.20.99:  If the strings in the info object aren't
   207                 // $$jb 08.20.99:  If the strings in the info object aren't
   208                 // set, then Netscape (using jdk1.1.5) tends to throw
   208                 // set, then Netscape (using jdk1.1.5) tends to throw
   215 
   215 
   216         //$$fb if looking for default mixer, and not found yet, add a round of looking
   216         //$$fb if looking for default mixer, and not found yet, add a round of looking
   217         if (info == null) {
   217         if (info == null) {
   218             for(int i = 0; i < providers.size(); i++ ) {
   218             for(int i = 0; i < providers.size(); i++ ) {
   219                 try {
   219                 try {
   220                     MixerProvider provider = (MixerProvider) providers.get(i);
   220                     MixerProvider provider = providers.get(i);
   221                     Mixer.Info[] infos = provider.getMixerInfo();
   221                     Mixer.Info[] infos = provider.getMixerInfo();
   222                     // start from 0 to last device (do not reverse this order)
   222                     // start from 0 to last device (do not reverse this order)
   223                     for (int ii = 0; ii < infos.length; ii++) {
   223                     for (int ii = 0; ii < infos.length; ii++) {
   224                         try {
   224                         try {
   225                             return provider.getMixer(infos[ii]);
   225                             return provider.getMixer(infos[ii]);
   251      *         supported, an array of length 0 is returned.
   251      *         supported, an array of length 0 is returned.
   252      * @see Mixer#getSourceLineInfo(Line.Info)
   252      * @see Mixer#getSourceLineInfo(Line.Info)
   253      */
   253      */
   254     public static Line.Info[] getSourceLineInfo(Line.Info info) {
   254     public static Line.Info[] getSourceLineInfo(Line.Info info) {
   255 
   255 
   256         Vector vector = new Vector();
   256         Vector<Line.Info> vector = new Vector<>();
   257         Line.Info[] currentInfoArray;
   257         Line.Info[] currentInfoArray;
   258 
   258 
   259         Mixer mixer;
   259         Mixer mixer;
   260         Line.Info fullInfo = null;
   260         Line.Info fullInfo = null;
   261         Mixer.Info[] infoArray = getMixerInfo();
   261         Mixer.Info[] infoArray = getMixerInfo();
   271         }
   271         }
   272 
   272 
   273         Line.Info[] returnedArray = new Line.Info[vector.size()];
   273         Line.Info[] returnedArray = new Line.Info[vector.size()];
   274 
   274 
   275         for (int i = 0; i < returnedArray.length; i++) {
   275         for (int i = 0; i < returnedArray.length; i++) {
   276             returnedArray[i] = (Line.Info)vector.get(i);
   276             returnedArray[i] = vector.get(i);
   277         }
   277         }
   278 
   278 
   279         return returnedArray;
   279         return returnedArray;
   280     }
   280     }
   281 
   281 
   290      *         supported, an array of length 0 is returned.
   290      *         supported, an array of length 0 is returned.
   291      * @see Mixer#getTargetLineInfo(Line.Info)
   291      * @see Mixer#getTargetLineInfo(Line.Info)
   292      */
   292      */
   293     public static Line.Info[] getTargetLineInfo(Line.Info info) {
   293     public static Line.Info[] getTargetLineInfo(Line.Info info) {
   294 
   294 
   295         Vector vector = new Vector();
   295         Vector<Line.Info> vector = new Vector<>();
   296         Line.Info[] currentInfoArray;
   296         Line.Info[] currentInfoArray;
   297 
   297 
   298         Mixer mixer;
   298         Mixer mixer;
   299         Line.Info fullInfo = null;
   299         Line.Info fullInfo = null;
   300         Mixer.Info[] infoArray = getMixerInfo();
   300         Mixer.Info[] infoArray = getMixerInfo();
   310         }
   310         }
   311 
   311 
   312         Line.Info[] returnedArray = new Line.Info[vector.size()];
   312         Line.Info[] returnedArray = new Line.Info[vector.size()];
   313 
   313 
   314         for (int i = 0; i < returnedArray.length; i++) {
   314         for (int i = 0; i < returnedArray.length; i++) {
   315             returnedArray[i] = (Line.Info)vector.get(i);
   315             returnedArray[i] = vector.get(i);
   316         }
   316         }
   317 
   317 
   318         return returnedArray;
   318         return returnedArray;
   319     }
   319     }
   320 
   320 
   380      *         one line matching the specified {@code Line.Info} object through
   380      *         one line matching the specified {@code Line.Info} object through
   381      *         any installed mixer
   381      *         any installed mixer
   382      */
   382      */
   383     public static Line getLine(Line.Info info) throws LineUnavailableException {
   383     public static Line getLine(Line.Info info) throws LineUnavailableException {
   384         LineUnavailableException lue = null;
   384         LineUnavailableException lue = null;
   385         List providers = getMixerProviders();
   385         List<MixerProvider> providers = getMixerProviders();
   386 
   386 
   387 
   387 
   388         // 1: try from default mixer for this line class
   388         // 1: try from default mixer for this line class
   389         try {
   389         try {
   390             Mixer mixer = getDefaultMixer(providers, info);
   390             Mixer mixer = getDefaultMixer(providers, info);
   399         }
   399         }
   400 
   400 
   401 
   401 
   402         // 2: if that doesn't work, try to find any mixing mixer
   402         // 2: if that doesn't work, try to find any mixing mixer
   403         for(int i = 0; i < providers.size(); i++) {
   403         for(int i = 0; i < providers.size(); i++) {
   404             MixerProvider provider = (MixerProvider) providers.get(i);
   404             MixerProvider provider = providers.get(i);
   405             Mixer.Info[] infos = provider.getMixerInfo();
   405             Mixer.Info[] infos = provider.getMixerInfo();
   406 
   406 
   407             for (int j = 0; j < infos.length; j++) {
   407             for (int j = 0; j < infos.length; j++) {
   408                 try {
   408                 try {
   409                     Mixer mixer = provider.getMixer(infos[j]);
   409                     Mixer mixer = provider.getMixer(infos[j]);
   421         }
   421         }
   422 
   422 
   423 
   423 
   424         // 3: if that didn't work, try to find any non-mixing mixer
   424         // 3: if that didn't work, try to find any non-mixing mixer
   425         for(int i = 0; i < providers.size(); i++) {
   425         for(int i = 0; i < providers.size(); i++) {
   426             MixerProvider provider = (MixerProvider) providers.get(i);
   426             MixerProvider provider = providers.get(i);
   427             Mixer.Info[] infos = provider.getMixerInfo();
   427             Mixer.Info[] infos = provider.getMixerInfo();
   428             for (int j = 0; j < infos.length; j++) {
   428             for (int j = 0; j < infos.length; j++) {
   429                 try {
   429                 try {
   430                     Mixer mixer = provider.getMixer(infos[j]);
   430                     Mixer mixer = provider.getMixer(infos[j]);
   431                     // see if this is an appropriate mixer which can mix
   431                     // see if this is an appropriate mixer which can mix
   698      *         length of at least 1, representing {@code sourceEncoding}
   698      *         length of at least 1, representing {@code sourceEncoding}
   699      *         (no conversion).
   699      *         (no conversion).
   700      */
   700      */
   701     public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat.Encoding sourceEncoding) {
   701     public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat.Encoding sourceEncoding) {
   702 
   702 
   703         List codecs = getFormatConversionProviders();
   703         List<FormatConversionProvider> codecs = getFormatConversionProviders();
   704         Vector encodings = new Vector();
   704         Vector<AudioFormat.Encoding> encodings = new Vector<>();
   705 
   705 
   706         AudioFormat.Encoding encs[] = null;
   706         AudioFormat.Encoding encs[] = null;
   707 
   707 
   708         // gather from all the codecs
   708         // gather from all the codecs
   709         for(int i=0; i<codecs.size(); i++ ) {
   709         for(int i=0; i<codecs.size(); i++ ) {
   710             FormatConversionProvider codec = (FormatConversionProvider) codecs.get(i);
   710             FormatConversionProvider codec = codecs.get(i);
   711             if( codec.isSourceEncodingSupported( sourceEncoding ) ) {
   711             if( codec.isSourceEncodingSupported( sourceEncoding ) ) {
   712                 encs = codec.getTargetEncodings();
   712                 encs = codec.getTargetEncodings();
   713                 for (int j = 0; j < encs.length; j++) {
   713                 for (int j = 0; j < encs.length; j++) {
   714                     encodings.addElement( encs[j] );
   714                     encodings.addElement( encs[j] );
   715                 }
   715                 }
   716             }
   716             }
   717         }
   717         }
   718         AudioFormat.Encoding encs2[] = (AudioFormat.Encoding[]) encodings.toArray(new AudioFormat.Encoding[0]);
   718         AudioFormat.Encoding encs2[] = encodings.toArray(new AudioFormat.Encoding[0]);
   719         return encs2;
   719         return encs2;
   720     }
   720     }
   721 
   721 
   722     // $$fb 2002-04-12: fix for 4662082: behavior of AudioSystem.getTargetEncodings() methods doesn't match the spec
   722     // $$fb 2002-04-12: fix for 4662082: behavior of AudioSystem.getTargetEncodings() methods doesn't match the spec
   723 
   723 
   733      *         {@code sourceFormat} (no conversion).
   733      *         {@code sourceFormat} (no conversion).
   734      */
   734      */
   735     public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat) {
   735     public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat) {
   736 
   736 
   737 
   737 
   738         List codecs = getFormatConversionProviders();
   738         List<FormatConversionProvider> codecs = getFormatConversionProviders();
   739         Vector encodings = new Vector();
   739         Vector<AudioFormat.Encoding[]> encodings = new Vector<>();
   740 
   740 
   741         int size = 0;
   741         int size = 0;
   742         int index = 0;
   742         int index = 0;
   743         AudioFormat.Encoding encs[] = null;
   743         AudioFormat.Encoding encs[] = null;
   744 
   744 
   745         // gather from all the codecs
   745         // gather from all the codecs
   746 
   746 
   747         for(int i=0; i<codecs.size(); i++ ) {
   747         for(int i=0; i<codecs.size(); i++ ) {
   748             encs = ((FormatConversionProvider) codecs.get(i)).getTargetEncodings(sourceFormat);
   748             encs = codecs.get(i).getTargetEncodings(sourceFormat);
   749             size += encs.length;
   749             size += encs.length;
   750             encodings.addElement( encs );
   750             encodings.addElement( encs );
   751         }
   751         }
   752 
   752 
   753         // now build a new array
   753         // now build a new array
   754 
   754 
   755         AudioFormat.Encoding encs2[] = new AudioFormat.Encoding[size];
   755         AudioFormat.Encoding encs2[] = new AudioFormat.Encoding[size];
   756         for(int i=0; i<encodings.size(); i++ ) {
   756         for(int i=0; i<encodings.size(); i++ ) {
   757             encs = (AudioFormat.Encoding [])(encodings.get(i));
   757             encs = encodings.get(i);
   758             for(int j=0; j<encs.length; j++ ) {
   758             for(int j=0; j<encs.length; j++ ) {
   759                 encs2[index++] = encs[j];
   759                 encs2[index++] = encs[j];
   760             }
   760             }
   761         }
   761         }
   762         return encs2;
   762         return encs2;
   772      *         {@code false}
   772      *         {@code false}
   773      */
   773      */
   774     public static boolean isConversionSupported(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat) {
   774     public static boolean isConversionSupported(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat) {
   775 
   775 
   776 
   776 
   777         List codecs = getFormatConversionProviders();
   777         List<FormatConversionProvider> codecs = getFormatConversionProviders();
   778 
   778 
   779         for(int i=0; i<codecs.size(); i++ ) {
   779         for(int i=0; i<codecs.size(); i++ ) {
   780             FormatConversionProvider codec = (FormatConversionProvider) codecs.get(i);
   780             FormatConversionProvider codec = codecs.get(i);
   781             if(codec.isConversionSupported(targetEncoding,sourceFormat) ) {
   781             if(codec.isConversionSupported(targetEncoding,sourceFormat) ) {
   782                 return true;
   782                 return true;
   783             }
   783             }
   784         }
   784         }
   785         return false;
   785         return false;
   799      * @see #getAudioInputStream(AudioFormat, AudioInputStream)
   799      * @see #getAudioInputStream(AudioFormat, AudioInputStream)
   800      */
   800      */
   801     public static AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding,
   801     public static AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding,
   802                                                        AudioInputStream sourceStream) {
   802                                                        AudioInputStream sourceStream) {
   803 
   803 
   804         List codecs = getFormatConversionProviders();
   804         List<FormatConversionProvider> codecs = getFormatConversionProviders();
   805 
   805 
   806         for(int i = 0; i < codecs.size(); i++) {
   806         for(int i = 0; i < codecs.size(); i++) {
   807             FormatConversionProvider codec = (FormatConversionProvider) codecs.get(i);
   807             FormatConversionProvider codec = codecs.get(i);
   808             if( codec.isConversionSupported( targetEncoding, sourceStream.getFormat() ) ) {
   808             if( codec.isConversionSupported( targetEncoding, sourceStream.getFormat() ) ) {
   809                 return codec.getAudioInputStream( targetEncoding, sourceStream );
   809                 return codec.getAudioInputStream( targetEncoding, sourceStream );
   810             }
   810             }
   811         }
   811         }
   812         // we ran out of options, throw an exception
   812         // we ran out of options, throw an exception
   823      * @return array of formats. If no formats of the specified encoding are
   823      * @return array of formats. If no formats of the specified encoding are
   824      *         supported, an array of length 0 is returned.
   824      *         supported, an array of length 0 is returned.
   825      */
   825      */
   826     public static AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat) {
   826     public static AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat) {
   827 
   827 
   828         List codecs = getFormatConversionProviders();
   828         List<FormatConversionProvider> codecs = getFormatConversionProviders();
   829         Vector formats = new Vector();
   829         Vector<AudioFormat[]> formats = new Vector<>();
   830 
   830 
   831         int size = 0;
   831         int size = 0;
   832         int index = 0;
   832         int index = 0;
   833         AudioFormat fmts[] = null;
   833         AudioFormat fmts[] = null;
   834 
   834 
   835         // gather from all the codecs
   835         // gather from all the codecs
   836 
   836 
   837         for(int i=0; i<codecs.size(); i++ ) {
   837         for(int i=0; i<codecs.size(); i++ ) {
   838             FormatConversionProvider codec = (FormatConversionProvider) codecs.get(i);
   838             FormatConversionProvider codec = codecs.get(i);
   839             fmts = codec.getTargetFormats(targetEncoding, sourceFormat);
   839             fmts = codec.getTargetFormats(targetEncoding, sourceFormat);
   840             size += fmts.length;
   840             size += fmts.length;
   841             formats.addElement( fmts );
   841             formats.addElement( fmts );
   842         }
   842         }
   843 
   843 
   844         // now build a new array
   844         // now build a new array
   845 
   845 
   846         AudioFormat fmts2[] = new AudioFormat[size];
   846         AudioFormat fmts2[] = new AudioFormat[size];
   847         for(int i=0; i<formats.size(); i++ ) {
   847         for(int i=0; i<formats.size(); i++ ) {
   848             fmts = (AudioFormat [])(formats.get(i));
   848             fmts = formats.get(i);
   849             for(int j=0; j<fmts.length; j++ ) {
   849             for(int j=0; j<fmts.length; j++ ) {
   850                 fmts2[index++] = fmts[j];
   850                 fmts2[index++] = fmts[j];
   851             }
   851             }
   852         }
   852         }
   853         return fmts2;
   853         return fmts2;
   862      * @return {@code true} if the conversion is supported, otherwise
   862      * @return {@code true} if the conversion is supported, otherwise
   863      *         {@code false}
   863      *         {@code false}
   864      */
   864      */
   865     public static boolean isConversionSupported(AudioFormat targetFormat, AudioFormat sourceFormat) {
   865     public static boolean isConversionSupported(AudioFormat targetFormat, AudioFormat sourceFormat) {
   866 
   866 
   867         List codecs = getFormatConversionProviders();
   867         List<FormatConversionProvider> codecs = getFormatConversionProviders();
   868 
   868 
   869         for(int i=0; i<codecs.size(); i++ ) {
   869         for(int i=0; i<codecs.size(); i++ ) {
   870             FormatConversionProvider codec = (FormatConversionProvider) codecs.get(i);
   870             FormatConversionProvider codec = codecs.get(i);
   871             if(codec.isConversionSupported(targetFormat, sourceFormat) ) {
   871             if(codec.isConversionSupported(targetFormat, sourceFormat) ) {
   872                 return true;
   872                 return true;
   873             }
   873             }
   874         }
   874         }
   875         return false;
   875         return false;
   893 
   893 
   894         if (sourceStream.getFormat().matches(targetFormat)) {
   894         if (sourceStream.getFormat().matches(targetFormat)) {
   895             return sourceStream;
   895             return sourceStream;
   896         }
   896         }
   897 
   897 
   898         List codecs = getFormatConversionProviders();
   898         List<FormatConversionProvider> codecs = getFormatConversionProviders();
   899 
   899 
   900         for(int i = 0; i < codecs.size(); i++) {
   900         for(int i = 0; i < codecs.size(); i++) {
   901             FormatConversionProvider codec = (FormatConversionProvider) codecs.get(i);
   901             FormatConversionProvider codec = codecs.get(i);
   902             if(codec.isConversionSupported(targetFormat,sourceStream.getFormat()) ) {
   902             if(codec.isConversionSupported(targetFormat,sourceStream.getFormat()) ) {
   903                 return codec.getAudioInputStream(targetFormat,sourceStream);
   903                 return codec.getAudioInputStream(targetFormat,sourceStream);
   904             }
   904             }
   905         }
   905         }
   906 
   906 
   929      * @see InputStream#mark
   929      * @see InputStream#mark
   930      */
   930      */
   931     public static AudioFileFormat getAudioFileFormat(InputStream stream)
   931     public static AudioFileFormat getAudioFileFormat(InputStream stream)
   932         throws UnsupportedAudioFileException, IOException {
   932         throws UnsupportedAudioFileException, IOException {
   933 
   933 
   934         List providers = getAudioFileReaders();
   934         List<AudioFileReader> providers = getAudioFileReaders();
   935         AudioFileFormat format = null;
   935         AudioFileFormat format = null;
   936 
   936 
   937         for(int i = 0; i < providers.size(); i++ ) {
   937         for(int i = 0; i < providers.size(); i++ ) {
   938             AudioFileReader reader = (AudioFileReader) providers.get(i);
   938             AudioFileReader reader = providers.get(i);
   939             try {
   939             try {
   940                 format = reader.getAudioFileFormat( stream ); // throws IOException
   940                 format = reader.getAudioFileFormat( stream ); // throws IOException
   941                 break;
   941                 break;
   942             } catch (UnsupportedAudioFileException e) {
   942             } catch (UnsupportedAudioFileException e) {
   943                 continue;
   943                 continue;
   964      * @throws IOException if an input/output exception occurs
   964      * @throws IOException if an input/output exception occurs
   965      */
   965      */
   966     public static AudioFileFormat getAudioFileFormat(URL url)
   966     public static AudioFileFormat getAudioFileFormat(URL url)
   967         throws UnsupportedAudioFileException, IOException {
   967         throws UnsupportedAudioFileException, IOException {
   968 
   968 
   969         List providers = getAudioFileReaders();
   969         List<AudioFileReader> providers = getAudioFileReaders();
   970         AudioFileFormat format = null;
   970         AudioFileFormat format = null;
   971 
   971 
   972         for(int i = 0; i < providers.size(); i++ ) {
   972         for(int i = 0; i < providers.size(); i++ ) {
   973             AudioFileReader reader = (AudioFileReader) providers.get(i);
   973             AudioFileReader reader = providers.get(i);
   974             try {
   974             try {
   975                 format = reader.getAudioFileFormat( url ); // throws IOException
   975                 format = reader.getAudioFileFormat( url ); // throws IOException
   976                 break;
   976                 break;
   977             } catch (UnsupportedAudioFileException e) {
   977             } catch (UnsupportedAudioFileException e) {
   978                 continue;
   978                 continue;
   999      * @throws IOException if an I/O exception occurs
   999      * @throws IOException if an I/O exception occurs
  1000      */
  1000      */
  1001     public static AudioFileFormat getAudioFileFormat(File file)
  1001     public static AudioFileFormat getAudioFileFormat(File file)
  1002         throws UnsupportedAudioFileException, IOException {
  1002         throws UnsupportedAudioFileException, IOException {
  1003 
  1003 
  1004         List providers = getAudioFileReaders();
  1004         List<AudioFileReader> providers = getAudioFileReaders();
  1005         AudioFileFormat format = null;
  1005         AudioFileFormat format = null;
  1006 
  1006 
  1007         for(int i = 0; i < providers.size(); i++ ) {
  1007         for(int i = 0; i < providers.size(); i++ ) {
  1008             AudioFileReader reader = (AudioFileReader) providers.get(i);
  1008             AudioFileReader reader = providers.get(i);
  1009             try {
  1009             try {
  1010                 format = reader.getAudioFileFormat( file ); // throws IOException
  1010                 format = reader.getAudioFileFormat( file ); // throws IOException
  1011                 break;
  1011                 break;
  1012             } catch (UnsupportedAudioFileException e) {
  1012             } catch (UnsupportedAudioFileException e) {
  1013                 continue;
  1013                 continue;
  1042      * @see InputStream#mark
  1042      * @see InputStream#mark
  1043      */
  1043      */
  1044     public static AudioInputStream getAudioInputStream(InputStream stream)
  1044     public static AudioInputStream getAudioInputStream(InputStream stream)
  1045         throws UnsupportedAudioFileException, IOException {
  1045         throws UnsupportedAudioFileException, IOException {
  1046 
  1046 
  1047         List providers = getAudioFileReaders();
  1047         List<AudioFileReader> providers = getAudioFileReaders();
  1048         AudioInputStream audioStream = null;
  1048         AudioInputStream audioStream = null;
  1049 
  1049 
  1050         for(int i = 0; i < providers.size(); i++ ) {
  1050         for(int i = 0; i < providers.size(); i++ ) {
  1051             AudioFileReader reader = (AudioFileReader) providers.get(i);
  1051             AudioFileReader reader = providers.get(i);
  1052             try {
  1052             try {
  1053                 audioStream = reader.getAudioInputStream( stream ); // throws IOException
  1053                 audioStream = reader.getAudioInputStream( stream ); // throws IOException
  1054                 break;
  1054                 break;
  1055             } catch (UnsupportedAudioFileException e) {
  1055             } catch (UnsupportedAudioFileException e) {
  1056                 continue;
  1056                 continue;
  1077      * @throws IOException if an I/O exception occurs
  1077      * @throws IOException if an I/O exception occurs
  1078      */
  1078      */
  1079     public static AudioInputStream getAudioInputStream(URL url)
  1079     public static AudioInputStream getAudioInputStream(URL url)
  1080         throws UnsupportedAudioFileException, IOException {
  1080         throws UnsupportedAudioFileException, IOException {
  1081 
  1081 
  1082         List providers = getAudioFileReaders();
  1082         List<AudioFileReader> providers = getAudioFileReaders();
  1083         AudioInputStream audioStream = null;
  1083         AudioInputStream audioStream = null;
  1084 
  1084 
  1085         for(int i = 0; i < providers.size(); i++ ) {
  1085         for(int i = 0; i < providers.size(); i++ ) {
  1086             AudioFileReader reader = (AudioFileReader) providers.get(i);
  1086             AudioFileReader reader = providers.get(i);
  1087             try {
  1087             try {
  1088                 audioStream = reader.getAudioInputStream( url ); // throws IOException
  1088                 audioStream = reader.getAudioInputStream( url ); // throws IOException
  1089                 break;
  1089                 break;
  1090             } catch (UnsupportedAudioFileException e) {
  1090             } catch (UnsupportedAudioFileException e) {
  1091                 continue;
  1091                 continue;
  1112      * @throws IOException if an I/O exception occurs
  1112      * @throws IOException if an I/O exception occurs
  1113      */
  1113      */
  1114     public static AudioInputStream getAudioInputStream(File file)
  1114     public static AudioInputStream getAudioInputStream(File file)
  1115         throws UnsupportedAudioFileException, IOException {
  1115         throws UnsupportedAudioFileException, IOException {
  1116 
  1116 
  1117         List providers = getAudioFileReaders();
  1117         List<AudioFileReader> providers = getAudioFileReaders();
  1118         AudioInputStream audioStream = null;
  1118         AudioInputStream audioStream = null;
  1119 
  1119 
  1120         for(int i = 0; i < providers.size(); i++ ) {
  1120         for(int i = 0; i < providers.size(); i++ ) {
  1121             AudioFileReader reader = (AudioFileReader) providers.get(i);
  1121             AudioFileReader reader = providers.get(i);
  1122             try {
  1122             try {
  1123                 audioStream = reader.getAudioInputStream( file ); // throws IOException
  1123                 audioStream = reader.getAudioInputStream( file ); // throws IOException
  1124                 break;
  1124                 break;
  1125             } catch (UnsupportedAudioFileException e) {
  1125             } catch (UnsupportedAudioFileException e) {
  1126                 continue;
  1126                 continue;
  1140      *
  1140      *
  1141      * @return array of unique file types. If no file types are supported, an
  1141      * @return array of unique file types. If no file types are supported, an
  1142      *         array of length 0 is returned.
  1142      *         array of length 0 is returned.
  1143      */
  1143      */
  1144     public static AudioFileFormat.Type[] getAudioFileTypes() {
  1144     public static AudioFileFormat.Type[] getAudioFileTypes() {
  1145         List providers = getAudioFileWriters();
  1145         List<AudioFileWriter> providers = getAudioFileWriters();
  1146         Set returnTypesSet = new HashSet();
  1146         Set<AudioFileFormat.Type> returnTypesSet = new HashSet<>();
  1147 
  1147 
  1148         for(int i=0; i < providers.size(); i++) {
  1148         for(int i=0; i < providers.size(); i++) {
  1149             AudioFileWriter writer = (AudioFileWriter) providers.get(i);
  1149             AudioFileWriter writer = providers.get(i);
  1150             AudioFileFormat.Type[] fileTypes = writer.getAudioFileTypes();
  1150             AudioFileFormat.Type[] fileTypes = writer.getAudioFileTypes();
  1151             for(int j=0; j < fileTypes.length; j++) {
  1151             for(int j=0; j < fileTypes.length; j++) {
  1152                 returnTypesSet.add(fileTypes[j]);
  1152                 returnTypesSet.add(fileTypes[j]);
  1153             }
  1153             }
  1154         }
  1154         }
  1155         AudioFileFormat.Type returnTypes[] = (AudioFileFormat.Type[])
  1155         AudioFileFormat.Type returnTypes[] =
  1156             returnTypesSet.toArray(new AudioFileFormat.Type[0]);
  1156             returnTypesSet.toArray(new AudioFileFormat.Type[0]);
  1157         return returnTypes;
  1157         return returnTypes;
  1158     }
  1158     }
  1159 
  1159 
  1160     /**
  1160     /**
  1165      * @return {@code true} if the file type is supported, otherwise
  1165      * @return {@code true} if the file type is supported, otherwise
  1166      *         {@code false}
  1166      *         {@code false}
  1167      */
  1167      */
  1168     public static boolean isFileTypeSupported(AudioFileFormat.Type fileType) {
  1168     public static boolean isFileTypeSupported(AudioFileFormat.Type fileType) {
  1169 
  1169 
  1170         List providers = getAudioFileWriters();
  1170         List<AudioFileWriter> providers = getAudioFileWriters();
  1171 
  1171 
  1172         for(int i=0; i < providers.size(); i++) {
  1172         for(int i=0; i < providers.size(); i++) {
  1173             AudioFileWriter writer = (AudioFileWriter) providers.get(i);
  1173             AudioFileWriter writer = providers.get(i);
  1174             if (writer.isFileTypeSupported(fileType)) {
  1174             if (writer.isFileTypeSupported(fileType)) {
  1175                 return true;
  1175                 return true;
  1176             }
  1176             }
  1177         }
  1177         }
  1178         return false;
  1178         return false;
  1186      *         support is queried
  1186      *         support is queried
  1187      * @return array of file types. If no file types are supported, an array of
  1187      * @return array of file types. If no file types are supported, an array of
  1188      *         length 0 is returned.
  1188      *         length 0 is returned.
  1189      */
  1189      */
  1190     public static AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
  1190     public static AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
  1191         List providers = getAudioFileWriters();
  1191         List<AudioFileWriter> providers = getAudioFileWriters();
  1192         Set returnTypesSet = new HashSet();
  1192         Set<AudioFileFormat.Type> returnTypesSet = new HashSet<>();
  1193 
  1193 
  1194         for(int i=0; i < providers.size(); i++) {
  1194         for(int i=0; i < providers.size(); i++) {
  1195             AudioFileWriter writer = (AudioFileWriter) providers.get(i);
  1195             AudioFileWriter writer = providers.get(i);
  1196             AudioFileFormat.Type[] fileTypes = writer.getAudioFileTypes(stream);
  1196             AudioFileFormat.Type[] fileTypes = writer.getAudioFileTypes(stream);
  1197             for(int j=0; j < fileTypes.length; j++) {
  1197             for(int j=0; j < fileTypes.length; j++) {
  1198                 returnTypesSet.add(fileTypes[j]);
  1198                 returnTypesSet.add(fileTypes[j]);
  1199             }
  1199             }
  1200         }
  1200         }
  1201         AudioFileFormat.Type returnTypes[] = (AudioFileFormat.Type[])
  1201         AudioFileFormat.Type returnTypes[] =
  1202             returnTypesSet.toArray(new AudioFileFormat.Type[0]);
  1202             returnTypesSet.toArray(new AudioFileFormat.Type[0]);
  1203         return returnTypes;
  1203         return returnTypes;
  1204     }
  1204     }
  1205 
  1205 
  1206     /**
  1206     /**
  1213      *         stream, otherwise {@code false}
  1213      *         stream, otherwise {@code false}
  1214      */
  1214      */
  1215     public static boolean isFileTypeSupported(AudioFileFormat.Type fileType,
  1215     public static boolean isFileTypeSupported(AudioFileFormat.Type fileType,
  1216                                               AudioInputStream stream) {
  1216                                               AudioInputStream stream) {
  1217 
  1217 
  1218         List providers = getAudioFileWriters();
  1218         List<AudioFileWriter> providers = getAudioFileWriters();
  1219 
  1219 
  1220         for(int i=0; i < providers.size(); i++) {
  1220         for(int i=0; i < providers.size(); i++) {
  1221             AudioFileWriter writer = (AudioFileWriter) providers.get(i);
  1221             AudioFileWriter writer = providers.get(i);
  1222             if(writer.isFileTypeSupported(fileType, stream)) {
  1222             if(writer.isFileTypeSupported(fileType, stream)) {
  1223                 return true;
  1223                 return true;
  1224             }
  1224             }
  1225         }
  1225         }
  1226         return false;
  1226         return false;
  1246      * @see #getAudioFileTypes
  1246      * @see #getAudioFileTypes
  1247      */
  1247      */
  1248     public static int write(AudioInputStream stream, AudioFileFormat.Type fileType,
  1248     public static int write(AudioInputStream stream, AudioFileFormat.Type fileType,
  1249                             OutputStream out) throws IOException {
  1249                             OutputStream out) throws IOException {
  1250 
  1250 
  1251         List providers = getAudioFileWriters();
  1251         List<AudioFileWriter> providers = getAudioFileWriters();
  1252         int bytesWritten = 0;
  1252         int bytesWritten = 0;
  1253         boolean flag = false;
  1253         boolean flag = false;
  1254 
  1254 
  1255         for(int i=0; i < providers.size(); i++) {
  1255         for(int i=0; i < providers.size(); i++) {
  1256             AudioFileWriter writer = (AudioFileWriter) providers.get(i);
  1256             AudioFileWriter writer = providers.get(i);
  1257             try {
  1257             try {
  1258                 bytesWritten = writer.write( stream, fileType, out ); // throws IOException
  1258                 bytesWritten = writer.write( stream, fileType, out ); // throws IOException
  1259                 flag = true;
  1259                 flag = true;
  1260                 break;
  1260                 break;
  1261             } catch (IllegalArgumentException e) {
  1261             } catch (IllegalArgumentException e) {
  1286      * @see #getAudioFileTypes
  1286      * @see #getAudioFileTypes
  1287      */
  1287      */
  1288     public static int write(AudioInputStream stream, AudioFileFormat.Type fileType,
  1288     public static int write(AudioInputStream stream, AudioFileFormat.Type fileType,
  1289                             File out) throws IOException {
  1289                             File out) throws IOException {
  1290 
  1290 
  1291         List providers = getAudioFileWriters();
  1291         List<AudioFileWriter> providers = getAudioFileWriters();
  1292         int bytesWritten = 0;
  1292         int bytesWritten = 0;
  1293         boolean flag = false;
  1293         boolean flag = false;
  1294 
  1294 
  1295         for(int i=0; i < providers.size(); i++) {
  1295         for(int i=0; i < providers.size(); i++) {
  1296             AudioFileWriter writer = (AudioFileWriter) providers.get(i);
  1296             AudioFileWriter writer = providers.get(i);
  1297             try {
  1297             try {
  1298                 bytesWritten = writer.write( stream, fileType, out ); // throws IOException
  1298                 bytesWritten = writer.write( stream, fileType, out ); // throws IOException
  1299                 flag = true;
  1299                 flag = true;
  1300                 break;
  1300                 break;
  1301             } catch (IllegalArgumentException e) {
  1301             } catch (IllegalArgumentException e) {
  1313     // METHODS FOR INTERNAL IMPLEMENTATION USE
  1313     // METHODS FOR INTERNAL IMPLEMENTATION USE
  1314 
  1314 
  1315     /**
  1315     /**
  1316      * Obtains the set of MixerProviders on the system.
  1316      * Obtains the set of MixerProviders on the system.
  1317      */
  1317      */
  1318     private static List getMixerProviders() {
  1318     @SuppressWarnings("unchecked")
  1319         return getProviders(MixerProvider.class);
  1319     private static List<MixerProvider> getMixerProviders() {
       
  1320         return (List<MixerProvider>) getProviders(MixerProvider.class);
  1320     }
  1321     }
  1321 
  1322 
  1322     /**
  1323     /**
  1323      * Obtains the set of format converters (codecs, transcoders, etc.) that are
  1324      * Obtains the set of format converters (codecs, transcoders, etc.) that are
  1324      * currently installed on the system.
  1325      * currently installed on the system.
  1326      * @return an array of {@link javax.sound.sampled.spi.FormatConversionProvider
  1327      * @return an array of {@link javax.sound.sampled.spi.FormatConversionProvider
  1327      *         FormatConversionProvider} objects representing the available
  1328      *         FormatConversionProvider} objects representing the available
  1328      *         format converters. If no format converters readers are available
  1329      *         format converters. If no format converters readers are available
  1329      *         on the system, an array of length 0 is returned.
  1330      *         on the system, an array of length 0 is returned.
  1330      */
  1331      */
  1331     private static List getFormatConversionProviders() {
  1332     @SuppressWarnings("unchecked")
  1332         return getProviders(FormatConversionProvider.class);
  1333     private static List<FormatConversionProvider> getFormatConversionProviders() {
       
  1334         return (List<FormatConversionProvider>) getProviders(FormatConversionProvider.class);
  1333     }
  1335     }
  1334 
  1336 
  1335     /**
  1337     /**
  1336      * Obtains the set of audio file readers that are currently installed on the
  1338      * Obtains the set of audio file readers that are currently installed on the
  1337      * system.
  1339      * system.
  1339      * @return a List of {@link javax.sound.sampled.spi.AudioFileReader
  1341      * @return a List of {@link javax.sound.sampled.spi.AudioFileReader
  1340      *         AudioFileReader} objects representing the installed audio file
  1342      *         AudioFileReader} objects representing the installed audio file
  1341      *         readers. If no audio file readers are available on the system, an
  1343      *         readers. If no audio file readers are available on the system, an
  1342      *         empty List is returned.
  1344      *         empty List is returned.
  1343      */
  1345      */
  1344     private static List getAudioFileReaders() {
  1346     @SuppressWarnings("unchecked")
  1345         return getProviders(AudioFileReader.class);
  1347     private static List<AudioFileReader> getAudioFileReaders() {
       
  1348         return (List<AudioFileReader>)getProviders(AudioFileReader.class);
  1346     }
  1349     }
  1347 
  1350 
  1348     /**
  1351     /**
  1349      * Obtains the set of audio file writers that are currently installed on the
  1352      * Obtains the set of audio file writers that are currently installed on the
  1350      * system.
  1353      * system.
  1352      * @return a List of {@link javax.sound.sampled.spi.AudioFileWriter
  1355      * @return a List of {@link javax.sound.sampled.spi.AudioFileWriter
  1353      *         AudioFileWriter} objects representing the available audio file
  1356      *         AudioFileWriter} objects representing the available audio file
  1354      *         writers. If no audio file writers are available on the system, an
  1357      *         writers. If no audio file writers are available on the system, an
  1355      *         empty List is returned.
  1358      *         empty List is returned.
  1356      */
  1359      */
  1357     private static List getAudioFileWriters() {
  1360     @SuppressWarnings("unchecked")
  1358         return getProviders(AudioFileWriter.class);
  1361     private static List<AudioFileWriter> getAudioFileWriters() {
       
  1362         return (List<AudioFileWriter>)getProviders(AudioFileWriter.class);
  1359     }
  1363     }
  1360 
  1364 
  1361     /**
  1365     /**
  1362      * Attempts to locate and return a default Mixer that provides lines of the
  1366      * Attempts to locate and return a default Mixer that provides lines of the
  1363      * specified type.
  1367      * specified type.
  1366      * @param  info The requested line type TargetDataLine.class, Clip.class or
  1370      * @param  info The requested line type TargetDataLine.class, Clip.class or
  1367      *         Port.class
  1371      *         Port.class
  1368      * @return a Mixer that matches the requirements, or null if no default
  1372      * @return a Mixer that matches the requirements, or null if no default
  1369      *         mixer found
  1373      *         mixer found
  1370      */
  1374      */
  1371     private static Mixer getDefaultMixer(List providers, Line.Info info) {
  1375     private static Mixer getDefaultMixer(List<MixerProvider> providers, Line.Info info) {
  1372         Class lineClass = info.getLineClass();
  1376         Class<?> lineClass = info.getLineClass();
  1373         String providerClassName = JDK13Services.getDefaultProviderClassName(lineClass);
  1377         String providerClassName = JDK13Services.getDefaultProviderClassName(lineClass);
  1374         String instanceName = JDK13Services.getDefaultInstanceName(lineClass);
  1378         String instanceName = JDK13Services.getDefaultInstanceName(lineClass);
  1375         Mixer mixer;
  1379         Mixer mixer;
  1376 
  1380 
  1377         if (providerClassName != null) {
  1381         if (providerClassName != null) {
  1416      * @param  providerClassName The class name of the provider to be returned
  1420      * @param  providerClassName The class name of the provider to be returned
  1417      * @param  providers The list of MixerProviders that is searched
  1421      * @param  providers The list of MixerProviders that is searched
  1418      * @return A MixerProvider of the requested class, or null if none is found
  1422      * @return A MixerProvider of the requested class, or null if none is found
  1419      */
  1423      */
  1420     private static MixerProvider getNamedProvider(String providerClassName,
  1424     private static MixerProvider getNamedProvider(String providerClassName,
  1421                                                   List providers) {
  1425                                                   List<MixerProvider> providers) {
  1422         for(int i = 0; i < providers.size(); i++) {
  1426         for(int i = 0; i < providers.size(); i++) {
  1423             MixerProvider provider = (MixerProvider) providers.get(i);
  1427             MixerProvider provider = providers.get(i);
  1424             if (provider.getClass().getName().equals(providerClassName)) {
  1428             if (provider.getClass().getName().equals(providerClassName)) {
  1425                 return provider;
  1429                 return provider;
  1426             }
  1430             }
  1427         }
  1431         }
  1428         return null;
  1432         return null;
  1460      * @param  providers The List of MixerProviders to check for Mixers
  1464      * @param  providers The List of MixerProviders to check for Mixers
  1461      * @param  info The type of line the returned Mixer is required to support
  1465      * @param  info The type of line the returned Mixer is required to support
  1462      * @return A Mixer matching the requirements, or null if none is found
  1466      * @return A Mixer matching the requirements, or null if none is found
  1463      */
  1467      */
  1464     private static Mixer getNamedMixer(String mixerName,
  1468     private static Mixer getNamedMixer(String mixerName,
  1465                                        List providers,
  1469                                        List<MixerProvider> providers,
  1466                                        Line.Info info) {
  1470                                        Line.Info info) {
  1467         for(int i = 0; i < providers.size(); i++) {
  1471         for(int i = 0; i < providers.size(); i++) {
  1468             MixerProvider provider = (MixerProvider) providers.get(i);
  1472             MixerProvider provider = providers.get(i);
  1469             Mixer mixer = getNamedMixer(mixerName, provider, info);
  1473             Mixer mixer = getNamedMixer(mixerName, provider, info);
  1470             if (mixer != null) {
  1474             if (mixer != null) {
  1471                 return mixer;
  1475                 return mixer;
  1472             }
  1476             }
  1473         }
  1477         }
  1509                                               Line.Info lineInfo,
  1513                                               Line.Info lineInfo,
  1510                                               boolean isMixingRequired) {
  1514                                               boolean isMixingRequired) {
  1511         if (! mixer.isLineSupported(lineInfo)) {
  1515         if (! mixer.isLineSupported(lineInfo)) {
  1512             return false;
  1516             return false;
  1513         }
  1517         }
  1514         Class lineClass = lineInfo.getLineClass();
  1518         Class<?> lineClass = lineInfo.getLineClass();
  1515         if (isMixingRequired
  1519         if (isMixingRequired
  1516             && (SourceDataLine.class.isAssignableFrom(lineClass) ||
  1520             && (SourceDataLine.class.isAssignableFrom(lineClass) ||
  1517                 Clip.class.isAssignableFrom(lineClass))) {
  1521                 Clip.class.isAssignableFrom(lineClass))) {
  1518             int maxLines = mixer.getMaxLines(lineInfo);
  1522             int maxLines = mixer.getMaxLines(lineInfo);
  1519             return ((maxLines == NOT_SPECIFIED) || (maxLines > 1));
  1523             return ((maxLines == NOT_SPECIFIED) || (maxLines > 1));
  1522     }
  1526     }
  1523 
  1527 
  1524     /**
  1528     /**
  1525      * Like getMixerInfo, but return List.
  1529      * Like getMixerInfo, but return List.
  1526      */
  1530      */
  1527     private static List getMixerInfoList() {
  1531     private static List<Mixer.Info> getMixerInfoList() {
  1528         List providers = getMixerProviders();
  1532         List<MixerProvider> providers = getMixerProviders();
  1529         return getMixerInfoList(providers);
  1533         return getMixerInfoList(providers);
  1530     }
  1534     }
  1531 
  1535 
  1532     /**
  1536     /**
  1533      * Like getMixerInfo, but return List.
  1537      * Like getMixerInfo, but return List.
  1534      */
  1538      */
  1535     private static List getMixerInfoList(List providers) {
  1539     private static List<Mixer.Info> getMixerInfoList(List<MixerProvider> providers) {
  1536         List infos = new ArrayList();
  1540         List<Mixer.Info> infos = new ArrayList<>();
  1537 
  1541 
  1538         Mixer.Info[] someInfos; // per-mixer
  1542         Mixer.Info[] someInfos; // per-mixer
  1539         Mixer.Info[] allInfos;  // for all mixers
  1543         Mixer.Info[] allInfos;  // for all mixers
  1540 
  1544 
  1541         for(int i = 0; i < providers.size(); i++ ) {
  1545         for(int i = 0; i < providers.size(); i++ ) {
  1542             someInfos = ((MixerProvider)providers.get(i)).getMixerInfo();
  1546             someInfos = providers.get(i).getMixerInfo();
  1543 
  1547 
  1544             for (int j = 0; j < someInfos.length; j++) {
  1548             for (int j = 0; j < someInfos.length; j++) {
  1545                 infos.add(someInfos[j]);
  1549                 infos.add(someInfos[j]);
  1546             }
  1550             }
  1547         }
  1551         }
  1554      * SPI mechanism in 1.3.
  1558      * SPI mechanism in 1.3.
  1555      *
  1559      *
  1556      * @return a List of instances of providers for the requested service. If no
  1560      * @return a List of instances of providers for the requested service. If no
  1557      *         providers are available, a vector of length 0 will be returned.
  1561      *         providers are available, a vector of length 0 will be returned.
  1558      */
  1562      */
  1559     private static List getProviders(Class providerClass) {
  1563     private static List<?> getProviders(Class<?> providerClass) {
  1560         return JDK13Services.getProviders(providerClass);
  1564         return JDK13Services.getProviders(providerClass);
  1561     }
  1565     }
  1562 }
  1566 }