Path: blob/master/test/jdk/javax/sound/midi/Gervill/ModelByteBuffer/LoadAll.java
41159 views
/*1* Copyright (c) 2007, 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*/2223/* @test24@summary Test ModelByteBuffer loadAll method25@modules java.desktop/com.sun.media.sound26*/2728import java.io.File;29import java.io.FileOutputStream;30import java.nio.file.Files;31import java.nio.file.Paths;32import java.util.ArrayList;33import java.util.List;3435import javax.sound.sampled.*;3637import com.sun.media.sound.*;3839public class LoadAll {4041static float[] testarray;42static byte[] test_byte_array;43static File test_file;44static AudioFormat format = new AudioFormat(44100, 16, 1, true, false);4546static void setUp() throws Exception {47testarray = new float[1024];48for (int i = 0; i < 1024; i++) {49double ii = i / 1024.0;50ii = ii * ii;51testarray[i] = (float)Math.sin(10*ii*2*Math.PI);52testarray[i] += (float)Math.sin(1.731 + 2*ii*2*Math.PI);53testarray[i] += (float)Math.sin(0.231 + 6.3*ii*2*Math.PI);54testarray[i] *= 0.3;55}56test_byte_array = new byte[testarray.length*2];57AudioFloatConverter.getConverter(format).toByteArray(testarray, test_byte_array);58test_file = File.createTempFile("test", ".raw");59try (FileOutputStream fos = new FileOutputStream(test_file)) {60fos.write(test_byte_array);61}62}6364static void tearDown() throws Exception {65Files.delete(Paths.get(test_file.getAbsolutePath()));66}6768public static void main(String[] args) throws Exception {69try70{71setUp();7273ModelByteBuffer buff = new ModelByteBuffer(test_file);74List<ModelByteBuffer> col = new ArrayList<ModelByteBuffer>();75col.add(buff);76ModelByteBuffer.loadAll(col);77if(buff.array() == null)78throw new RuntimeException("buf is null!");79if(buff.array().length != test_byte_array.length)80throw new RuntimeException("buff.array().length length is incorrect!");81byte[] b = buff.array();82for (int i = 0; i < b.length; i++)83if(test_byte_array[i] != b[i])84throw new RuntimeException("buff.array() incorrect!");8586}87finally88{89tearDown();90}91}9293}949596