jdk/test/javax/sound/sampled/spi/AudioFileWriter/WriteUnsupportedAudioFormat.java
changeset 35686 1c21a27682a5
child 40718 fe2adbe4d101
equal deleted inserted replaced
35685:f405bbd07cc6 35686:1c21a27682a5
       
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.io.ByteArrayInputStream;
       
    25 import java.io.ByteArrayOutputStream;
       
    26 import java.io.File;
       
    27 import java.io.IOException;
       
    28 import java.io.InputStream;
       
    29 import java.io.OutputStream;
       
    30 import java.util.ArrayList;
       
    31 import java.util.List;
       
    32 
       
    33 import javax.sound.sampled.AudioFileFormat;
       
    34 import javax.sound.sampled.AudioFileFormat.Type;
       
    35 import javax.sound.sampled.AudioFormat;
       
    36 import javax.sound.sampled.AudioInputStream;
       
    37 import javax.sound.sampled.AudioSystem;
       
    38 import javax.sound.sampled.spi.AudioFileWriter;
       
    39 
       
    40 import static java.util.ServiceLoader.load;
       
    41 import static javax.sound.sampled.AudioFileFormat.Type.AIFC;
       
    42 import static javax.sound.sampled.AudioFileFormat.Type.AIFF;
       
    43 import static javax.sound.sampled.AudioFileFormat.Type.AU;
       
    44 import static javax.sound.sampled.AudioFileFormat.Type.SND;
       
    45 import static javax.sound.sampled.AudioFileFormat.Type.WAVE;
       
    46 
       
    47 /**
       
    48  * @test
       
    49  * @bug 8064800
       
    50  */
       
    51 public final class WriteUnsupportedAudioFormat {
       
    52 
       
    53     /**
       
    54      * We will try to use all formats, in this case all our providers will be
       
    55      * covered by supported/unsupported formats.
       
    56      */
       
    57     private static final List<AudioFormat> formats = new ArrayList<>(23000);
       
    58 
       
    59     private static final AudioFormat.Encoding[] encodings = {
       
    60             AudioFormat.Encoding.ALAW, AudioFormat.Encoding.ULAW,
       
    61             AudioFormat.Encoding.PCM_SIGNED, AudioFormat.Encoding.PCM_UNSIGNED,
       
    62             AudioFormat.Encoding.PCM_FLOAT, new AudioFormat.Encoding("Test")
       
    63     };
       
    64 
       
    65     private static final int[] sampleRates = {
       
    66             /*AudioSystem.NOT_SPECIFIED,*/ 8000, 11025, 16000, 22050, 32000,
       
    67             37800, 44056, 44100, 47250, 48000, 50000, 50400, 88200, 96000,
       
    68             176400, 192000, 352800, 2822400, 5644800
       
    69     };
       
    70 
       
    71     private static final int[] sampleBits = {
       
    72             /*AudioSystem.NOT_SPECIFIED, 4,*/ 8,/* 11,*/ 16/*, 20*/, 24,
       
    73             32/*, 48, 64, 128*/
       
    74     };
       
    75 
       
    76     public static final int BUFFER_LEN = 127;
       
    77 
       
    78     private static final int[] channels = {
       
    79             /*AudioSystem.NOT_SPECIFIED,*/ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
       
    80     };
       
    81 
       
    82     static final Type[] types = {
       
    83             WAVE, AU, AIFF, AIFC, SND, new Type("TestName", "TestExt")
       
    84     };
       
    85 
       
    86     private static final File FILE;
       
    87 
       
    88     static {
       
    89         try {
       
    90             FILE = File.createTempFile("sound", null);
       
    91         } catch (final IOException e) {
       
    92             throw new RuntimeException(e);
       
    93         }
       
    94         FILE.deleteOnExit();
       
    95 
       
    96         for (final Boolean end : new boolean[]{false, true}) {
       
    97             for (final int sampleSize : sampleBits) {
       
    98                 for (final int sampleRate : sampleRates) {
       
    99                     for (final int channel : channels) {
       
   100                         for (final AudioFormat.Encoding enc : encodings) {
       
   101 
       
   102                             if (enc.equals(AudioFormat.Encoding.PCM_FLOAT)
       
   103                                     && sampleSize != 32) {
       
   104                                 continue;
       
   105                             }
       
   106                             if (enc.equals(AudioFormat.Encoding.ALAW)
       
   107                                     && sampleSize != 8) {
       
   108                                 continue;
       
   109                             }
       
   110                             if (enc.equals(AudioFormat.Encoding.ULAW)
       
   111                                     && sampleSize != 8) {
       
   112                                 continue;
       
   113                             }
       
   114 
       
   115                             final int frameSize = ((sampleSize + 7) / 8)
       
   116                                     * channel;
       
   117                             formats.add(
       
   118                                     new AudioFormat(enc, sampleRate, sampleSize,
       
   119                                                     channel, frameSize,
       
   120                                                     sampleRate, end));
       
   121                         }
       
   122                     }
       
   123                 }
       
   124             }
       
   125         }
       
   126     }
       
   127 
       
   128     public static void main(final String[] args) throws Exception {
       
   129         for (final AudioFileFormat.Type type : types) {
       
   130             for (final AudioFormat format : formats) {
       
   131                 testAS(type, format);
       
   132                 for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
       
   133                     testAFW(afw, type, format);
       
   134                 }
       
   135             }
       
   136         }
       
   137     }
       
   138 
       
   139     /**
       
   140      * Tests the part of AudioSystem API, which implemented via AudioFileWriter.
       
   141      */
       
   142     private static void testAS(final AudioFileFormat.Type type,
       
   143                                final AudioFormat format) throws Exception {
       
   144         final AudioInputStream ais = getStream(format);
       
   145         final OutputStream buffer = new ByteArrayOutputStream(BUFFER_LEN);
       
   146 
       
   147         if (AudioSystem.isFileTypeSupported(type, ais)) {
       
   148             if (!AudioSystem.isFileTypeSupported(type)) {
       
   149                 throw new RuntimeException(type + ", " + format);
       
   150             }
       
   151             try {
       
   152                 AudioSystem.write(ais, type, buffer);
       
   153                 AudioSystem.write(ais, type, FILE);
       
   154             } catch (final IllegalArgumentException e) {
       
   155                 throw new RuntimeException(type + ", " + format, e);
       
   156             }
       
   157         } else {
       
   158             try {
       
   159                 AudioSystem.write(ais, type, buffer);
       
   160                 throw new RuntimeException(type + ", " + format);
       
   161             } catch (final IllegalArgumentException ignored) {
       
   162             }
       
   163             try {
       
   164                 AudioSystem.write(ais, type, FILE);
       
   165                 throw new RuntimeException(type + ", " + format);
       
   166             } catch (final IllegalArgumentException ignored) {
       
   167             }
       
   168         }
       
   169     }
       
   170 
       
   171     /**
       
   172      * Tests the AudioFileWriter API directly.
       
   173      */
       
   174     private static void testAFW(final AudioFileWriter afw,
       
   175                                 final AudioFileFormat.Type type,
       
   176                                 final AudioFormat format) throws Exception {
       
   177         final AudioInputStream ais = getStream(format);
       
   178         final OutputStream buffer = new ByteArrayOutputStream(BUFFER_LEN);
       
   179 
       
   180         if (afw.isFileTypeSupported(type, ais)) {
       
   181             if (!afw.isFileTypeSupported(type)) {
       
   182                 throw new RuntimeException(type + "," + format + ',' + afw);
       
   183             }
       
   184             try {
       
   185                 afw.write(ais, type, buffer);
       
   186                 afw.write(ais, type, FILE);
       
   187             } catch (final IllegalArgumentException e) {
       
   188                 throw new RuntimeException(type + "," + format + ',' + afw, e);
       
   189             }
       
   190         } else {
       
   191             try {
       
   192                 afw.write(ais, type, buffer);
       
   193                 throw new RuntimeException(type + "," + format + ',' + afw);
       
   194             } catch (final IllegalArgumentException ignored) {
       
   195             }
       
   196             try {
       
   197                 afw.write(ais, type, FILE);
       
   198                 throw new RuntimeException(type + "," + format + ',' + afw);
       
   199             } catch (final IllegalArgumentException ignored) {
       
   200             }
       
   201         }
       
   202     }
       
   203 
       
   204     private static AudioInputStream getStream(final AudioFormat format) {
       
   205         final InputStream in = new ByteArrayInputStream(new byte[BUFFER_LEN]);
       
   206         return new AudioInputStream(in, format, 10);
       
   207     }
       
   208 }