Path: blob/master/test/jdk/javax/sound/sampled/spi/FormatConversionProvider/AlawUlaw.java
41159 views
/*1* Copyright (c) 2002, 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;26import java.io.OutputStream;2728import javax.sound.sampled.AudioFormat;29import javax.sound.sampled.AudioInputStream;30import javax.sound.sampled.AudioSystem;3132/**33* @test34* @bug 471484635* @summary JavaSound ULAW (8-bit) encoder erroneously depends on endian-ness36*/37public class AlawUlaw {38static ByteArrayInputStream in;39static int byteLength = 1000;4041static boolean failed = false;4243public static void main(String[] args) throws Exception {44// generate some random data45byte[] soundData = new byte[byteLength];46for (int i=0; i<soundData.length; i++) {47soundData[i] = (byte) ((i % 256) - 128);48}4950// create an AudioInputStream from it51in = new ByteArrayInputStream(soundData);52in.mark(1);5354test1(PCM_FORMAT_1, ULAW_FORMAT_1, ULAW_FORMAT_2);55test1(PCM_FORMAT_2, ULAW_FORMAT_1, ULAW_FORMAT_2);56test2(ULAW_FORMAT_1, ULAW_FORMAT_2, PCM_FORMAT_1);57test2(ULAW_FORMAT_1, ULAW_FORMAT_2, PCM_FORMAT_2);5859test1(PCM_FORMAT_1, ALAW_FORMAT_1, ALAW_FORMAT_2);60test1(PCM_FORMAT_2, ALAW_FORMAT_1, ALAW_FORMAT_2);61test2(ALAW_FORMAT_1, ALAW_FORMAT_2, PCM_FORMAT_1);62test2(ALAW_FORMAT_1, ALAW_FORMAT_2, PCM_FORMAT_2);6364if (failed) {65throw new Exception("Test failed!");66}67}6869public static String printFormat(AudioFormat format) {70return format.toString()+" "+(format.isBigEndian()?"big":"little")+" endian";71}727374public static void test1(AudioFormat inFormat, AudioFormat outFormat1, AudioFormat outFormat2) throws Exception {75AudioInputStream inStream = new AudioInputStream(in, inFormat, -1);76System.out.println("Input Format: " + printFormat(inStream.getFormat()));7778// get a converted stream79AudioInputStream stream1 = AudioSystem.getAudioInputStream(outFormat1, inStream);80System.out.println("Output Format 1: " + printFormat(stream1.getFormat()));8182// get a converted stream in big endian ulaw83AudioInputStream stream2 = AudioSystem.getAudioInputStream(outFormat2, inStream);84System.out.println("Output Format 2: " + printFormat(stream2.getFormat()));8586compareStreams(stream1, stream2);87}8889public static void test2(AudioFormat inFormat1, AudioFormat inFormat2, AudioFormat outFormat) throws Exception {90AudioInputStream inStream1 = new AudioInputStream(in, inFormat1, -1);91System.out.println("Input Format1: " + printFormat(inStream1.getFormat()));9293// get a converted stream94AudioInputStream stream1 = AudioSystem.getAudioInputStream(outFormat, inStream1);95System.out.println("Output Format 1: " + printFormat(stream1.getFormat()));9697AudioInputStream inStream2 = new AudioInputStream(in, inFormat2, -1);98System.out.println("Input Format1: " + printFormat(inStream2.getFormat()));99100// get a converted stream in big endian ulaw101AudioInputStream stream2 = AudioSystem.getAudioInputStream(outFormat, inStream2);102System.out.println("Output Format 2: " + printFormat(stream2.getFormat()));103104compareStreams(stream1, stream2);105}106107public static void compareStreams(InputStream stream1, InputStream stream2) throws Exception {108ByteArrayOutputStream baos1 = new ByteArrayOutputStream();109ByteArrayOutputStream baos2 = new ByteArrayOutputStream();110111in.reset();112writeDirectly(stream1, baos1);113in.reset();114writeDirectly(stream2, baos2);115116if (baos1.size() != baos2.size()) {117System.out.println(" stream1 has length = "+baos1.size()+", stream2 has length = "+baos2.size());118}119int len = baos1.size();120if (len > baos2.size()) {121len = baos2.size();122}123byte[] data1=baos1.toByteArray();124byte[] data2=baos2.toByteArray();125for (int i=0; i<len; i++) {126if (data1[i] != data2[i]) {127System.out.println(" FAILED! Difference encountered at position "+i);128failed = true;129return;130}131}132if (baos1.size() != baos2.size()) {133System.out.println(" No difference, but different length!");134failed = true;135return;136}137System.out.println(" PASSED");138}139140public static void writeDirectly(InputStream in, OutputStream out) throws Exception {141// read data from the stream until we reach the end of the stream142byte tmp[] = new byte[16384];143while (true) {144int bytesRead = in.read(tmp, 0, tmp.length);145if (bytesRead == -1) {146break;147}148out.write(tmp, 0, bytesRead);149} // while150}151152public static final AudioFormat PCM_FORMAT_1 =153new AudioFormat( AudioFormat.Encoding.PCM_SIGNED,1548000f, //sample rate15516, //bits per sample1561, //channels1572, //frame size1588000f, // frame rate159false); //isBigEndian160public static final AudioFormat PCM_FORMAT_2 =161new AudioFormat( AudioFormat.Encoding.PCM_SIGNED,1628000f, //sample rate16316, //bits per sample1641, //channels1652, //frame size1668000f, // frame rate167true); //isBigEndian168169public static final AudioFormat ULAW_FORMAT_1 =170new AudioFormat( AudioFormat.Encoding.ULAW,1718000f, //sample rate1728, //bits per sample1731, //channels1741, //frame size1758000f, // frame rate176false); //isBigEndian177178public static final AudioFormat ULAW_FORMAT_2 =179new AudioFormat( AudioFormat.Encoding.ULAW,1808000f, //sample rate1818, //bits per sample1821, //channels1831, //frame size1848000f, // frame rate185true); //isBigEndian186187public static final AudioFormat ALAW_FORMAT_1 =188new AudioFormat( AudioFormat.Encoding.ALAW,1898000f, //sample rate1908, //bits per sample1911, //channels1921, //frame size1938000f, // frame rate194false); //isBigEndian195196public static final AudioFormat ALAW_FORMAT_2 =197new AudioFormat( AudioFormat.Encoding.ALAW,1988000f, //sample rate1998, //bits per sample2001, //channels2011, //frame size2028000f, // frame rate203true); //isBigEndian204}205206207