1 /*
   2  * Copyright (c) 2002, 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.File;
  26 import java.io.FileInputStream;
  27 import java.io.InputStream;
  28 import java.net.URL;
  29 
  30 import javax.sound.sampled.AudioFileFormat;
  31 import javax.sound.sampled.AudioSystem;
  32 
  33 /**
  34  * @test
  35  * @bug 4489272
  36  * @summary AudioSystem.getAudioFileFormat() fails for InputStream, but works
  37  *          for URL
  38  */
  39 public class OpenWaveFile {
  40 
  41     static void check(Object source) throws Exception {
  42          AudioFileFormat aff2 = null;
  43          if (source instanceof File) {
  44             aff2 = AudioSystem.getAudioFileFormat((File) source);
  45          }
  46          else if (source instanceof InputStream) {
  47             aff2 = AudioSystem.getAudioFileFormat((InputStream) source);
  48          }
  49          else if (source instanceof URL) {
  50             aff2 = AudioSystem.getAudioFileFormat((URL) source);
  51          } else throw new Exception("wrong source. Test FAILED");
  52          System.out.println("Got: "+aff2);
  53          if (aff2.getFormat().getSampleSizeInBits()==-1) {
  54             throw new Exception("wrong audio format. Test FAILED");
  55          }
  56     }
  57 
  58     public static void main(String args[]) throws Exception {
  59          //check(new File(args[0]));
  60          //check(new URL("file", "", args[0]));
  61          check(new ByteArrayInputStream(SHORT_AU));
  62          check(new ByteArrayInputStream(SHORT_WAVE));
  63          check(new ByteArrayInputStream(SHORT_AIFF));
  64          System.out.println("Test passed.");
  65 
  66          //printFile(args[0]);
  67      }
  68 
  69     public static String getString(byte b) {
  70         //String res = Integer.toHexString(b & 0xFF).toUpperCase();
  71         //while (res.length()<2) res="0"+res;
  72         //return res;
  73         return String.valueOf(b);
  74     }
  75 
  76 
  77     public static void printFile(String filename) throws Exception {
  78         File file = new File(filename);
  79         FileInputStream fis = new FileInputStream(file);
  80         byte[] data = new byte[(int) file.length()];
  81         fis.read(data);
  82         String s = "";
  83         for (int i=0; i<data.length; i++) {
  84             s+=getString(data[i])+", ";
  85             if (s.length()>72) {
  86                 System.out.println(s);
  87                 s="";
  88             }
  89         }
  90         System.out.println(s);
  91     }
  92 
  93     public static byte[] SHORT_WAVE = {
  94         82, 73, 70, 70, -120, 0, 0, 0, 87, 65, 86, 69, 102, 109, 116, 32, 16, 0,
  95         0, 0, 1, 0, 1, 0, 34, 86, 0, 0, 34, 86, 0, 0, 1, 0, 8, 0, 100, 97, 116, 97,
  96         100, 0, 0, 0, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,
  97         -128, -128, -128, -128, -128, -128, -128, -128, -128, 127, 127, -128, 127,
  98         127, 127, -128, -128, -128, -128, 127, 127, -128, -128, 127, -128, -128,
  99         -128, 127, 127, 127, -128, -128, -128, 127, 127, 127, 127, -128, -128, -128,
 100         -128, -128, -128, 127, 127, 127, -128, -128, -128, -128, -128, 127, -128,
 101         -128, 127, -128, -128, 127, 127, -128, -128, 127, 127, -128, -128, -128,
 102         -128, -128, 127, 127, -128, -128, -128, 127, 127, 127, -128, 127, -128, -128,
 103         127, 127, 127, -128, -128, -128, 127, 127, -128, -128,
 104     };
 105 
 106     public static byte[] SHORT_AU = {
 107         46, 115, 110, 100, 0, 0, 0, 24, 0, 0, 0, 100, 0, 0, 0, 2, 0, 0, 86, 34, 0,
 108         0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1,
 109         0, -1, -1, -1, 0, 0, 0, 0, -1, -1, 0, 0, -1, 0, 0, 0, -1, -1, -1, 0, 0, 0,
 110         -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0, 0, 0, -1, 0, 0, -1,
 111         0, 0, -1, -1, 0, 0, -1, -1, 0, 0, 0, 0, 0, -1, -1, 0, 0, 0, -1, -1, -1, 0,
 112         -1, 0, 0, -1, -1, -1, 0, 0, 0, -1, -1, 0, 0,
 113     };
 114 
 115     public static byte[] SHORT_AIFF = {
 116         70, 79, 82, 77, 0, 0, 0, -110, 65, 73, 70, 70, 67, 79, 77, 77, 0, 0, 0, 18,
 117         0, 1, 0, 0, 0, 100, 0, 8, 64, 13, -84, 68, 0, 0, 0, 0, 0, 0, 83, 83, 78,
 118         68, 0, 0, 0, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 119         0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 0, -1, -1, -1, 0, 0, 0, 0, -1, -1, 0, 0,
 120         -1, 0, 0, 0, -1, -1, -1, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, -1, -1,
 121         -1, 0, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, -1, 0, 0, -1, -1, 0, 0, 0, 0,
 122         0, -1, -1, 0, 0, 0, -1, -1, -1, 0, -1, 0, 0, -1, -1, -1, 0, 0, 0, -1, -1,
 123         0, 0,
 124     };
 125 }