com.mindbright.nio
Class NonBlockingInput

java.lang.Object
  extended by com.mindbright.nio.NonBlockingInput

public class NonBlockingInput
extends java.lang.Object

A non-blocking input stream.


Constructor Summary
  NonBlockingInput(java.nio.channels.Pipe pipe)
          Create a new NonBlockingInput object which reads data from the given pipe.
protected NonBlockingInput(Switchboard switchboard, java.nio.channels.spi.AbstractSelectableChannel channel)
           
 
Method Summary
protected  void clearReadWaiting()
           
 void close()
          Close the underlying stream.
 java.nio.ByteBuffer createBuffer(byte[] array)
          Create a ByteBuffer wrapped around the provided array
 java.nio.ByteBuffer createBuffer(byte[] array, int offset)
          Create a ByteBuffer wrapped around the provided array
 java.nio.ByteBuffer createBuffer(byte[] array, int offset, int length)
          Create a ByteBuffer wrapped around the provided array
 void pushback(byte[] data, int offset, int length)
          Push back data onto the stream so that future reads will get it.
 void read(java.nio.ByteBuffer buf)
          Reads data from the stream and places it in the provided buffer.
 boolean read(java.nio.ByteBuffer buf, int offset, int length, NIOCallback callback, boolean mayShortcut, boolean shortDataOk)
          Reads data from the stream and places it in the provided buffer.
 void read(java.nio.ByteBuffer buf, NIOCallback callback)
          Reads data from the stream and places it in the provided buffer.
 boolean read(java.nio.ByteBuffer buf, NIOCallback callback, boolean mayShortcut, boolean shortDataOk)
          Reads data from the stream and places it in the provided buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NonBlockingInput

protected NonBlockingInput(Switchboard switchboard,
                           java.nio.channels.spi.AbstractSelectableChannel channel)
                    throws java.io.IOException
Throws:
java.io.IOException

NonBlockingInput

public NonBlockingInput(java.nio.channels.Pipe pipe)
                 throws java.io.IOException
Create a new NonBlockingInput object which reads data from the given pipe.

Parameters:
pipe - The pipe the object will read from
Throws:
java.io.IOException
Method Detail

read

public void read(java.nio.ByteBuffer buf)
          throws java.io.IOException
Reads data from the stream and places it in the provided buffer. This call will block until all the needed data has been read.

The method will fill the buffer from the current position to the limit.

Parameters:
buf - buffer in which to store the read data
Throws:
java.io.IOException

read

public void read(java.nio.ByteBuffer buf,
                 NIOCallback callback)
          throws java.io.IOException
Reads data from the stream and places it in the provided buffer. The provided callback will be called when the data has been read.

The method will fill the buffer from the current position to the limit.

Parameters:
buf - buffer in which to store the read data
callback - class to notify when data has been read
Throws:
java.io.IOException

read

public boolean read(java.nio.ByteBuffer buf,
                    NIOCallback callback,
                    boolean mayShortcut,
                    boolean shortDataOk)
             throws java.io.IOException
Reads data from the stream and places it in the provided buffer. The provided callback will be called when the data has been read unless the data is already available. If the data is available immediately and the mayShortcut parameter is true, then the callback will not be called but the function will return true.

The method will try to fill the buffer from the current position to the limit. But if shortDataOk is true then the callback will be called as soon as at least one byte has been read.

Parameters:
buf - buffer in which to store the read data
callback - class to notify when data has been read (unless data is available immediately)
mayShortcut - If this is true then the function will not call the callback if data is available immediately but instead return true.
shortDataOk - If this is ok then the call will return/call the callback as soon as data is available, even if the buffer is not full.
Returns:
returns true if the data is available directly and the mayShortcut parameter is true. In this case the callback will not be called.
Throws:
java.io.IOException

read

public boolean read(java.nio.ByteBuffer buf,
                    int offset,
                    int length,
                    NIOCallback callback,
                    boolean mayShortcut,
                    boolean shortDataOk)
             throws java.io.IOException
Reads data from the stream and places it in the provided buffer. The provided callback will be called when the data has been read unless the data is already available. If the data is available immediately and the mayShortcut parameter is true, then the callback will not be called but the function will return true.

The method will try to fill the buffer from the current position to the limit. But if shortDataOk is true then the callback will be called as soon as at least one byte has been read.

Parameters:
buf - buffer in which to store the read data
callback - class to notify when data has been read (unless data is available immediately)
mayShortcut - If this is true then the function will not call the callback if data is available immediately but instead return true.
shortDataOk - If this is ok then the call will return/call the callback as soon as data is available, even if the buffer is not full.
Returns:
returns true if the data is available directly and the mayShortcut parameter is true. In this case the callback will not be called.
Throws:
java.io.IOException

clearReadWaiting

protected void clearReadWaiting()

pushback

public void pushback(byte[] data,
                     int offset,
                     int length)
Push back data onto the stream so that future reads will get it.

Parameters:
data - array holding data to push back
offset - offset to start getting data from
length - number of bytes to push back

close

public void close()
           throws java.io.IOException
Close the underlying stream. It will not be possible to perform any further reads.

Throws:
java.io.IOException

createBuffer

public java.nio.ByteBuffer createBuffer(byte[] array)
Create a ByteBuffer wrapped around the provided array


createBuffer

public java.nio.ByteBuffer createBuffer(byte[] array,
                                        int offset)
Create a ByteBuffer wrapped around the provided array


createBuffer

public java.nio.ByteBuffer createBuffer(byte[] array,
                                        int offset,
                                        int length)
Create a ByteBuffer wrapped around the provided array