1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.IOException;
  26 import java.io.InputStream;
  27 
  28 import javax.sound.midi.InvalidMidiDataException;
  29 import javax.sound.midi.MidiSystem;
  30 
  31 /**
  32  * @test
  33  * @bug 4629810
  34  * @summary MidiSystem.getSoundbank() throws unexpected IOException
  35  */
  36 public class GetSoundBankIOException {
  37 
  38     public static void main(String args[]) throws Exception {
  39         boolean failed = false;
  40         try {
  41             String filename = "GetSoundBankIOException.java";
  42             System.out.println("Opening "+filename+" as soundbank...");
  43             File midiFile = new File(System.getProperty("test.src", "."), filename);
  44             MidiSystem.getSoundbank(midiFile);
  45             //Soundbank sBank = MidiSystem.getSoundbank(new NonMarkableIS());
  46             System.err.println("InvalidMidiDataException was not thrown!");
  47             failed = true;
  48         } catch (InvalidMidiDataException invMidiEx) {
  49             System.err.println("InvalidMidiDataException was thrown. OK.");
  50         } catch (IOException ioEx) {
  51             System.err.println("Unexpected IOException was caught!");
  52             System.err.println(ioEx.getMessage());
  53             ioEx.printStackTrace();
  54             failed = true;
  55         }
  56 
  57         if (failed) throw new Exception("Test FAILED!");
  58         System.out.println("Test passed.");
  59     }
  60 
  61     private static class NonMarkableIS extends InputStream {
  62         int counter = 0;
  63 
  64         public NonMarkableIS() {
  65         }
  66 
  67         public int read() throws IOException {
  68             if (counter > 1000) return -1;
  69             return (++counter) % 256;
  70         }
  71 
  72         public synchronized void mark(int readlimit) {
  73             System.out.println("Called mark with readlimit= "+readlimit);
  74         }
  75 
  76         public synchronized void reset() throws IOException {
  77             throw new IOException("mark/reset not supported");
  78         }
  79 
  80         public boolean markSupported() {
  81             return false;
  82         }
  83 
  84     }
  85 }