Path: blob/master/test/jdk/javax/sound/sampled/AudioInputStream/AISReadFraction.java
41152 views
/*1* Copyright (c) 2003, 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*/2223import java.io.IOException;24import java.io.InputStream;2526import javax.sound.sampled.AudioFormat;27import javax.sound.sampled.AudioInputStream;28import javax.sound.sampled.AudioSystem;2930/**31* @test32* @bug 494866333* @summary AudioInputStream does not use the original stream passed to its constructor34*/35public class AISReadFraction {3637static int failed = 0;38static byte[] testData = new byte[256];39static boolean DEBUG = false;4041static AudioFormat[] formats = {42new AudioFormat(44100.0f, 8, 1, false, false), // frameSize = 143new AudioFormat(44100.0f, 8, 2, false, false), // frameSize = 244new AudioFormat(44100.0f, 16, 1, true, false), // frameSize = 245new AudioFormat(44100.0f, 24, 1, true, false), // frameSize = 346new AudioFormat(44100.0f, 16, 2, true, false), // frameSize = 447new AudioFormat(44100.0f, 8, 5, false, false), // frameSize = 548new AudioFormat(44100.0f, 16, 3, true, false), // frameSize = 649new AudioFormat(44100.0f, 8, 7, false, false), // frameSize = 750new AudioFormat(44100.0f, 32, 2, true, false) // frameSize = 851};525354public static void main(String args[]) throws Exception {55for (int i = 0; i<testData.length; i++) {56testData[i] = (byte) (i % 128);57}5859for (int f = 0; f < formats.length; f++) {60// first test without marking61doTest(formats[f], false);62// then with marking63doTest(formats[f], true);64}6566out(""+failed+" failures.");67if (failed>0) throw new Exception("Test FAILED!");68out("Test passed.");69}7071static void doTest(AudioFormat format, boolean doMark) {72out("Test with"+(doMark?"":"out")+" marking. Audio format: "73+"sampleSize="+format.getSampleSizeInBits()+"bits "74+"channels="+format.getChannels()+" "75+"frameSize="+format.getFrameSize()+"byte(s)");76int maxReadBytes = (testData.length / format.getFrameSize()) * format.getFrameSize();77InputStream is = new FractionalIS(testData, doMark);78AudioInputStream ais = new AudioInputStream(is, format, AudioSystem.NOT_SPECIFIED);79// first some general tests80if (ais.markSupported() && !doMark) {81out(" #AIS reports markSupported, but underlying stream cannot! FAILED");82failed ++;83}84if (!ais.markSupported() && doMark) {85out(" #AIS does not report markSupported, but underlying stream can mark! FAILED");86failed++;87}88byte[] data = new byte[1000];89int frameSize = format.getFrameSize();90int counter = 5;91int totalReadBytes = 0;92boolean hasRead0 = false;93boolean hasMarked = false;94boolean hasReset = false;95int markPos = 0;96while (true) {97try {98int toBeRead = frameSize * counter;99counter += 3;100if (counter > 14) {101counter -= 14;102}103int read = ais.read(data, 0, toBeRead);104if (DEBUG) out(" -> ais.read(data, 0, "+toBeRead+"): "+read+" (frameSize="+frameSize+")");105if ((totalReadBytes == maxReadBytes) && (read != -1)106&& ((read > 0) || hasRead0)) {107if (read == 0) {108out(" #stream was read to the end ("+maxReadBytes+"), but ais.read returned repeatedly 0 bytes. FAILED");109} else {110out(" #stream was read to the end ("+maxReadBytes+"), but ais.read returned "+read+" bytes... FAILED");111}112failed++;113break;114}115if (read > 0) {116verifyReadBytes(data, totalReadBytes, read);117if ((read % frameSize) != 0) {118out(" #Read non-integral number of frames: "+read+" bytes, frameSize="+frameSize+" bytes. FAILED");119failed++;120}121totalReadBytes += read;122hasRead0 = false;123}124else if (read == 0) {125//out(" wanted to read "+toBeRead+" at position "+totalReadBytes+", but got 0 bytes!");126if (hasRead0) {127out(" read 0 twice in a row! FAILED");128failed++;129break;130}131hasRead0 = true;132} else {133// end of stream134out(" End of stream reached. Total read bytes: "+totalReadBytes);135if (totalReadBytes != maxReadBytes) {136out(" #Failed: should have read "+maxReadBytes+" bytes! FAILED.");137failed++;138}139break;140}141142// test marking143if (totalReadBytes > 50 && !hasMarked && !hasReset && doMark) {144out(" Marking at position "+totalReadBytes);145hasMarked = true;146ais.mark(0);147markPos = totalReadBytes;148}149if (totalReadBytes > 100 && hasMarked && !hasReset && doMark) {150out(" Resetting at position "+totalReadBytes+" back to "+markPos);151hasReset = true;152ais.reset();153totalReadBytes = markPos;154}155156} catch (IOException e) {157out(" #caught unexpected exception:");158e.printStackTrace();159failed++;160}161}162}163164static void verifyReadBytes(byte[] data, int offset, int len) {165int firstWrongByte = -1;166for (int i = 0; i < len; i++) {167int expected = ((offset + i) % 128);168if (data[i] != expected) {169out(" read data is not correct! offset="+offset+" expected="+expected+" actual="+data[i]);170failed++;171break;172}173}174}175176177public static void out(String s) {178System.out.println(s);179}180181182static class FractionalIS extends InputStream {183byte[] data;184int pos = 0;185boolean canMark;186// a counter how many bytes are not returned187int missingBytes = 0;188int markPos = -1;189190FractionalIS(byte[] data, boolean canMark) {191this.data = data;192this.canMark = canMark;193}194195public int read() throws IOException {196if (pos >= data.length) {197return -1;198}199return data[pos++] & 0xFF;200}201202public int read(byte[] b, int off, int len) throws IOException {203if (++missingBytes > 5) {204missingBytes = 0;205}206int reducedLen = len - missingBytes;207if (reducedLen <= 0) reducedLen = 1;208if (DEBUG) out(" FIS.read(data, 0, "+len+"): reducing len to "+reducedLen+" bytes.");209int ret = super.read(b, off, reducedLen);210if (DEBUG) out(" returning "+ret+" bytes. Now at pos="+pos);211return ret;212}213214public void mark(int readlimit) {215markPos = pos;216if (DEBUG) out(" FIS.mark(): marking at "+pos);217}218219public void reset() throws IOException {220if (!canMark) {221throw new IOException("reset not supported!");222}223if (markPos == -1) {224throw new IOException("Mark position not set!");225}226pos = markPos;227if (DEBUG) out(" FIS.reset(): now back at "+pos);228}229230public boolean markSupported() {231return canMark;232}233234}235236}237238239