Path: blob/master/test/jdk/javax/sound/sampled/spi/AudioFileWriter/AlawEncoderSync.java
41159 views
/*1* Copyright (c) 2010, 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 693842626* @bug 705885227* @summary Tests that Alaw encoder works properly in multithreaded environment28* @author Alex Menkov29*/3031import java.io.ByteArrayInputStream;32import java.io.ByteArrayOutputStream;33import java.io.InputStream;34import java.util.Arrays;35import javax.sound.sampled.AudioFormat;36import javax.sound.sampled.AudioInputStream;37import javax.sound.sampled.AudioSystem;3839public class AlawEncoderSync {4041static final int THREAD_COUNT = 20;4243static final AudioFormat pcmFormat = new AudioFormat(8000f, 16, 2, true, false);44static final int STREAM_LENGTH = 10; // in seconds45static byte[] pcmBuffer;46static final AudioFormat alawFormat47= new AudioFormat(AudioFormat.Encoding.ALAW, 8000f, 8, 2, 2, 8000f, false);4849static final ConversionThread[] threads = new ConversionThread[THREAD_COUNT];5051public static void main(String[] args) {52preparePCMBuffer();53log("pcmStream size: " + pcmBuffer.length);5455for (int i=0; i<THREAD_COUNT; i++) {56threads[i] = new ConversionThread(i);57threads[i].start();58}5960for (int i=0; i<THREAD_COUNT; i++) {61try {62threads[i].join();63} catch (InterruptedException ex) {64log("Main thread was interrupted, exiting.");65return;66}67}6869int failed = 0;70log("comparing result arrays...");71for (int i=1; i<THREAD_COUNT; i++) {72if (!Arrays.equals(threads[0].resultArray, threads[i].resultArray)) {73failed++;74log("NOT equals: 0 and " + i);75}76}77if (failed > 0) {78throw new RuntimeException("test FAILED");79}80log("test PASSED.");81}828384static void preparePCMBuffer() {85pcmBuffer = new byte[STREAM_LENGTH * (int)pcmFormat.getSampleRate()86* (pcmFormat.getSampleSizeInBits() / 8) * pcmFormat.getChannels()];87for (int i=0; i<pcmBuffer.length; i++) {88pcmBuffer[i] = (byte)(Math.random() * 256.0 - 128.0);89}90}9192static AudioInputStream createPCMStream() {93InputStream byteStream = new ByteArrayInputStream(pcmBuffer);94return new AudioInputStream(byteStream, pcmFormat, AudioSystem.NOT_SPECIFIED);95}9697static class ConversionThread extends Thread {98public final int num;99public byte[] resultArray = null;100public ConversionThread(int num) {101this.num = num;102}103@Override104public void run() {105log("ConversionThread[" + num + "] started.");106try {107InputStream inStream = new ByteArrayInputStream(pcmBuffer);108109AudioInputStream pcmStream = new AudioInputStream(110inStream, pcmFormat, AudioSystem.NOT_SPECIFIED);111AudioInputStream alawStream = AudioSystem.getAudioInputStream(alawFormat, pcmStream);112113ByteArrayOutputStream outStream = new ByteArrayOutputStream();114int read = 0;115byte[] data = new byte[4096];116while((read = alawStream.read(data)) != -1) {117outStream.write(data, 0, read);118}119alawStream.close();120resultArray = outStream.toByteArray();121} catch (Exception ex) {122log("ConversionThread[" + num + "] exception:");123log(ex);124}125log("ConversionThread[" + num + "] completed.");126}127}128129static void log(String s) {130System.out.println(s);131}132133static void log(Exception ex) {134ex.printStackTrace(System.out);135}136}137138139