Path: blob/master/test/jdk/javax/sound/sampled/spi/AudioFileWriter/WaveBigEndian.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 500195235* @summary Writing WAVE with big endian data produces corrupt file. WAVE should36* always write signed 16-bit, little endian, regardless of the37* endianness of the input data.38*/39public class WaveBigEndian {4041static boolean failed = false;4243public static byte[] writeDataAndGetAIS(boolean bigEndian) throws Exception {44if (bigEndian) {45out("Create WAVE file from big endian data...");46} else {47out("Create WAVE file from little endian data...");48}49byte[] data = new byte[3000];50for (int i = 0; i < data.length; i+=2) {51if (bigEndian) {52data[i] = (byte) i;53data[i+1] = (byte) (i+1);54} else {55data[i] = (byte) (i+1);56data[i+1] = (byte) i;57}58}59AudioFormat format = new AudioFormat(44100.0f, 16, 1, true, bigEndian);60InputStream is = new ByteArrayInputStream(data);61AudioInputStream ais = new AudioInputStream(is, format, data.length);62ByteArrayOutputStream os = new ByteArrayOutputStream();63int written = AudioSystem.write(ais, AudioFileFormat.Type.WAVE, os);64data = os.toByteArray();65out("Wrote "+written+" bytes, got "+data.length+" bytes in written file.");66is = new ByteArrayInputStream(data);67ais = AudioSystem.getAudioInputStream(is);68out("Got AIS with length = "+ais.getFrameLength()+" frames.");69return data;70}717273public static void main(String args[]) throws Exception {74byte[] data1 = writeDataAndGetAIS(false);75byte[] data2 = writeDataAndGetAIS(true);7677if (data1.length != data2.length) {78out("# data1.length != data2.length!");79failed = true;80} else {81for (int i = 0 ; i < data1.length; i++) {82if (data1[i] != data2[i]) {83out("# At index "+i+": le="+(data1[i] & 0xFF)+" be="+(data2[i] & 0xFF)+" !");84failed = true;85}86}87}8889if (failed) throw new Exception("Test FAILED!");90out("Files are identical.");91out("test passed");92}9394static void out(String s) {95System.out.println(s);96}97}9899100