org.norther.tammi.acorn.nio
Class ChannelInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.norther.tammi.acorn.nio.ChannelInputStream
All Implemented Interfaces:
Closeable

public class ChannelInputStream
extends InputStream

A non-blocking channel input stream which may wait before ready.

If the timeout is positive, the stream adds OP_READ to the interest set of the key and waits for the buffer when it needs a filled one until a timeout has elapsed or until notified. If the timeout is negative, the interest set of the key is not modified and the stream waits for the buffer without a timeout until notified. The latter alternative allows better control over the selection process from outside of the stream.

Note that I/O operations are synchronized on the buffer.

Author:
Ilkka Priha

Constructor Summary
ChannelInputStream(SelectionKey key, ReadableByteChannel c, ByteBuffer b, long t)
          Constructs a new stream.
 
Method Summary
 int available()
           
 void close()
           
 long getTimeout()
          Gets the waiting timeout of this stream.
 void mark(int readlimit)
           
 boolean markSupported()
           
 int read()
           
 int read(byte[] bb, int off, int len)
           
 void reset()
           
 void setTimeout(long t)
          Sets the waiting timeout of this stream.
 boolean sync()
          Syncs the stream with the channel by notifying the thread waiting on the buffer.
 
Methods inherited from class java.io.InputStream
read, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ChannelInputStream

public ChannelInputStream(SelectionKey key,
                          ReadableByteChannel c,
                          ByteBuffer b,
                          long t)
Constructs a new stream.

Parameters:
key - the selection key.
c - the readable channel.
b - the buffer in the get state.
t - timeout in msecs to wait for the buffer.
Method Detail

available

public int available()
              throws IOException
Overrides:
available in class InputStream
Throws:
IOException

read

public int read()
         throws IOException
Specified by:
read in class InputStream
Throws:
IOException

read

public int read(byte[] bb,
                int off,
                int len)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

mark

public void mark(int readlimit)
Overrides:
mark in class InputStream

reset

public void reset()
           throws IOException
Overrides:
reset in class InputStream
Throws:
IOException

markSupported

public boolean markSupported()
Overrides:
markSupported in class InputStream

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException

getTimeout

public long getTimeout()
Gets the waiting timeout of this stream.

Returns:
the timeout in msecs.

setTimeout

public void setTimeout(long t)
Sets the waiting timeout of this stream.

Parameters:
t - the timeout in msecs.

sync

public boolean sync()
             throws IOException
Syncs the stream with the channel by notifying the thread waiting on the buffer. If none is available, read in the calling thread.

Returns:
true if synced, false otherwise.
Throws:
IOException - on I/O errors.


Copyright © 2004 The Norther Organization. All rights reserved.