ReadByte.java revision 9330:8b1f1c2a400f
1223328Sgavin/* 2223328Sgavin * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. 379971Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4223328Sgavin * 5223328Sgavin * This code is free software; you can redistribute it and/or modify it 6223328Sgavin * under the terms of the GNU General Public License version 2 only, as 7223328Sgavin * published by the Free Software Foundation. 8223328Sgavin * 9223328Sgavin * This code is distributed in the hope that it will be useful, but WITHOUT 1079971Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11223328Sgavin * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12223328Sgavin * version 2 for more details (a copy is included in the LICENSE file that 13223328Sgavin * accompanied this code). 14223328Sgavin * 15223328Sgavin * You should have received a copy of the GNU General Public License version 1679971Sobrien * 2 along with this work; if not, write to the Free Software Foundation, 17223328Sgavin * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18223328Sgavin * 19223328Sgavin * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20223328Sgavin * or visit www.oracle.com if you need additional information or have any 21223328Sgavin * questions. 22223328Sgavin */ 23223328Sgavin 24223328Sgavin/* @test 25223328Sgavin @summary Test ModelByteBuffer.RandomFileInputStream read(byte[]) method */ 26223328Sgavin 27223328Sgavinimport java.io.File; 28223328Sgavinimport java.io.FileOutputStream; 29223328Sgavinimport java.io.IOException; 30223328Sgavinimport java.io.InputStream; 31223328Sgavin 32223328Sgavinimport javax.sound.sampled.*; 33223328Sgavin 34223328Sgavinimport com.sun.media.sound.*; 35223328Sgavin 36223328Sgavinpublic class ReadByte { 37223328Sgavin 38223328Sgavin static float[] testarray; 39223328Sgavin static byte[] test_byte_array; 40223328Sgavin static File test_file; 41223328Sgavin static AudioFormat format = new AudioFormat(44100, 16, 1, true, false); 42223328Sgavin 43223328Sgavin static void setUp() throws Exception { 44223328Sgavin testarray = new float[1024]; 45223328Sgavin for (int i = 0; i < 1024; i++) { 46223328Sgavin double ii = i / 1024.0; 47223328Sgavin ii = ii * ii; 48223328Sgavin testarray[i] = (float)Math.sin(10*ii*2*Math.PI); 49223328Sgavin testarray[i] += (float)Math.sin(1.731 + 2*ii*2*Math.PI); 50223328Sgavin testarray[i] += (float)Math.sin(0.231 + 6.3*ii*2*Math.PI); 51223328Sgavin testarray[i] *= 0.3; 52223328Sgavin } 53223328Sgavin test_byte_array = new byte[testarray.length*2]; 54223328Sgavin AudioFloatConverter.getConverter(format).toByteArray(testarray, test_byte_array); 55223328Sgavin test_file = File.createTempFile("test", ".raw"); 56223328Sgavin FileOutputStream fos = new FileOutputStream(test_file); 57223328Sgavin fos.write(test_byte_array); 58223328Sgavin } 59223328Sgavin 60223328Sgavin static void tearDown() throws Exception { 61223328Sgavin if(!test_file.delete()) 62223328Sgavin test_file.deleteOnExit(); 63223328Sgavin } 64223328Sgavin 65223328Sgavin public static void main(String[] args) throws Exception { 66223328Sgavin try 67223328Sgavin { 68223328Sgavin setUp(); 69223328Sgavin 70223328Sgavin for (int i = 0; i < 8; i++) { 71223328Sgavin ModelByteBuffer buff; 72223328Sgavin if(i % 2 == 0) 73223328Sgavin buff = new ModelByteBuffer(test_file); 74223328Sgavin else 75223328Sgavin buff = new ModelByteBuffer(test_byte_array); 76223328Sgavin if((i / 2) == 1) 77223328Sgavin buff.subbuffer(5); 78223328Sgavin if((i / 2) == 2) 79223328Sgavin buff.subbuffer(5,500); 80223328Sgavin if((i / 2) == 3) 81223328Sgavin buff.subbuffer(5,600,true); 82223328Sgavin 83223328Sgavin long capacity = buff.capacity(); 84223328Sgavin InputStream is = buff.getInputStream(); 85223328Sgavin try 86223328Sgavin { 87223328Sgavin byte[] b = new byte[100]; 88223328Sgavin int ret = is.available(); 89223328Sgavin int n = is.read(b); 90223328Sgavin if(n == -1) 91223328Sgavin throw new RuntimeException("is.read shouldn't return -1!"); 92223328Sgavin if(is.available() != ret - n) 93223328Sgavin throw new RuntimeException( 94223328Sgavin "is.available() returns incorrect value (" 95223328Sgavin + is.available() + "!="+(ret - n)+") !"); 96223328Sgavin is.skip(5000); 97223328Sgavin if(is.read(b) != -1) 98223328Sgavin throw new RuntimeException( 99223328Sgavin "is.read() doesn't return -1!"); 100223328Sgavin 101223328Sgavin } 102223328Sgavin finally 103223328Sgavin { 104223328Sgavin is.close(); 105223328Sgavin } 106223328Sgavin if(buff.capacity() != capacity) 107223328Sgavin throw new RuntimeException("Capacity variable should not change!"); 108223328Sgavin } 109223328Sgavin } 110223328Sgavin finally 111223328Sgavin { 112223328Sgavin tearDown(); 113223328Sgavin } 114223328Sgavin } 115223328Sgavin 116223328Sgavin} 117223328Sgavin