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.util.ArrayList;
  25 import java.util.List;
  26 
  27 import javax.sound.sampled.AudioFormat;
  28 import javax.sound.sampled.AudioFormat.Encoding;
  29 import javax.sound.sampled.AudioSystem;
  30 import javax.sound.sampled.Clip;
  31 import javax.sound.sampled.LineUnavailableException;
  32 
  33 /**
  34  * @test
  35  * @bug 8167435
  36  */
  37 public final class OpenNonIntegralNumberOfSampleframes {
  38 
  39     /**
  40      * We will try to use all formats, in this case all our providers will be
  41      * covered by supported/unsupported formats.
  42      */
  43     private static final List<AudioFormat> formats = new ArrayList<>(2900);
  44 
  45     private static final Encoding[] encodings = {
  46             Encoding.ALAW, Encoding.ULAW, Encoding.PCM_SIGNED,
  47             Encoding.PCM_UNSIGNED, Encoding.PCM_FLOAT
  48     };
  49 
  50     private static final int[] sampleRates = {
  51             8000, 11025, 16000, 32000, 44100
  52     };
  53 
  54     private static final int[] sampleBits = {
  55             4, 8, 11, 16, 20, 24, 32, 48, 64, 128
  56     };
  57 
  58     private static final int[] channels = {
  59             1, 2, 3, 4, 5, 6
  60     };
  61 
  62     static {
  63         for (final Boolean end : new boolean[]{false, true}) {
  64             for (final int sampleSize : sampleBits) {
  65                 for (final int sampleRate : sampleRates) {
  66                     for (final int channel : channels) {
  67                         final int frameSize = ((sampleSize + 7) / 8) * channel;
  68                         if (frameSize == 1) {
  69                             // frameSize=1 is ok for any buffers, skip it
  70                             continue;
  71                         }
  72                         for (final Encoding enc : encodings) {
  73                             formats.add(
  74                                     new AudioFormat(enc, sampleRate, sampleSize,
  75                                                     channel, frameSize,
  76                                                     sampleRate, end));
  77                         }
  78                     }
  79                 }
  80             }
  81         }
  82     }
  83 
  84     public static void main(final String[] args) {
  85         for (final AudioFormat af : formats) {
  86             try (Clip clip = AudioSystem.getClip()) {
  87                 final int bufferSize = af.getFrameSize() + 1;
  88                 try {
  89                     clip.open(af, new byte[100], 0, bufferSize);
  90                 } catch (final IllegalArgumentException ignored) {
  91                     // expected exception
  92                     continue;
  93                 } catch (final LineUnavailableException e) {
  94                     // should not occur, we passed incorrect bufferSize
  95                     e.printStackTrace();
  96                 }
  97                 System.err.println("af = " + af);
  98                 System.err.println("bufferSize = " + bufferSize);
  99                 throw new RuntimeException("Expected exception is not thrown");
 100             } catch (IllegalArgumentException
 101                     | LineUnavailableException ignored) {
 102                 // the test is not applicable
 103             }
 104         }
 105     }
 106 }