Path: blob/master/test/jdk/javax/sound/sampled/spi/AudioFileReader/OpenWaveFile.java
40527 views
/*1* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayInputStream;24import java.io.File;25import java.io.FileInputStream;26import java.io.InputStream;27import java.net.URL;2829import javax.sound.sampled.AudioFileFormat;30import javax.sound.sampled.AudioSystem;3132/**33* @test34* @bug 448927235* @summary AudioSystem.getAudioFileFormat() fails for InputStream, but works36* for URL37*/38public class OpenWaveFile {3940static void check(Object source) throws Exception {41AudioFileFormat aff2 = null;42if (source instanceof File) {43aff2 = AudioSystem.getAudioFileFormat((File) source);44}45else if (source instanceof InputStream) {46aff2 = AudioSystem.getAudioFileFormat((InputStream) source);47}48else if (source instanceof URL) {49aff2 = AudioSystem.getAudioFileFormat((URL) source);50} else throw new Exception("wrong source. Test FAILED");51System.out.println("Got: "+aff2);52if (aff2.getFormat().getSampleSizeInBits()==-1) {53throw new Exception("wrong audio format. Test FAILED");54}55}5657public static void main(String args[]) throws Exception {58//check(new File(args[0]));59//check(new URL("file", "", args[0]));60check(new ByteArrayInputStream(SHORT_AU));61check(new ByteArrayInputStream(SHORT_WAVE));62check(new ByteArrayInputStream(SHORT_AIFF));63System.out.println("Test passed.");6465//printFile(args[0]);66}6768public static String getString(byte b) {69//String res = Integer.toHexString(b & 0xFF).toUpperCase();70//while (res.length()<2) res="0"+res;71//return res;72return String.valueOf(b);73}747576public static void printFile(String filename) throws Exception {77File file = new File(filename);78FileInputStream fis = new FileInputStream(file);79byte[] data = new byte[(int) file.length()];80fis.read(data);81String s = "";82for (int i=0; i<data.length; i++) {83s+=getString(data[i])+", ";84if (s.length()>72) {85System.out.println(s);86s="";87}88}89System.out.println(s);90}9192public static byte[] SHORT_WAVE = {9382, 73, 70, 70, -120, 0, 0, 0, 87, 65, 86, 69, 102, 109, 116, 32, 16, 0,940, 0, 1, 0, 1, 0, 34, 86, 0, 0, 34, 86, 0, 0, 1, 0, 8, 0, 100, 97, 116, 97,95100, 0, 0, 0, -128, -128, -128, -128, -128, -128, -128, -128, -128, -128,96-128, -128, -128, -128, -128, -128, -128, -128, -128, 127, 127, -128, 127,97127, 127, -128, -128, -128, -128, 127, 127, -128, -128, 127, -128, -128,98-128, 127, 127, 127, -128, -128, -128, 127, 127, 127, 127, -128, -128, -128,99-128, -128, -128, 127, 127, 127, -128, -128, -128, -128, -128, 127, -128,100-128, 127, -128, -128, 127, 127, -128, -128, 127, 127, -128, -128, -128,101-128, -128, 127, 127, -128, -128, -128, 127, 127, 127, -128, 127, -128, -128,102127, 127, 127, -128, -128, -128, 127, 127, -128, -128,103};104105public static byte[] SHORT_AU = {10646, 115, 110, 100, 0, 0, 0, 24, 0, 0, 0, 100, 0, 0, 0, 2, 0, 0, 86, 34, 0,1070, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1,1080, -1, -1, -1, 0, 0, 0, 0, -1, -1, 0, 0, -1, 0, 0, 0, -1, -1, -1, 0, 0, 0,109-1, -1, -1, -1, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0, 0, 0, -1, 0, 0, -1,1100, 0, -1, -1, 0, 0, -1, -1, 0, 0, 0, 0, 0, -1, -1, 0, 0, 0, -1, -1, -1, 0,111-1, 0, 0, -1, -1, -1, 0, 0, 0, -1, -1, 0, 0,112};113114public static byte[] SHORT_AIFF = {11570, 79, 82, 77, 0, 0, 0, -110, 65, 73, 70, 70, 67, 79, 77, 77, 0, 0, 0, 18,1160, 1, 0, 0, 0, 100, 0, 8, 64, 13, -84, 68, 0, 0, 0, 0, 0, 0, 83, 83, 78,11768, 0, 0, 0, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,1180, 0, 0, 0, 0, 0, 0, 0, -1, -1, 0, -1, -1, -1, 0, 0, 0, 0, -1, -1, 0, 0,119-1, 0, 0, 0, -1, -1, -1, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, -1, -1,120-1, 0, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, -1, 0, 0, -1, -1, 0, 0, 0, 0,1210, -1, -1, 0, 0, 0, -1, -1, -1, 0, -1, 0, 0, -1, -1, -1, 0, 0, 0, -1, -1,1220, 0,123};124}125126127