Path: blob/master/test/jdk/javax/sound/sampled/spi/AudioFileWriter/RIFFHeader.java
41159 views
/*1* Copyright (c) 2006, 2016, 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.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.InputStream;2627import javax.sound.sampled.AudioFileFormat;28import javax.sound.sampled.AudioFormat;29import javax.sound.sampled.AudioInputStream;30import javax.sound.sampled.AudioSystem;3132/**33* @test34* @bug 463635535* @summary Check that RIFF headers are written with extra data length field.36*/37public class RIFFHeader {3839public static void main(String args[]) throws Exception {40System.out.println();41System.out.println();42System.out.println("4636355: Check that RIFF headers are written with extra data length field.");43byte[] fakedata=new byte[1234];44MyByteArrayInputStream is = new MyByteArrayInputStream(fakedata);45AudioFormat inFormat = new AudioFormat(AudioFormat.Encoding.ULAW, 8000, 8, 1, 1, 8000, true);4647AudioInputStream ais = new AudioInputStream((InputStream) is, inFormat, fakedata.length);48ByteArrayOutputStream out = new ByteArrayOutputStream(1500);49System.out.println(" ulaw data will be written as WAVE to stream...");50int t = AudioSystem.write(ais, AudioFileFormat.Type.WAVE, out);51byte[] writtenData = out.toByteArray();52// now header must have at least 46 bytes53System.out.println(" Length should be "+(fakedata.length+46)+" bytes: "+writtenData.length);54// re-read this file55is = new MyByteArrayInputStream(writtenData);56System.out.println(" Get AudioFileFormat of written file");57AudioFileFormat fileformat = AudioSystem.getAudioFileFormat(is);58AudioFileFormat.Type type = fileformat.getType();59System.out.println(" The file format type: "+type);60if (fileformat.getFrameLength()!=fakedata.length61&& fileformat.getFrameLength()!=AudioSystem.NOT_SPECIFIED) {62throw new Exception("The written file's frame length is "+fileformat.getFrameLength()+" but should be "+fakedata.length+" !");63}64ais = AudioSystem.getAudioInputStream(is);65System.out.println(" Got Stream with format: "+ais.getFormat());66if (is.getPos()<46) {67throw new Exception("After reading the header, stream position must be at least 46, but is "+is.getPos()+" !");68}69System.out.println(" test passed.");70}7172static class MyByteArrayInputStream extends ByteArrayInputStream {7374MyByteArrayInputStream(byte[] data) {75super(data);76}7778int getPos() {79return pos;80}81}82}838485