org.szegedi.nbpipe
Class NonblockingPipe

java.lang.Object
  |
  +--org.szegedi.nbpipe.NonblockingPipe

public class NonblockingPipe
extends java.lang.Object

NonblockingPipe is a device for bridging between one-connection-per-thread code and the multiple-connections-per-thread (nio) code. The legacy code must be shielded from both the complexity of non-blocking I/O and from getting blocked on I/O. This is where a NonblockingPipe comes in. This pipe uses a ByteBufferPool to satisfy its memory needs, and does not work with an ordinary cyclic buffer that blocks write operations when full. This way, it is ideal for presenting InputStream and OutputStream interfaces for legacy code, while retaining full non-blocking I/O capacity.


Constructor Summary
NonblockingPipe(ByteBufferPool bufferPool)
           
 
Method Summary
 int autoTransferTo()
          Transfers as many bytes are possible from this pipe's buffer to the channel specified in the call to setAutoWriteChannel.
 int available()
          Returns the number of bytes currently available in the pipe.
 void clear()
           
 void closeForWriting()
          Called to signal that no more data will be transferred from an external channel to the pipe.
 java.io.InputStream getInputStream()
          Returns an input stream through which the contents of the pipe can be read.
 java.io.OutputStream getOutputStream()
          Returns an output stream through which the contents of the pipe can be written.
 boolean isExhausted()
          Returns true if the pipe contains no data, and is closed for writing.
 void setAutoWriteChannel(java.nio.channels.WritableByteChannel channel, ChannelWriteListener listener)
          Sets a writable byte channel to which all writes on the output stream are automatically transferred whenever an internal pipe buffer is ready.
 int transferFrom(java.nio.ByteBuffer srcbuf, int maxbytes)
          Transfers as many bytes are possible from the specified byte buffer to this pipe's buffer.
 int transferFrom(java.nio.channels.ReadableByteChannel channel)
           
 int transferFrom(java.nio.channels.ReadableByteChannel channel, int maxbytes)
          Transfers as many bytes are possible from the specified channel to this pipe's buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NonblockingPipe

public NonblockingPipe(ByteBufferPool bufferPool)
Method Detail

setAutoWriteChannel

public void setAutoWriteChannel(java.nio.channels.WritableByteChannel channel,
                                ChannelWriteListener listener)
Sets a writable byte channel to which all writes on the output stream are automatically transferred whenever an internal pipe buffer is ready.

Parameters:
channel - the channel to which bytes are transferred automatically
listener - a listener that gets notified of closing the output stream as well as of any IOExceptions that are thrown during the channel write operations.

getInputStream

public java.io.InputStream getInputStream()
Returns an input stream through which the contents of the pipe can be read. Reading from the stream retrieves the same data that is transferred through autoTransferTo().


getOutputStream

public java.io.OutputStream getOutputStream()
Returns an output stream through which the contents of the pipe can be written. Writing to the stream is equivalent to using the transferFrom method.


available

public int available()
Returns the number of bytes currently available in the pipe.


isExhausted

public boolean isExhausted()
Returns true if the pipe contains no data, and is closed for writing.


autoTransferTo

public int autoTransferTo()
                   throws java.io.IOException
Transfers as many bytes are possible from this pipe's buffer to the channel specified in the call to setAutoWriteChannel. It will transfer until it can write 0 bytes.

Returns:
the number of bytes transferred. -1 is returned if the pipe is exhausted.
java.io.IOException

transferFrom

public int transferFrom(java.nio.channels.ReadableByteChannel channel)
                 throws java.io.IOException
java.io.IOException

transferFrom

public int transferFrom(java.nio.channels.ReadableByteChannel channel,
                        int maxbytes)
                 throws java.io.IOException
Transfers as many bytes are possible from the specified channel to this pipe's buffer. It will transfer until it can read 0 bytes. After this method has been called (potentially multiple times), you should call closeForWriting() to indicate you have written all data to the pipe (or alternatively, you can close the pipe's output stream to achieve the same effect).

Parameters:
channel - the channel to transfer bytes from
maxbytes - maximum number of bytes to transfer
Returns:
the number of bytes transferred
java.io.IOException

transferFrom

public int transferFrom(java.nio.ByteBuffer srcbuf,
                        int maxbytes)
                 throws java.io.IOException
Transfers as many bytes are possible from the specified byte buffer to this pipe's buffer. After this method has been called (potentially multiple times), you should call closeForWriting() to indicate you have written all data to the pipe (or alternatively, you can close the pipe's output stream to achieve the same effect).

Parameters:
srcbuf - the buffer to transfer bytes from
maxbytes - maximum number of bytes to transfer
Returns:
the number of bytes transferred
java.io.IOException

clear

public void clear()

closeForWriting

public void closeForWriting()
Called to signal that no more data will be transferred from an external channel to the pipe.