org.restlet.engine.io
Class NioUtils

java.lang.Object
  extended by org.restlet.engine.io.NioUtils

public class NioUtils
extends java.lang.Object

Utility methods for NIO processing.

Author:
Jerome Louvel

Field Summary
static int NIO_TIMEOUT
          The number of milliseconds after which NIO operation will time out.
 
Method Summary
static void copy(java.nio.channels.FileChannel fileChannel, java.nio.channels.WritableByteChannel writableChannel)
          Writes the representation to a byte channel.
static void copy(java.nio.channels.ReadableByteChannel readableChannel, java.nio.channels.WritableByteChannel writableChannel)
          Writes a readable channel to a writable channel.
static java.nio.channels.ReadableByteChannel getChannel(java.io.InputStream inputStream)
          Returns a readable byte channel based on a given inputstream.
static java.nio.channels.WritableByteChannel getChannel(java.io.OutputStream outputStream)
          Returns a writable byte channel based on a given output stream.
static java.nio.channels.ReadableByteChannel getChannel(Representation representation)
          Returns a readable byte channel based on the given representation's content and its write(WritableByteChannel) method.
static java.io.InputStream getStream(java.nio.channels.ReadableByteChannel readableChannel)
          Returns an input stream based on a given readable byte channel.
static java.io.OutputStream getStream(java.nio.channels.WritableByteChannel writableChannel)
          Returns an output stream based on a given writable byte channel.
static void release(java.nio.channels.Selector selector, java.nio.channels.SelectionKey selectionKey)
          Release the selection key, working around for bug #6403933.
static void waitForState(java.nio.channels.SelectableChannel selectableChannel, int operations)
          Waits for the given channel to be ready for a specific operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NIO_TIMEOUT

public static final int NIO_TIMEOUT
The number of milliseconds after which NIO operation will time out.

See Also:
Constant Field Values
Method Detail

copy

public static void copy(java.nio.channels.FileChannel fileChannel,
                        java.nio.channels.WritableByteChannel writableChannel)
                 throws java.io.IOException
Writes the representation to a byte channel. Optimizes using the file channel transferTo method.

Parameters:
fileChannel - The readable file channel.
writableChannel - A writable byte channel.
Throws:
java.io.IOException

copy

public static void copy(java.nio.channels.ReadableByteChannel readableChannel,
                        java.nio.channels.WritableByteChannel writableChannel)
                 throws java.io.IOException
Writes a readable channel to a writable channel.

Parameters:
readableChannel - The readable channel.
writableChannel - The writable channel.
Throws:
java.io.IOException

getChannel

public static java.nio.channels.ReadableByteChannel getChannel(java.io.InputStream inputStream)
Returns a readable byte channel based on a given inputstream. If it is supported by a file a read-only instance of FileChannel is returned.

Parameters:
inputStream - The input stream to convert.
Returns:
A readable byte channel.

getChannel

public static java.nio.channels.WritableByteChannel getChannel(java.io.OutputStream outputStream)
Returns a writable byte channel based on a given output stream.

Parameters:
outputStream - The output stream.
Returns:
A writable byte channel.

getChannel

public static java.nio.channels.ReadableByteChannel getChannel(Representation representation)
                                                        throws java.io.IOException
Returns a readable byte channel based on the given representation's content and its write(WritableByteChannel) method. Internally, it uses a writer thread and a pipe channel.

Parameters:
representation - the representation to get the OutputStream from.
Returns:
A readable byte channel.
Throws:
java.io.IOException

getStream

public static java.io.InputStream getStream(java.nio.channels.ReadableByteChannel readableChannel)
Returns an input stream based on a given readable byte channel.

Parameters:
readableChannel - The readable byte channel.
Returns:
An input stream based on a given readable byte channel.

getStream

public static java.io.OutputStream getStream(java.nio.channels.WritableByteChannel writableChannel)
Returns an output stream based on a given writable byte channel.

Parameters:
writableChannel - The writable byte channel.
Returns:
An output stream based on a given writable byte channel.

release

public static void release(java.nio.channels.Selector selector,
                           java.nio.channels.SelectionKey selectionKey)
                    throws java.io.IOException
Release the selection key, working around for bug #6403933.

Parameters:
selector - The associated selector.
selectionKey - The used selection key.
Throws:
java.io.IOException

waitForState

public static void waitForState(java.nio.channels.SelectableChannel selectableChannel,
                                int operations)
                         throws java.io.IOException
Waits for the given channel to be ready for a specific operation.

Parameters:
selectableChannel - The channel to monitor.
operations - The operations to be ready to do.
Throws:
java.io.IOException


Copyright © 2005-2010 Noelios Technologies.