Path: blob/master/test/jdk/javax/sound/midi/Gervill/SF2SoundbankReader/TestGetSoundbankInputStream2.java
41155 views
/*1* Copyright (c) 2007, 2015, 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 SF2SoundbankReader getSoundbank(InputStream) method using25very bad InputStream which can only read 1 byte at time26@modules java.desktop/com.sun.media.sound27*/2829import java.io.BufferedInputStream;30import java.io.File;31import java.io.FileInputStream;32import java.io.IOException;33import java.io.InputStream;3435import javax.sound.midi.Patch;36import javax.sound.midi.Soundbank;3738import com.sun.media.sound.SF2SoundbankReader;3940public class TestGetSoundbankInputStream2 {4142private static class BadInputStream extends InputStream43{4445InputStream is;4647public BadInputStream(InputStream is)48{49this.is = is;50}5152public int read() throws IOException {53return is.read();54}5556public int read(byte[] b, int off, int len) throws IOException {57if(len > 1) len = 1;58return is.read(b, off, len);59}6061public int read(byte[] b) throws IOException {62return read(b, 0, b.length);63}6465public long skip(long n) throws IOException {66if(n > 1) n = 1;67return is.skip(n);68}6970public int available() throws IOException {71int avail = is.available();72if(avail > 1) avail = 1;73return avail;74}7576public void close() throws IOException {77is.close();78}7980public synchronized void mark(int readlimit) {81is.mark(readlimit);82}8384public boolean markSupported() {85return is.markSupported();86}8788public synchronized void reset() throws IOException {89is.reset();90}9192}9394private static void assertTrue(boolean value) throws Exception95{96if(!value)97throw new RuntimeException("assertTrue fails!");98}99100public static void main(String[] args) throws Exception {101File file = new File(System.getProperty("test.src", "."), "ding.sf2");102FileInputStream fis = new FileInputStream(file);103BufferedInputStream bis = new BufferedInputStream(fis);104try105{106InputStream badis = new BadInputStream(bis);107Soundbank sf2 = new SF2SoundbankReader().getSoundbank(badis);108assertTrue(sf2.getInstruments().length == 1);109Patch patch = sf2.getInstruments()[0].getPatch();110assertTrue(patch.getProgram() == 0);111assertTrue(patch.getBank() == 0);112}113finally114{115bis.close();116}117}118}119120121