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

  1. /*
  2. /*
  3.  * @(#)AudioStream.java    1.10 95/08/22 Arthur van Hoff
  4.  *
  5.  * Copyright (c) 1994, 1995 Sun Microsystems, Inc. All Rights Reserved.
  6.  *
  7.  * Permission to use, copy, modify, and distribute this software
  8.  * and its documentation for NON-COMMERCIAL purposes and without
  9.  * fee is hereby granted provided that this copyright notice
  10.  * appears in all copies. Please refer to the file "copyright.html"
  11.  * for further important copyright and licensing information.
  12.  *
  13.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  14.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  15.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  16.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  17.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  18.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  19.  */
  20.  
  21. package sun.audio;
  22.  
  23. import java.io.InputStream;
  24. import java.io.DataInputStream;
  25. import java.io.FilterInputStream;
  26. import java.io.IOException;
  27.  
  28. /**
  29.  * Convert an InputStream to an AudioStream. 
  30.  *
  31.  * @version     1.10, 08/22/95
  32.  */
  33. public
  34. class AudioStream extends FilterInputStream {
  35.  
  36.     NativeAudioStream audioIn;
  37.     
  38.     public AudioStream(InputStream in) throws IOException {
  39.     super(in);
  40.     try {
  41.         audioIn = (new NativeAudioStream(in));
  42.     } catch (InvalidAudioFormatException e) {
  43.         // Not a native audio stream -- use a translator (if available).
  44.         // If not, let the exception bubble up.
  45.         audioIn = (new AudioTranslatorStream(in));
  46.     }
  47.     this.in = audioIn;
  48.     }
  49.  
  50.     /**
  51.      * A blocking read.
  52.      */
  53.     public int read(byte buf[], int pos, int len) throws IOException {
  54.     int count = 0;
  55.     while (count < len) {
  56.         int n = super.read(buf, pos + count, len - count);
  57.         if (n < 0) {
  58.         return count;
  59.         }
  60.         count += n;
  61.         Thread.currentThread().yield();
  62.     }
  63.     return count;
  64.     }
  65.  
  66.     /**
  67.      * Get the data.
  68.      */
  69.     public AudioData getData() throws IOException {
  70.     byte buffer[] = new byte[audioIn.getLength()];
  71.     int gotbytes = read(buffer, 0, audioIn.getLength());
  72.     close();
  73.     if (gotbytes != audioIn.getLength()) {
  74.         throw new IOException("audio data read error");
  75.     }
  76.     return new AudioData(buffer);
  77.     }
  78.     
  79.     public int getLength() {
  80.     return audioIn.getLength();
  81.     }
  82. }
  83.