3629N/A/**
3629N/A * @test
3629N/A * @bug 6938426
3629N/A * @summary Tests that Alaw encoder works properly in multithreaded environment
3629N/A * @author Alex Menkov
3629N/A */
3629N/A
3629N/Aimport java.io.ByteArrayInputStream;
3629N/Aimport java.io.ByteArrayOutputStream;
3629N/Aimport java.io.InputStream;
3629N/Aimport java.util.Arrays;
3629N/Aimport javax.sound.sampled.AudioFormat;
3629N/Aimport javax.sound.sampled.AudioInputStream;
3629N/Aimport javax.sound.sampled.AudioSystem;
3629N/A
3629N/Apublic class AlawEncoderSync {
3629N/A
3629N/A static final int THREAD_COUNT = 20;
3629N/A
3629N/A static final AudioFormat pcmFormat = new AudioFormat(8000f, 16, 2, true, false);
3629N/A static final int STREAM_LENGTH = 10; // in seconds
3629N/A static byte[] pcmBuffer;
3629N/A static final AudioFormat alawFormat
3629N/A = new AudioFormat(AudioFormat.Encoding.ALAW, 8000f, 8, 2, 2, 8000f, false);
3629N/A
3629N/A static final ConversionThread[] threads = new ConversionThread[THREAD_COUNT];
3629N/A
3629N/A public static void main(String[] args) {
3629N/A preparePCMBuffer();
3629N/A log("pcmStream size: " + pcmBuffer.length);
3629N/A
3629N/A for (int i=0; i<THREAD_COUNT; i++) {
3629N/A threads[i] = new ConversionThread(i);
3629N/A threads[i].start();
3629N/A }
3629N/A
3629N/A for (int i=1; i<THREAD_COUNT; i++) {
3629N/A try {
3629N/A threads[i].join();
3629N/A } catch (InterruptedException ex) {
3629N/A log("Main thread was interrupted, exiting.");
3629N/A return;
3629N/A }
3629N/A }
3629N/A
3629N/A int failed = 0;
3629N/A log("comparing result arrays...");
3629N/A for (int i=1; i<THREAD_COUNT; i++) {
3629N/A if (!Arrays.equals(threads[0].resultArray, threads[i].resultArray)) {
3629N/A failed++;
3629N/A log("NOT equals: 0 and " + i);
3629N/A }
3629N/A }
3629N/A if (failed > 0) {
3629N/A throw new RuntimeException("test FAILED");
3629N/A }
3629N/A log("test PASSED.");
3629N/A }
3629N/A
3629N/A
3629N/A static void preparePCMBuffer() {
3629N/A pcmBuffer = new byte[STREAM_LENGTH * (int)pcmFormat.getSampleRate()
3629N/A * (pcmFormat.getSampleSizeInBits() / 8) * pcmFormat.getChannels()];
3629N/A for (int i=0; i<pcmBuffer.length; i++) {
3629N/A pcmBuffer[i] = (byte)(Math.random() * 256.0 - 128.0);
3629N/A }
3629N/A }
3629N/A
3629N/A static AudioInputStream createPCMStream() {
3629N/A InputStream byteStream = new ByteArrayInputStream(pcmBuffer);
3629N/A return new AudioInputStream(byteStream, pcmFormat, AudioSystem.NOT_SPECIFIED);
3629N/A }
3629N/A
3629N/A static class ConversionThread extends Thread {
3629N/A public final int num;
3629N/A public byte[] resultArray = null;
3629N/A public ConversionThread(int num) {
3629N/A this.num = num;
3629N/A }
3629N/A @Override
3629N/A public void run() {
3629N/A log("ConversionThread[" + num + "] started.");
3629N/A try {
3629N/A InputStream inStream = new ByteArrayInputStream(pcmBuffer);
3629N/A
3629N/A AudioInputStream pcmStream = new AudioInputStream(
3629N/A inStream, pcmFormat, AudioSystem.NOT_SPECIFIED);
3629N/A AudioInputStream alawStream = AudioSystem.getAudioInputStream(alawFormat, pcmStream);
3629N/A
3629N/A ByteArrayOutputStream outStream = new ByteArrayOutputStream();
3629N/A int read = 0;
3629N/A byte[] data = new byte[4096];
3629N/A while((read = alawStream.read(data)) != -1) {
3629N/A outStream.write(data, 0, read);
3629N/A }
3629N/A alawStream.close();
3629N/A resultArray = outStream.toByteArray();
3629N/A } catch (Exception ex) {
3629N/A log("ConversionThread[" + num + "] exception:");
3629N/A log(ex);
3629N/A }
3629N/A log("ConversionThread[" + num + "] completed.");
3629N/A }
3629N/A }
3629N/A
3629N/A static void log(String s) {
3629N/A System.out.println(s);
3629N/A }
3629N/A
3629N/A static void log(Exception ex) {
3629N/A ex.printStackTrace(System.out);
3629N/A }
3629N/A}