org.restlet.engine.io
Class NioUtils

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

public class NioUtils
extends Object

Utility methods for NIO processing.

Author:
Jerome Louvel

Method Summary
static int copy(ByteBuffer sourceBuffer, ByteBuffer targetBuffer, long maxCopied)
          Writes the source buffer to the target buffer, up to a maximum number of bytes.
static void copy(FileChannel fileChannel, WritableByteChannel writableChannel)
          Writes the representation to a byte channel.
static void copy(ReadableByteChannel readableChannel, OutputStream outputStream)
          Writes a NIO readable channel to a BIO output stream.
static void copy(ReadableByteChannel readableChannel, WritableByteChannel writableChannel)
          Writes a readable channel to a writable channel.
static ReadableByteChannel getChannel(InputStream inputStream)
          Returns a readable byte channel based on a given input stream.
static WritableByteChannel getChannel(OutputStream outputStream)
          Returns a writable byte channel based on a given output stream.
static ReadableByteChannel getChannel(Representation representation)
          Returns a readable byte channel based on the given representation's content and its write(WritableByteChannel) method.
static InputStream getStream(ReadableByteChannel readableChannel)
          Returns an input stream based on a given readable byte channel.
static OutputStream getStream(WritableByteChannel writableChannel)
          Returns an output stream based on a given writable byte channel.
static boolean isBlocking(Channel channel)
          Indicates if the channel is in blocking mode.
static void release(Selector selector, SelectionKey selectionKey)
          Release the selection key, working around for bug #6403933.
static void waitForState(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
 

Method Detail

copy

public static int copy(ByteBuffer sourceBuffer,
                       ByteBuffer targetBuffer,
                       long maxCopied)
Writes the source buffer to the target buffer, up to a maximum number of bytes.

Parameters:
sourceBuffer - The source buffer.
targetBuffer - The target buffer.
maxCopied - The maximum number of bytes copied by this call or 0 for unlimited length.
Returns:
The number of bytes added to the target buffer.

copy

public static void copy(FileChannel fileChannel,
                        WritableByteChannel writableChannel)
                 throws 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:
IOException

copy

public static void copy(ReadableByteChannel readableChannel,
                        OutputStream outputStream)
                 throws IOException
Writes a NIO readable channel to a BIO output stream.

Parameters:
readableChannel - The readable channel.
outputStream - The output stream.
Throws:
IOException

copy

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

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

getChannel

public static ReadableByteChannel getChannel(InputStream inputStream)
                                      throws IOException
Returns a readable byte channel based on a given input stream. 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.
Throws:
IOException

getChannel

public static WritableByteChannel getChannel(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 ReadableByteChannel getChannel(Representation representation)
                                      throws 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:
IOException

getStream

public static InputStream getStream(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 OutputStream getStream(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.

isBlocking

public static boolean isBlocking(Channel channel)
Indicates if the channel is in blocking mode. It returns false when the channel is selectable and configured to be non blocking.

Parameters:
channel - The channel to test.
Returns:
True if the channel is in blocking mode.

release

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

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

waitForState

public static void waitForState(SelectableChannel selectableChannel,
                                int operations)
                         throws 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:
IOException


Copyright © 2005-2013 Restlet.