WriteTo.java revision 8729:0242fce0f717
1252504Slstewart/*
2252504Slstewart * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
3252504Slstewart * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4252504Slstewart *
5252504Slstewart * This code is free software; you can redistribute it and/or modify it
6252504Slstewart * under the terms of the GNU General Public License version 2 only, as
7252504Slstewart * published by the Free Software Foundation.
8252504Slstewart *
9252504Slstewart * This code is distributed in the hope that it will be useful, but WITHOUT
10252504Slstewart * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11252504Slstewart * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12252504Slstewart * version 2 for more details (a copy is included in the LICENSE file that
13252504Slstewart * accompanied this code).
14252504Slstewart *
15252504Slstewart * You should have received a copy of the GNU General Public License version
16252504Slstewart * 2 along with this work; if not, write to the Free Software Foundation,
17252504Slstewart * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18252504Slstewart *
19252504Slstewart * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20252504Slstewart * or visit www.oracle.com if you need additional information or have any
21252504Slstewart * questions.
22252504Slstewart */
23252504Slstewart
24252504Slstewart/* @test
25252504Slstewart   @summary Test ModelByteBuffer writeTo method */
26252504Slstewart
27252504Slstewartimport java.io.ByteArrayOutputStream;
28252504Slstewartimport java.io.File;
29252504Slstewartimport java.io.FileOutputStream;
30252504Slstewartimport java.io.IOException;
31252504Slstewart
32252504Slstewartimport javax.sound.sampled.*;
33252504Slstewart
34252504Slstewartimport com.sun.media.sound.*;
35252504Slstewart
36252504Slstewartpublic class WriteTo {
37252504Slstewart
38252504Slstewart    static float[] testarray;
39252504Slstewart    static byte[] test_byte_array;
40252504Slstewart    static File test_file;
41252504Slstewart    static AudioFormat format = new AudioFormat(44100, 16, 1, true, false);
42252504Slstewart
43252504Slstewart    static void setUp() throws Exception {
44252504Slstewart        testarray = new float[1024];
45252504Slstewart        for (int i = 0; i < 1024; i++) {
46252504Slstewart            double ii = i / 1024.0;
47252504Slstewart            ii = ii * ii;
48252504Slstewart            testarray[i] = (float)Math.sin(10*ii*2*Math.PI);
49252504Slstewart            testarray[i] += (float)Math.sin(1.731 + 2*ii*2*Math.PI);
50252504Slstewart            testarray[i] += (float)Math.sin(0.231 + 6.3*ii*2*Math.PI);
51252504Slstewart            testarray[i] *= 0.3;
52252504Slstewart        }
53252504Slstewart        test_byte_array = new byte[testarray.length*2];
54252504Slstewart        AudioFloatConverter.getConverter(format).toByteArray(testarray, test_byte_array);
55252504Slstewart        test_file = File.createTempFile("test", ".raw");
56252504Slstewart        FileOutputStream fos = new FileOutputStream(test_file);
57252504Slstewart        fos.write(test_byte_array);
58252504Slstewart    }
59252504Slstewart
60252504Slstewart    static void tearDown() throws Exception {
61252504Slstewart        if(!test_file.delete())
62252504Slstewart            test_file.deleteOnExit();
63252504Slstewart    }
64252504Slstewart
65252504Slstewart    public static void main(String[] args) throws Exception {
66252504Slstewart        try
67252504Slstewart        {
68252504Slstewart            setUp();
69252504Slstewart
70252504Slstewart            for (int i = 0; i < 2; i++) {
71252504Slstewart                ModelByteBuffer buff;
72252504Slstewart                ByteArrayOutputStream baos = new ByteArrayOutputStream();
73252504Slstewart                if(i == 0)
74252504Slstewart                    buff = new ModelByteBuffer(test_file);
75252504Slstewart                else
76252504Slstewart                    buff = new ModelByteBuffer(test_byte_array);
77252504Slstewart                buff.writeTo(baos);
78252504Slstewart                byte[] b = baos.toByteArray();
79252504Slstewart                for (int j = 0; j < b.length; j++)
80252504Slstewart                    if(b[i] != test_byte_array[i])
81252504Slstewart                        throw new RuntimeException("baos.toByteArray() incorrect!");
82252504Slstewart            }
83252504Slstewart        }
84252504Slstewart        finally
85252504Slstewart        {
86252504Slstewart            tearDown();
87252504Slstewart        }
88252504Slstewart    }
89252504Slstewart
90252504Slstewart}
91252504Slstewart