Path: blob/master/test/jdk/javax/sound/midi/Gervill/ModelByteBuffer/RandomFileInputStream/MarkReset.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 mark and reset methods25@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 MarkReset {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{89is.mark(1000);90int ret = is.available();91int a = is.read();92is.skip(75);93is.reset();94if(is.available() != ret)95throw new RuntimeException(96"is.available() returns incorrect value ("97+ is.available() + "!="+(ret)+") !");98int b = is.read();99if(a != b)100throw new RuntimeException(101"is doesn't return same value after reset ("102+ a + "!="+b+") !");103104is.skip(15);105ret = is.available();106is.mark(1000);107is.reset();108if(is.available() != ret)109throw new RuntimeException(110"is.available() returns incorrect value ("111+ is.available() + "!="+(ret)+") !");112113114}115finally116{117is.close();118}119if(buff.capacity() != capacity)120throw new RuntimeException("Capacity variable should not change!");121}122}123finally124{125tearDown();126}127}128129}130131132