Path: blob/master/test/jdk/javax/sound/sampled/spi/AudioFileWriter/WriterCloseInput.java
41159 views
/*1* Copyright (c) 2011, 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*/2223/**24* @test25* @bug 701352126* @summary AIFF/AU/WAVE writers close input audio stream27* @author Alex Menkov28*/2930import java.io.ByteArrayInputStream;31import java.io.File;32import java.io.IOException;33import java.io.InputStream;34import java.io.OutputStream;35import java.nio.file.Files;36import java.nio.file.Paths;3738import javax.sound.sampled.AudioFileFormat;39import javax.sound.sampled.AudioFormat;40import javax.sound.sampled.AudioInputStream;41import javax.sound.sampled.AudioSystem;424344public class WriterCloseInput {4546final static AudioFormat audioFormat = new AudioFormat(44100f, 16, 2, true, true);47//final static AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.ULAW, 44100f, 8, 2, 2, 44100f, true);48final static int frameLength = 44100 * 2; // 2 seconds49final static byte[] dataBuffer50= new byte[frameLength * (audioFormat.getSampleSizeInBits()/8)51* audioFormat.getChannels()];5253static int testTotal = 0;54static int testFailed = 0;5556public static void main(String[] args) throws Exception {57test(AudioFileFormat.Type.AIFF);58test(AudioFileFormat.Type.AU);59test(AudioFileFormat.Type.WAVE);6061if (testFailed == 0) {62out("All tests passed.");63} else {64out("" + testFailed + " of " + testTotal + " tests FAILED.");65System.out.flush();66throw new RuntimeException("Test FAILED.");67}68}6970static void test(AudioFileFormat.Type fileType) {71test(fileType, frameLength);72test(fileType, AudioSystem.NOT_SPECIFIED);73}7475static void test(AudioFileFormat.Type fileType, int length) {76test(fileType, length, false);77test(fileType, length, true);78}7980static void test(AudioFileFormat.Type fileType, int length, boolean isFile) {81testTotal++;82out("Testing fileType: " + fileType83+ ", frameLength: " + (length >= 0 ? length : "unspecified")84+ ", output: " + (isFile ? "File" : "OutputStream"));85AudioInputStream inStream = new ThrowAfterCloseStream(86new ByteArrayInputStream(dataBuffer), audioFormat, length);8788AudioSystem.isFileTypeSupported(fileType, inStream);8990try {91if (isFile) {92File f = File.createTempFile("WriterCloseInput" + testTotal, "tmp");93AudioSystem.write(inStream, fileType, f);94Files.delete(Paths.get(f.getAbsolutePath()));95} else {96OutputStream outStream = new NullOutputStream();97AudioSystem.write(inStream, fileType, outStream);98}99} catch (Exception ex) {100// this is not failure101out("SKIPPED (AudioSystem.write exception): " + ex.getMessage());102//out(ex);103inStream = null;104}105106if (inStream != null) {107try {108// test if the stream is closed109inStream.available();110out("PASSED");111} catch (IOException ex) {112testFailed++;113out("FAILED: " + ex.getMessage());114//out(ex);115}116}117out("");118}119120static class ThrowAfterCloseStream extends AudioInputStream {121private boolean closed = false;122public ThrowAfterCloseStream(InputStream in, AudioFormat format, long length) {123super(in, format, length);124}125@Override126public void close() {127closed = true;128}129@Override130public int available() throws IOException {131if (closed) {132throw new IOException("The stream has been closed");133}134return 1;135}136}137138static class NullOutputStream extends OutputStream {139@Override140public void write(int b) throws IOException {141// nop142}143}144145static void out(String s) {146System.out.println(s);147}148149static void out(Exception ex) {150ex.printStackTrace(System.out);151}152}153154155