286N/A/*
286N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/A/* @test
286N/A @summary Test ModelByteBuffer.RandomFileInputStream read(byte[], int, int) method */
286N/A
286N/Aimport java.io.File;
286N/Aimport java.io.FileOutputStream;
286N/Aimport java.io.IOException;
286N/Aimport java.io.InputStream;
286N/A
286N/Aimport javax.sound.sampled.*;
286N/A
286N/Aimport com.sun.media.sound.*;
286N/A
286N/Apublic class ReadByteIntInt {
286N/A
286N/A static float[] testarray;
286N/A static byte[] test_byte_array;
286N/A static File test_file;
286N/A static AudioFormat format = new AudioFormat(44100, 16, 1, true, false);
286N/A
286N/A static void setUp() throws Exception {
286N/A testarray = new float[1024];
286N/A for (int i = 0; i < 1024; i++) {
286N/A double ii = i / 1024.0;
286N/A ii = ii * ii;
286N/A testarray[i] = (float)Math.sin(10*ii*2*Math.PI);
286N/A testarray[i] += (float)Math.sin(1.731 + 2*ii*2*Math.PI);
286N/A testarray[i] += (float)Math.sin(0.231 + 6.3*ii*2*Math.PI);
286N/A testarray[i] *= 0.3;
286N/A }
286N/A test_byte_array = new byte[testarray.length*2];
286N/A AudioFloatConverter.getConverter(format).toByteArray(testarray, test_byte_array);
286N/A test_file = File.createTempFile("test", ".raw");
286N/A FileOutputStream fos = new FileOutputStream(test_file);
286N/A fos.write(test_byte_array);
286N/A }
286N/A
286N/A static void tearDown() throws Exception {
286N/A if(!test_file.delete())
286N/A test_file.deleteOnExit();
286N/A }
286N/A
286N/A public static void main(String[] args) throws Exception {
286N/A try
286N/A {
286N/A setUp();
286N/A
286N/A for (int i = 0; i < 8; i++) {
286N/A ModelByteBuffer buff;
286N/A if(i % 2 == 0)
286N/A buff = new ModelByteBuffer(test_file);
286N/A else
286N/A buff = new ModelByteBuffer(test_byte_array);
286N/A if((i / 2) == 1)
286N/A buff.subbuffer(5);
286N/A if((i / 2) == 2)
286N/A buff.subbuffer(5,500);
286N/A if((i / 2) == 3)
286N/A buff.subbuffer(5,600,true);
286N/A
286N/A long capacity = buff.capacity();
286N/A InputStream is = buff.getInputStream();
286N/A try
286N/A {
286N/A byte[] b = new byte[100];
286N/A int ret = is.available();
286N/A int n = is.read(b, 7, 50);
286N/A if(n == -1)
286N/A throw new RuntimeException("is.read shouldn't return -1!");
286N/A if(is.available() != ret - n)
286N/A throw new RuntimeException(
"is.available() returns incorrect value ("
+ is.available() + "!="+(ret - n)+") !");
is.skip(5000);
if(is.read(b, 7, 50) != -1)
throw new RuntimeException(
"is.read() doesn't return -1!");
}
finally
{
is.close();
}
if(buff.capacity() != capacity)
throw new RuntimeException("Capacity variable should not change!");
}
}
finally
{
tearDown();
}
}
}