Path: blob/master/test/jdk/javax/sound/midi/Gervill/ModelByteBuffer/RandomFileInputStream/Skip.java
41161 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.RandomFileInputStream skip(long) method25@modules java.desktop/com.sun.media.sound26*/2728import java.io.File;29import java.io.FileOutputStream;30import java.io.InputStream;31import java.nio.file.Files;32import java.nio.file.Paths;3334import javax.sound.sampled.*;3536import com.sun.media.sound.*;3738public class Skip {3940static float[] testarray;41static byte[] test_byte_array;42static File test_file;43static AudioFormat format = new AudioFormat(44100, 16, 1, true, false);4445static void setUp() throws Exception {46testarray = new float[1024];47for (int i = 0; i < 1024; i++) {48double ii = i / 1024.0;49ii = ii * ii;50testarray[i] = (float)Math.sin(10*ii*2*Math.PI);51testarray[i] += (float)Math.sin(1.731 + 2*ii*2*Math.PI);52testarray[i] += (float)Math.sin(0.231 + 6.3*ii*2*Math.PI);53testarray[i] *= 0.3;54}55test_byte_array = new byte[testarray.length*2];56AudioFloatConverter.getConverter(format).toByteArray(testarray, test_byte_array);57test_file = File.createTempFile("test", ".raw");58try (FileOutputStream fos = new FileOutputStream(test_file)) {59fos.write(test_byte_array);60}61}6263static void tearDown() throws Exception {64Files.delete(Paths.get(test_file.getAbsolutePath()));65}6667public static void main(String[] args) throws Exception {68try69{70setUp();7172for (int i = 0; i < 8; i++) {73ModelByteBuffer buff;74if(i % 2 == 0)75buff = new ModelByteBuffer(test_file);76else77buff = new ModelByteBuffer(test_byte_array);78if((i / 2) == 1)79buff.subbuffer(5);80if((i / 2) == 2)81buff.subbuffer(5,500);82if((i / 2) == 3)83buff.subbuffer(5,600,true);8485long capacity = buff.capacity();86InputStream is = buff.getInputStream();87try88{89int ret = is.available();90long n = is.skip(75);91if(n == -1)92throw new RuntimeException("is.read shouldn't return -1!");93if(is.available() != ret - n)94throw new RuntimeException(95"is.available() returns incorrect value ("96+ is.available() + "!="+(ret - n)+") !");9798ret = is.available();99n = is.skip(-100);100if(n != 0)101throw new RuntimeException("is.skip(-100) shouldn't skip values!");102if(is.available() != ret - n)103throw new RuntimeException(104"is.available() returns incorrect value ("105+ is.available() + "!="+(ret - n)+") !");106107ret = is.available();108n = is.skip(5000);109if(is.available() != ret - n)110throw new RuntimeException(111"is.available() returns incorrect value ("112+ is.available() + "!="+(ret - n)+") !");113if(is.available() != 0)114throw new RuntimeException(115"is.available() returns incorrect value ("116+ is.available() + "!="+(0)+") !"); }117finally118{119is.close();120}121if(buff.capacity() != capacity)122throw new RuntimeException("Capacity variable should not change!");123}124}125finally126{127tearDown();128}129}130131}132133134