< prev index next >

test/javax/sound/sampled/spi/AudioFileReader/ShortHeader.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 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.BufferedInputStream;
  25 import java.io.File;
  26 import java.io.FileInputStream;
  27 import java.io.FileOutputStream;
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.io.OutputStream;
  31 import java.net.URL;
  32 import java.nio.file.Files;

  33 import java.util.Arrays;
  34 
  35 import javax.sound.sampled.AudioSystem;
  36 import javax.sound.sampled.UnsupportedAudioFileException;
  37 import javax.sound.sampled.spi.AudioFileReader;
  38 
  39 import static java.util.ServiceLoader.load;
  40 
  41 /**
  42  * @test
  43  * @bug 8131974
  44  * @summary Short files should be reported as unsupported
  45  */
  46 public final class ShortHeader {
  47 
  48     private static byte[] W = {-12, 3, 45};
  49 
  50     private static byte[] R = new byte[3];
  51 
  52     public static void main(final String[] args) throws Exception {
  53         final File file = Files.createTempFile("audio", "test").toFile();
  54         file.deleteOnExit();
  55         try (final OutputStream fos = new FileOutputStream(file)) {
  56             fos.write(W);
  57         }
  58 
  59         testAS(file);
  60         for (final AudioFileReader afr : load(AudioFileReader.class)) {
  61             testAFR(afr, file);
  62         }



  63     }
  64 
  65     /**
  66      * Tests the part of AudioSystem API, which implemented via
  67      * AudioFileReader.
  68      *
  69      * @see AudioSystem#getAudioFileFormat(InputStream)
  70      * @see AudioSystem#getAudioFileFormat(File)
  71      * @see AudioSystem#getAudioFileFormat(URL)
  72      * @see AudioSystem#getAudioInputStream(InputStream)
  73      * @see AudioSystem#getAudioInputStream(File)
  74      * @see AudioSystem#getAudioInputStream(URL)
  75      */
  76     private static void testAS(final File file) throws IOException {
  77         try {
  78             AudioSystem.getAudioFileFormat(file);
  79             throw new RuntimeException();
  80         } catch (final UnsupportedAudioFileException ignored) {
  81         }
  82         try {


   1 /*
   2  * Copyright (c) 2015, 2017, 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.BufferedInputStream;
  25 import java.io.File;
  26 import java.io.FileInputStream;
  27 import java.io.FileOutputStream;
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.io.OutputStream;
  31 import java.net.URL;
  32 import java.nio.file.Files;
  33 import java.nio.file.Paths;
  34 import java.util.Arrays;
  35 
  36 import javax.sound.sampled.AudioSystem;
  37 import javax.sound.sampled.UnsupportedAudioFileException;
  38 import javax.sound.sampled.spi.AudioFileReader;
  39 
  40 import static java.util.ServiceLoader.load;
  41 
  42 /**
  43  * @test
  44  * @bug 8131974
  45  * @summary Short files should be reported as unsupported
  46  */
  47 public final class ShortHeader {
  48 
  49     private static byte[] W = {-12, 3, 45};
  50 
  51     private static byte[] R = new byte[3];
  52 
  53     public static void main(final String[] args) throws Exception {
  54         final File file = Files.createTempFile("audio", "test").toFile();
  55         try {
  56             try (final OutputStream fos = new FileOutputStream(file)) {
  57                 fos.write(W);
  58             }

  59             testAS(file);
  60             for (final AudioFileReader afr : load(AudioFileReader.class)) {
  61                 testAFR(afr, file);
  62             }
  63         } finally {
  64             Files.delete(Paths.get(file.getAbsolutePath()));
  65         }
  66     }
  67 
  68     /**
  69      * Tests the part of AudioSystem API, which implemented via
  70      * AudioFileReader.
  71      *
  72      * @see AudioSystem#getAudioFileFormat(InputStream)
  73      * @see AudioSystem#getAudioFileFormat(File)
  74      * @see AudioSystem#getAudioFileFormat(URL)
  75      * @see AudioSystem#getAudioInputStream(InputStream)
  76      * @see AudioSystem#getAudioInputStream(File)
  77      * @see AudioSystem#getAudioInputStream(URL)
  78      */
  79     private static void testAS(final File file) throws IOException {
  80         try {
  81             AudioSystem.getAudioFileFormat(file);
  82             throw new RuntimeException();
  83         } catch (final UnsupportedAudioFileException ignored) {
  84         }
  85         try {


< prev index next >