home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / audio / AudioStreamSequence.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  2.6 KB  |  94 lines

  1. /*
  2.  * @(#)AudioStreamSequence.java    1.10 95/08/12 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package sun.audio;
  21.  
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.util.Enumeration;
  25.  
  26. /**
  27.  * Convert a sequence of input streams into a single InputStream.
  28.  * This class can be used to play two audio clips in sequence.<p>
  29.  * For example:
  30.  * <pre>
  31.  *    Vector v = new Vector();
  32.  *    v.addElement(audiostream1);
  33.  *    v.addElement(audiostream2);
  34.  *    AudioStreamSequence audiostream = new AudioStreamSequence(v.elements());
  35.  *    AudioPlayer.player.start(audiostream);
  36.  * </pre>
  37.  * @see AudioPlayer
  38.  * @author Arthur van Hoff
  39.  * @version     1.10, 08/12/95
  40.  */
  41. public
  42. class AudioStreamSequence extends InputStream {
  43.     Enumeration e;
  44.     InputStream in;
  45.     
  46.     /**
  47.      * Create an AudioStreamSequence given an
  48.      * enumeration of streams.
  49.      */
  50.     public AudioStreamSequence(Enumeration e) {
  51.     this.e = e;
  52.     in = e.hasMoreElements() ? (InputStream)e.nextElement() : null;
  53.     }
  54.  
  55.     /**
  56.      * Read, when reaching an EOF, flip to the next stream.
  57.      */
  58.     public int read() throws IOException {
  59.     if (in == null) {
  60.         return -1;
  61.     }
  62.     int c = in.read();
  63.     if (c == -1) {
  64.         in.close();
  65.         in = null;
  66.         while (e.hasMoreElements() && (in == null)) {
  67.         in = (InputStream)e.nextElement();
  68.         }
  69.         return read();
  70.     }
  71.     return c;
  72.     }
  73.  
  74.     /**
  75.      * Read, when reaching an EOF, flip to the next stream.
  76.      */
  77.     public int read(byte buf[], int pos, int len) throws IOException {
  78.     if (in == null) {
  79.         return -1;
  80.     }
  81.     int n = in.read(buf, pos, len);
  82.     if (n < len) {
  83.         if (n < 0) {
  84.         n = 0;
  85.         }
  86.         in.close();
  87.         in = e.hasMoreElements() ? (InputStream)e.nextElement() : null;
  88.         int m = read(buf, pos + n, len - n);
  89.         return (m > 0) ? n + m : n;
  90.     }
  91.     return n;
  92.     }
  93. }
  94.