org.norther.tammi.acorn.nio
Class ChannelOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.norther.tammi.acorn.nio.ChannelOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class ChannelOutputStream
extends OutputStream

A non-blocking channel output stream which may wait until ready.

If a selection key is given, the stream sets OP_WRITE to the interest set of the key and waits for the buffer, when it needs it flushed, until a timeout has elapsed or until notified. After the timeout or the notification, it synchronizes the buffer with the channel. The notifier thread may also flush the buffer before notifying the stream or if the stream is not in the wait state.

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

Version:
$Id: ChannelOutputStream.java,v 1.13 2009/09/28 15:08:50 cvsimp Exp $
Author:
Ilkka Priha

Constructor Summary
ChannelOutputStream(SelectionKey key, WritableByteChannel c, ByteBuffer b, long t)
          Constructs a new stream.
 
Method Summary
 void close()
           
 void flush()
           
 long getTimeout()
          Gets the waiting timeout of this stream.
 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.
 void write(byte[] bb, int off, int len)
           
 void write(int b)
           
 
Methods inherited from class java.io.OutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ChannelOutputStream

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

Parameters:
key - the selection key.
c - the writable channel.
b - the buffer in the put state.
t - timeout in msecs to wait until ready.
Method Detail

write

public void write(int b)
           throws IOException
Specified by:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] bb,
                  int off,
                  int len)
           throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

flush

public void flush()
           throws IOException
Specified by:
flush in interface Flushable
Overrides:
flush in class OutputStream
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
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, write in the calling thread.

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


Copyright © 2004 The Norther Organization. All rights reserved.