Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/sampled/FileWriter/WriterCloseInput.java
38855 views
/*1* Copyright (c) 2011, 2013, 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 javax.sound.sampled.AudioFileFormat;36import javax.sound.sampled.AudioFormat;37import javax.sound.sampled.AudioInputStream;38import javax.sound.sampled.AudioSystem;394041public class WriterCloseInput {4243final static AudioFormat audioFormat = new AudioFormat(44100f, 16, 2, true, true);44//final static AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.ULAW, 44100f, 8, 2, 2, 44100f, true);45final static int frameLength = 44100 * 2; // 2 seconds46final static byte[] dataBuffer47= new byte[frameLength * (audioFormat.getSampleSizeInBits()/8)48* audioFormat.getChannels()];4950static int testTotal = 0;51static int testFailed = 0;5253public static void main(String[] args) throws Exception {54test(AudioFileFormat.Type.AIFF);55test(AudioFileFormat.Type.AU);56test(AudioFileFormat.Type.WAVE);5758if (testFailed == 0) {59out("All tests passed.");60} else {61out("" + testFailed + " of " + testTotal + " tests FAILED.");62System.out.flush();63throw new RuntimeException("Test FAILED.");64}65}6667static void test(AudioFileFormat.Type fileType) {68test(fileType, frameLength);69test(fileType, AudioSystem.NOT_SPECIFIED);70}7172static void test(AudioFileFormat.Type fileType, int length) {73test(fileType, length, false);74test(fileType, length, true);75}7677static void test(AudioFileFormat.Type fileType, int length, boolean isFile) {78testTotal++;79out("Testing fileType: " + fileType80+ ", frameLength: " + (length >= 0 ? length : "unspecified")81+ ", output: " + (isFile ? "File" : "OutputStream"));82AudioInputStream inStream = new ThrowAfterCloseStream(83new ByteArrayInputStream(dataBuffer), audioFormat, length);8485AudioSystem.isFileTypeSupported(fileType, inStream);8687try {88if (isFile) {89File f = File.createTempFile("WriterCloseInput" + testTotal, "tmp");90AudioSystem.write(inStream, fileType, f);91f.delete();92} else {93OutputStream outStream = new NullOutputStream();94AudioSystem.write(inStream, fileType, outStream);95}96} catch (Exception ex) {97// this is not failure98out("SKIPPED (AudioSystem.write exception): " + ex.getMessage());99//out(ex);100inStream = null;101}102103if (inStream != null) {104try {105// test if the stream is closed106inStream.available();107out("PASSED");108} catch (IOException ex) {109testFailed++;110out("FAILED: " + ex.getMessage());111//out(ex);112}113}114out("");115}116117static class ThrowAfterCloseStream extends AudioInputStream {118private boolean closed = false;119public ThrowAfterCloseStream(InputStream in, AudioFormat format, long length) {120super(in, format, length);121}122@Override123public void close() {124closed = true;125}126@Override127public int available() throws IOException {128if (closed) {129throw new IOException("The stream has been closed");130}131return 1;132}133}134135static class NullOutputStream extends OutputStream {136@Override137public void write(int b) throws IOException {138// nop139}140}141142static void out(String s) {143System.out.println(s);144}145146static void out(Exception ex) {147ex.printStackTrace(System.out);148}149}150151152