home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / sun / audio / AudioStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  1.5 KB  |  54 lines

  1. package sun.audio;
  2.  
  3. import java.io.FilterInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6.  
  7. public class AudioStream extends FilterInputStream {
  8.    NativeAudioStream audioIn;
  9.  
  10.    public AudioStream(InputStream in) throws IOException {
  11.       super(in);
  12.  
  13.       try {
  14.          this.audioIn = new NativeAudioStream(in);
  15.       } catch (InvalidAudioFormatException var2) {
  16.          this.audioIn = new AudioTranslatorStream(in);
  17.       }
  18.  
  19.       super.in = this.audioIn;
  20.    }
  21.  
  22.    public int read(byte[] buf, int pos, int len) throws IOException {
  23.       int count = 0;
  24.  
  25.       while(count < len) {
  26.          int n = super.read(buf, pos + count, len - count);
  27.          if (n < 0) {
  28.             return count;
  29.          }
  30.  
  31.          count += n;
  32.          Thread.currentThread();
  33.          Thread.yield();
  34.       }
  35.  
  36.       return count;
  37.    }
  38.  
  39.    public AudioData getData() throws IOException {
  40.       byte[] buffer = new byte[this.audioIn.getLength()];
  41.       int gotbytes = this.read(buffer, 0, this.audioIn.getLength());
  42.       ((FilterInputStream)this).close();
  43.       if (gotbytes != this.audioIn.getLength()) {
  44.          throw new IOException("audio data read error");
  45.       } else {
  46.          return new AudioData(buffer);
  47.       }
  48.    }
  49.  
  50.    public int getLength() {
  51.       return this.audioIn.getLength();
  52.    }
  53. }
  54.