1/*
2 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.io.IOException;
25import java.io.InputStream;
26
27import javax.sound.sampled.AudioFormat;
28import javax.sound.sampled.AudioInputStream;
29import javax.sound.sampled.AudioSystem;
30
31/**
32 * @test
33 * @bug 6188860
34 * @summary Tests that method AudioInputStream.read() returns right value
35 */
36public class bug6188860 {
37
38    public static void main(String[] args) throws Exception {
39        byte[] testData = new byte[256];
40
41        // fill data
42        for (int i = 0; i < testData.length; i++)
43            testData[i] = (byte) (i % 128);
44
45        InputStream streamSrc = new TestInputStream(testData);
46        AudioFormat format = new AudioFormat(44100.0f, 8, 1, false, false); // frameSize = 1
47        AudioInputStream streamAudio = new AudioInputStream(streamSrc, format, AudioSystem.NOT_SPECIFIED);
48
49        int nErrCount = 0;
50        int nTotal = 0;
51
52        int dataSrc, dataRead;
53        while (nTotal < (testData.length - 1)) {
54            dataRead = streamAudio.read();
55            if (dataRead < 0) {
56                System.out.println("end of stream");
57                break;
58            }
59
60            dataSrc = testData[nTotal];
61
62            if (dataRead != dataSrc) {
63                System.out.println("" + nTotal + " - mismatch :" + dataRead + " <> " + dataSrc);
64                nErrCount++;
65            }
66            nTotal++;
67        }
68
69        System.out.println("Total: " + nTotal + "; Mismatches: " + nErrCount);
70
71        if (nErrCount > 0) {
72            throw new RuntimeException("test failed: " + nErrCount + " mismatches of total " + nTotal + " bytes.");
73        }
74        System.out.println("Test sucessfully passed.");
75    }
76
77
78    static class TestInputStream extends InputStream {
79        byte[] data;
80        int pos = 0;
81
82        TestInputStream(byte[] data) {
83            this.data = data;
84        }
85
86        public int read() throws IOException {
87            if (pos >= data.length) {
88                return -1;
89            }
90            return data[pos++] & 0xFF;
91        }
92    }
93
94}
95