1 /*
   2  * Copyright (c) 2007, 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 /* @test
  25  @summary Test DLSSoundbankReader getSoundbank(InputStream) method using
  26      very bad InputStream which can only read 1 byte at time */
  27 
  28 import java.io.BufferedInputStream;
  29 import java.io.File;
  30 import java.io.FileInputStream;
  31 import java.io.IOException;
  32 import java.io.InputStream;
  33 
  34 import javax.sound.midi.Patch;
  35 import javax.sound.midi.Soundbank;
  36 
  37 import com.sun.media.sound.DLSSoundbankReader;
  38 
  39 public class TestGetSoundbankInputStream2 {
  40 
  41     private static class BadInputStream extends InputStream
  42     {
  43 
  44         InputStream is;
  45 
  46         public BadInputStream(InputStream is)
  47         {
  48             this.is = is;
  49         }
  50 
  51         public int read() throws IOException {
  52             return is.read();
  53         }
  54 
  55         public int read(byte[] b, int off, int len) throws IOException {
  56             if(len > 1) len = 1;
  57             return is.read(b, off, len);
  58         }
  59 
  60         public int read(byte[] b) throws IOException {
  61             return read(b, 0, b.length);
  62         }
  63 
  64         public long skip(long n) throws IOException {
  65             if(n > 1) n = 1;
  66             return is.skip(n);
  67         }
  68 
  69         public int available() throws IOException {
  70             int avail = is.available();
  71             if(avail > 1) avail = 1;
  72             return avail;
  73         }
  74 
  75         public void close() throws IOException {
  76             is.close();
  77         }
  78 
  79         public synchronized void mark(int readlimit) {
  80             is.mark(readlimit);
  81         }
  82 
  83         public boolean markSupported() {
  84             return is.markSupported();
  85         }
  86 
  87         public synchronized void reset() throws IOException {
  88             is.reset();
  89         }
  90 
  91     }
  92 
  93     private static void assertTrue(boolean value) throws Exception
  94     {
  95         if(!value)
  96             throw new RuntimeException("assertTrue fails!");
  97     }
  98 
  99     public static void main(String[] args) throws Exception {
 100         File file = new File(System.getProperty("test.src", "."), "ding.dls");
 101         FileInputStream fis = new FileInputStream(file);
 102         BufferedInputStream bis = new BufferedInputStream(fis);
 103         try
 104         {
 105             InputStream badis = new BadInputStream(bis);
 106             Soundbank dls = new DLSSoundbankReader().getSoundbank(badis);
 107             assertTrue(dls.getInstruments().length == 1);
 108             Patch patch = dls.getInstruments()[0].getPatch();
 109             assertTrue(patch.getProgram() == 0);
 110             assertTrue(patch.getBank() == 0);
 111         }
 112         finally
 113         {
 114             bis.close();
 115         }
 116     }
 117 }