ccs.audio
Class NonBlockingFIFO

java.lang.Object
  extended by java.io.InputStream
      extended by ccs.audio.NonBlockingFIFO
All Implemented Interfaces:
java.io.Closeable

public class NonBlockingFIFO
extends java.io.InputStream

A FIFO which does not allow its methods to block. Bytes may be bulk read out of the stream as for a normal input stream, and methods are provided to allow the FIFO to be written to. MT-Safe.


Constructor Summary
NonBlockingFIFO()
          A 16K FIFO.
NonBlockingFIFO(int capacity)
          A FIFO of defined capacity.
 
Method Summary
 int available()
          The number of bytes available to read.
 void close()
          Close returns the FIFO to a pristine condition.
 int free()
          The number of bytes which could be written.
 boolean markSupported()
          This stream does not support marks.
 int read()
          Read a single byte, unless this would block.
 int read(byte[] b)
          Identical to read(b, 0, b.length).
 int read(byte[] b, int off, int len)
          Read zero or more bytes from the FIFO - this will read zero bytes rather than blocking.
 long skip(long n)
          Skip bytes without blocking.
 void write(byte[] b)
          Identical to write(b, 0, b.length.
 void write(byte[] b, int off, int len)
          Write bytes into the FIFO.
 
Methods inherited from class java.io.InputStream
mark, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NonBlockingFIFO

public NonBlockingFIFO()
A 16K FIFO. This is about as small as is pointful. Most apps will need one considerably bigger.


NonBlockingFIFO

public NonBlockingFIFO(int capacity)
A FIFO of defined capacity.

Method Detail

read

public int read()
         throws java.io.IOException
Read a single byte, unless this would block.

Specified by:
read in class java.io.InputStream
Throws:
WouldBlockException - if it would.
java.io.IOException

read

public int read(byte[] b)
         throws java.io.IOException
Identical to read(b, 0, b.length).

Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Read zero or more bytes from the FIFO - this will read zero bytes rather than blocking. May return less than available.

Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException
Skip bytes without blocking. Note that this uses every get-out clause the contract provides.

Overrides:
skip in class java.io.InputStream
Throws:
java.io.IOException

available

public int available()
The number of bytes available to read.

Overrides:
available in class java.io.InputStream

markSupported

public boolean markSupported()
This stream does not support marks.

Overrides:
markSupported in class java.io.InputStream

close

public void close()
Close returns the FIFO to a pristine condition.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream

free

public int free()
The number of bytes which could be written.


write

public void write(byte[] b)
           throws java.io.IOException
Identical to write(b, 0, b.length.

Throws:
java.io.IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Write bytes into the FIFO. Attempts which would block - ie. len > free - will throw.

Throws:
java.io.IOException