Path: blob/master/test/jdk/javax/sound/sampled/spi/AudioFileReader/ShortHeader.java
40527 views
/*1* Copyright (c) 2015, 2017, 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.BufferedInputStream;24import java.io.File;25import java.io.FileInputStream;26import java.io.FileOutputStream;27import java.io.IOException;28import java.io.InputStream;29import java.io.OutputStream;30import java.net.URL;31import java.nio.file.Files;32import java.nio.file.Paths;33import java.util.Arrays;3435import javax.sound.sampled.AudioSystem;36import javax.sound.sampled.UnsupportedAudioFileException;37import javax.sound.sampled.spi.AudioFileReader;3839import static java.util.ServiceLoader.load;4041/**42* @test43* @bug 813197444* @summary Short files should be reported as unsupported45*/46public final class ShortHeader {4748private static byte[] W = {-12, 3, 45};4950private static byte[] R = new byte[3];5152public static void main(final String[] args) throws Exception {53final File file = Files.createTempFile("audio", "test").toFile();54try {55try (final OutputStream fos = new FileOutputStream(file)) {56fos.write(W);57}58testAS(file);59for (final AudioFileReader afr : load(AudioFileReader.class)) {60testAFR(afr, file);61}62} finally {63Files.delete(Paths.get(file.getAbsolutePath()));64}65}6667/**68* Tests the part of AudioSystem API, which implemented via69* AudioFileReader.70*71* @see AudioSystem#getAudioFileFormat(InputStream)72* @see AudioSystem#getAudioFileFormat(File)73* @see AudioSystem#getAudioFileFormat(URL)74* @see AudioSystem#getAudioInputStream(InputStream)75* @see AudioSystem#getAudioInputStream(File)76* @see AudioSystem#getAudioInputStream(URL)77*/78private static void testAS(final File file) throws IOException {79try {80AudioSystem.getAudioFileFormat(file);81throw new RuntimeException();82} catch (final UnsupportedAudioFileException ignored) {83}84try {85AudioSystem.getAudioFileFormat(file.toURL());86throw new RuntimeException();87} catch (final UnsupportedAudioFileException ignored) {88}89try {90AudioSystem.getAudioInputStream(file);91throw new RuntimeException();92} catch (final UnsupportedAudioFileException ignored) {93}94try {95AudioSystem.getAudioInputStream(file.toURL());96throw new RuntimeException();97} catch (final UnsupportedAudioFileException ignored) {98}99100// AudioSystem.getAudioXXX(stream) should properly reset the stream101102try (FileInputStream fis = new FileInputStream(file);103InputStream stream = new BufferedInputStream(fis)) {104105try {106AudioSystem.getAudioFileFormat(stream);107throw new RuntimeException();108} catch (final UnsupportedAudioFileException ignored) {109}110try {111AudioSystem.getAudioInputStream(stream);112throw new RuntimeException();113} catch (final UnsupportedAudioFileException ignored) {114}115stream.read(R, 0, R.length);116}117118if (!Arrays.equals(R, W)) {119System.err.println("Expected = " + Arrays.toString(W));120System.err.println("Actual = " + Arrays.toString(R));121throw new RuntimeException();122}123}124/**125* Tests the AudioFileReader API directly.126*127* @see AudioFileReader#getAudioFileFormat(InputStream)128* @see AudioFileReader#getAudioFileFormat(File)129* @see AudioFileReader#getAudioFileFormat(URL)130* @see AudioFileReader#getAudioInputStream(InputStream)131* @see AudioFileReader#getAudioInputStream(File)132* @see AudioFileReader#getAudioInputStream(URL)133*/134private static void testAFR(final AudioFileReader fcp, final File file)135throws Exception {136try {137fcp.getAudioFileFormat(file);138throw new RuntimeException();139} catch (final UnsupportedAudioFileException ignored) {140}141try {142fcp.getAudioFileFormat(file.toURL());143throw new RuntimeException();144} catch (final UnsupportedAudioFileException ignored) {145}146try {147fcp.getAudioInputStream(file);148throw new RuntimeException();149} catch (final UnsupportedAudioFileException ignored) {150}151try {152fcp.getAudioInputStream(file.toURL());153throw new RuntimeException();154} catch (final UnsupportedAudioFileException ignored) {155}156157// AudioFileReader should properly reset the stream158159try (FileInputStream fis = new FileInputStream(file);160InputStream stream = new BufferedInputStream(fis)) {161162try {163fcp.getAudioFileFormat(stream);164throw new RuntimeException();165} catch (final UnsupportedAudioFileException ignored) {166}167try {168fcp.getAudioInputStream(stream);169throw new RuntimeException();170} catch (final UnsupportedAudioFileException ignored) {171}172stream.read(R, 0, R.length);173}174175if (!Arrays.equals(R, W)) {176System.err.println("Expected = " + Arrays.toString(W));177System.err.println("Actual = " + Arrays.toString(R));178throw new RuntimeException();179}180}181}182183184