1 /* 2 * Copyright (c) 2011, 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 /** 25 * @test 26 * @bug 7013521 27 * @summary AIFF/AU/WAVE writers close input audio stream 28 * @author Alex Menkov 29 */ 30 31 import java.io.ByteArrayInputStream; 32 import java.io.File; 33 import java.io.IOException; 34 import java.io.InputStream; 35 import java.io.OutputStream; 36 import javax.sound.sampled.AudioFileFormat; 37 import javax.sound.sampled.AudioFormat; 38 import javax.sound.sampled.AudioInputStream; 39 import javax.sound.sampled.AudioSystem; 40 41 42 public class WriterCloseInput { 43 44 final static AudioFormat audioFormat = new AudioFormat(44100f, 16, 2, true, true); 45 //final static AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.ULAW, 44100f, 8, 2, 2, 44100f, true); 46 final static int frameLength = 44100 * 2; // 2 seconds 47 final static byte[] dataBuffer 48 = new byte[frameLength * (audioFormat.getSampleSizeInBits()/8) 49 * audioFormat.getChannels()]; 50 51 static int testTotal = 0; 52 static int testFailed = 0; 53 54 public static void main(String[] args) throws Exception { 55 test(AudioFileFormat.Type.AIFF); 56 test(AudioFileFormat.Type.AU); 57 test(AudioFileFormat.Type.WAVE); 58 59 if (testFailed == 0) { 60 out("All tests passed."); 61 } else { 62 out("" + testFailed + " of " + testTotal + " tests FAILED."); 63 System.out.flush(); 64 throw new RuntimeException("Test FAILED."); 65 } 66 } 67 68 static void test(AudioFileFormat.Type fileType) { 69 test(fileType, frameLength); 70 test(fileType, AudioSystem.NOT_SPECIFIED); 71 } 72 73 static void test(AudioFileFormat.Type fileType, int length) { 74 test(fileType, length, false); 75 test(fileType, length, true); 76 } 77 78 static void test(AudioFileFormat.Type fileType, int length, boolean isFile) { 79 testTotal++; 80 out("Testing fileType: " + fileType 81 + ", frameLength: " + (length >= 0 ? length : "unspecified") 82 + ", output: " + (isFile ? "File" : "OutputStream")); 83 AudioInputStream inStream = new ThrowAfterCloseStream( 84 new ByteArrayInputStream(dataBuffer), audioFormat, length); 85 86 AudioSystem.isFileTypeSupported(fileType, inStream); 87 88 try { 89 if (isFile) { 90 File f = File.createTempFile("WriterCloseInput" + testTotal, "tmp"); 91 AudioSystem.write(inStream, fileType, f); 92 f.delete(); 93 } else { 94 OutputStream outStream = new NullOutputStream(); 95 AudioSystem.write(inStream, fileType, outStream); 96 } 97 } catch (Exception ex) { 98 // this is not failure 99 out("SKIPPED (AudioSystem.write exception): " + ex.getMessage()); 100 //out(ex); 101 inStream = null; 102 } 103 104 if (inStream != null) { 105 try { 106 // test if the stream is closed 107 inStream.available(); 108 out("PASSED"); 109 } catch (IOException ex) { 110 testFailed++; 111 out("FAILED: " + ex.getMessage()); 112 //out(ex); 113 } 114 } 115 out(""); 116 } 117 118 static class ThrowAfterCloseStream extends AudioInputStream { 119 private boolean closed = false; 120 public ThrowAfterCloseStream(InputStream in, AudioFormat format, long length) { 121 super(in, format, length); 122 } 123 @Override 124 public void close() { 125 closed = true; 126 } 127 @Override 128 public int available() throws IOException { 129 if (closed) { 130 throw new IOException("The stream has been closed"); 131 } 132 return 1; 133 } 134 } 135 136 static class NullOutputStream extends OutputStream { 137 @Override 138 public void write(int b) throws IOException { 139 // nop 140 } 141 } 142 143 static void out(String s) { 144 System.out.println(s); 145 } 146 147 static void out(Exception ex) { 148 ex.printStackTrace(System.out); 149 } 150 }