gnu.java.nio
Class VMChannel

java.lang.Object
  extended by gnu.java.nio.VMChannel

public final class VMChannel
extends Object

Native interface to support configuring of channel to run in a non-blocking manner and support scatter/gather io operations. JikesRVM-specific implementation by Robin Garner and Filip Pizlo.


Nested Class Summary
(package private) static class VMChannel.Kind
          An enumeration of possible kinds of channel.
private static class VMChannel.LocalByteArray
          A thread-local store of non-moving buffers.
 class VMChannel.State
          A wrapper for a native file descriptor integer.
 
Field Summary
private  VMChannel.Kind kind
           
private static VMChannel.LocalByteArray localByteArray
          Thread-local buffer for VM-side buffering of write calls
private static int MAX_STANDARD_FD
          Don't do fast I/O for the standard file descriptors - these are used for throwing exceptions, so may not be able to allocate.
private  VMChannel.State nfd
          Our reference implementation uses an integer to store the native file descriptor.
 
Constructor Summary
VMChannel()
           
VMChannel(int native_fd)
          This constructor is used by the POSIX reference implementation; other virtual machines need not support it.
 
Method Summary
 VMChannel accept()
          Accept an incoming connection, returning a new VMChannel, or null if the channel is nonblocking and no connection is pending.
private static int accept(int native_fd)
           
 int available()
           
private static int available(int native_fd)
           
 void close()
          Close this socket.
(package private) static void close(int native_fd)
           
 boolean connect(InetSocketAddress saddr, int timeout)
          Connect the underlying socket file descriptor to the remote host.
private static boolean connect(int fd, byte[] addr, int port, int timeout)
           
private static boolean connect6(int fd, byte[] addr, int port, int timeout)
           
 void disconnect()
          Disconnect this channel, if it is a datagram socket.
private static void disconnect(int fd)
           
 boolean flush(boolean metadata)
           
private static boolean flush(int fd, boolean metadata)
           
 InetSocketAddress getLocalAddress()
           
 InetSocketAddress getPeerAddress()
          Returns the socket address of the remote peer this channel is connected to, or null if this channel is not yet connected.
private static int getpeername(int fd, ByteBuffer name)
           
private static int getsockname(int fd, ByteBuffer name)
           
 VMChannel.State getState()
           
static VMChannel getStderr()
           
static VMChannel getStdin()
           
static VMChannel getStdout()
           
private static void initIDs()
           
 void initSocket(boolean stream)
          Create a new socket.
(package private) static boolean isThreadInterrupted()
          Provides a simple mean for the JNI code to find out whether the current thread was interrupted by a call to Thread.interrupt().
private static boolean lock(int fd, long pos, long len, boolean shared, boolean wait)
           
 boolean lock(long pos, long len, boolean shared, boolean wait)
           
 MappedByteBuffer map(char mode, long position, int size)
           
private static MappedByteBuffer map(int fd, char mode, long position, int size)
           
private static int max(int[] values)
           
private static int open(String path, int mode)
           
 void openFile(String path, int mode)
          Open a file at PATH, initializing the native state to operate on that open file.
 long position()
           
private static long position(int fd)
           
 int read()
          Read a single byte.
private  int read(byte[] dst, int pos, int len)
          Reads a byte array directly.
 int read(ByteBuffer dst)
          Read the specified byte buffer.
private  int read(ByteBuffer dst, int pos, int len)
          Read a byte buffer, given a starting position and length.
private static int read(int fd)
           
private static int read(int fd, byte[] dst, int position, int len)
          Use JikesRVM's internal read function - the fast way.
private static int read(int fd, ByteBuffer dst)
          Classpath's native read method.
 long readScattering(ByteBuffer[] dsts, int offset, int length)
          Reads into byte buffers directly using the supplied file descriptor.
private static long readScattering(int fd, ByteBuffer[] dsts, int offset, int length)
           
 SocketAddress receive(ByteBuffer dst)
          Receive a datagram on this channel, returning the host address that sent the datagram.
private static int receive(int fd, ByteBuffer dst, ByteBuffer address)
           
private static void seek(int fd, long pos)
           
 void seek(long pos)
           
 int send(ByteBuffer src, InetSocketAddress dst)
          Send a datagram to the given address.
private static int send(int fd, ByteBuffer src, byte[] addr, int port)
           
private static int send6(int fd, ByteBuffer src, byte[] addr, int port)
           
 void setBlocking(boolean blocking)
          Set the file descriptor to have the required blocking setting.
private static void setBlocking(int fd, boolean blocking)
           
 long size()
           
private static long size(int fd)
           
private static int socket(boolean stream)
          Create a new socket, returning the native file descriptor.
private static int stderr_fd()
           
private static int stdin_fd()
           
private static int stdout_fd()
           
private static void truncate(int fd, long len)
           
 void truncate(long length)
           
private static void unlock(int fd, long pos, long len)
           
 void unlock(long pos, long len)
           
 int write(byte[] src, int pos, int len)
          Writes from a byte array using the supplied file descriptor.
 int write(ByteBuffer src)
           
 int write(ByteBuffer src, int pos, int len)
           
 void write(int b)
          Write a single byte.
private static int write(int fd, byte[] src, int pos, int len)
          Use JikesRVM's internal read function - the fast way.
private static int write(int fd, ByteBuffer src)
          Classpath's native write method.
private static void write(int fd, int b)
           
 long writeGathering(ByteBuffer[] srcs, int offset, int length)
          Writes from byte buffers directly using the supplied file descriptor.
private  long writeGathering(int fd, ByteBuffer[] srcs, int offset, int length)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

nfd

private final VMChannel.State nfd
Our reference implementation uses an integer to store the native file descriptor.


kind

private VMChannel.Kind kind

MAX_STANDARD_FD

private static int MAX_STANDARD_FD
Don't do fast I/O for the standard file descriptors - these are used for throwing exceptions, so may not be able to allocate. Cache the max of the IDs to avoid gratuitous native calls.


localByteArray

private static final VMChannel.LocalByteArray localByteArray
Thread-local buffer for VM-side buffering of write calls

Constructor Detail

VMChannel

public VMChannel()

VMChannel

VMChannel(int native_fd)
    throws IOException
This constructor is used by the POSIX reference implementation; other virtual machines need not support it. Important: do not call this in library code that is not specific to Classpath's reference implementation.

Parameters:
native_fd - The native file descriptor integer.
Throws:
IOException
Method Detail

getState

public VMChannel.State getState()

max

private static int max(int[] values)

getStdin

public static VMChannel getStdin()
                          throws IOException
Throws:
IOException

getStdout

public static VMChannel getStdout()
                           throws IOException
Throws:
IOException

getStderr

public static VMChannel getStderr()
                           throws IOException
Throws:
IOException

stdin_fd

private static int stdin_fd()

stdout_fd

private static int stdout_fd()

stderr_fd

private static int stderr_fd()

setBlocking

public void setBlocking(boolean blocking)
                 throws IOException
Set the file descriptor to have the required blocking setting.

Parameters:
blocking - The blocking flag to set.
Throws:
IOException

setBlocking

private static void setBlocking(int fd,
                                boolean blocking)
                         throws IOException
Throws:
IOException

available

public int available()
              throws IOException
Throws:
IOException

available

private static int available(int native_fd)
                      throws IOException
Throws:
IOException

read

public int read(ByteBuffer dst)
         throws IOException
Read the specified byte buffer.

Parameters:
dst -
Returns:
the number of bytes actually read
Throws:
IOException

read

private int read(ByteBuffer dst,
                 int pos,
                 int len)
          throws IOException
Read a byte buffer, given a starting position and length. Looks at the type of buffer and decides which is the fastest way to perform the write. If the buffer is backed by a byte array, use the internal method, otherwise push it out to classpath's native function (the slow way).

Parameters:
dst -
pos -
len -
Returns:
the number of bytes actually read
Throws:
IOException

read

private int read(byte[] dst,
                 int pos,
                 int len)
          throws IOException
Reads a byte array directly. Performs optimal buffering. If the target buffer is pinned, use it directly. Otherwise allocate one of the thread-local buffers, perform the IO to that, and copy the result to the target array.

Parameters:
dst - Byte array to read to
Returns:
Number of bytes read.
Throws:
IOException - If an error occurs or dst is not a direct buffers.

read

private static int read(int fd,
                        byte[] dst,
                        int position,
                        int len)
                 throws IOException
Use JikesRVM's internal read function - the fast way.

Parameters:
fd - File descriptor
dst - Destination buffer
position - Starting offset in the buffer
len - Number of bytes to read
Returns:
Number of bytes read, or -1 for end of file.
Throws:
IOException

read

private static int read(int fd,
                        ByteBuffer dst)
                 throws IOException
Classpath's native read method. Slow, due to the amount of JNI processing.

Parameters:
fd -
dst -
Returns:
the number of bytes actually read
Throws:
IOException

read

public int read()
         throws IOException
Read a single byte.

Returns:
The byte read, or -1 on end of file.
Throws:
IOException

read

private static int read(int fd)
                 throws IOException
Throws:
IOException

readScattering

public long readScattering(ByteBuffer[] dsts,
                           int offset,
                           int length)
                    throws IOException
Reads into byte buffers directly using the supplied file descriptor. Assumes that the buffer list contains DirectBuffers. Will perform a scattering read.

Parameters:
dsts - An array direct byte buffers.
offset - Index of the first buffer to read to.
length - The number of buffers to read to.
Returns:
Number of bytes read.
Throws:
IOException - If an error occurs or the dsts are not direct buffers.

readScattering

private static long readScattering(int fd,
                                   ByteBuffer[] dsts,
                                   int offset,
                                   int length)
                            throws IOException
Throws:
IOException

receive

public SocketAddress receive(ByteBuffer dst)
                      throws IOException
Receive a datagram on this channel, returning the host address that sent the datagram.

Parameters:
dst - Where to store the datagram.
Returns:
The host address that sent the datagram.
Throws:
IOException

receive

private static int receive(int fd,
                           ByteBuffer dst,
                           ByteBuffer address)
                    throws IOException
Throws:
IOException

write

public int write(byte[] src,
                 int pos,
                 int len)
          throws IOException
Writes from a byte array using the supplied file descriptor.

Parameters:
src - The source buffer.
Returns:
Number of bytes written.
Throws:
IOException

write

public int write(ByteBuffer src,
                 int pos,
                 int len)
          throws IOException
Throws:
IOException

write

public int write(ByteBuffer src)
          throws IOException
Throws:
IOException

write

private static int write(int fd,
                         byte[] src,
                         int pos,
                         int len)
                  throws IOException
Use JikesRVM's internal read function - the fast way.

Parameters:
fd - File descriptor
src - SOurce buffer
pos - Starting offset in the buffer
len - Number of bytes to write
Returns:
Number of bytes written.
Throws:
IOException

write

private static int write(int fd,
                         ByteBuffer src)
                  throws IOException
Classpath's native write method. Slow, due to the amount of JNI processing.

Parameters:
fd -
src -
Returns:
Number of bytes written
Throws:
IOException

writeGathering

public long writeGathering(ByteBuffer[] srcs,
                           int offset,
                           int length)
                    throws IOException
Writes from byte buffers directly using the supplied file descriptor. Assumes the that buffer list contains DirectBuffers. Will perform as gathering write.

Parameters:
srcs -
offset -
length -
Returns:
Number of bytes written.
Throws:
IOException

writeGathering

private long writeGathering(int fd,
                            ByteBuffer[] srcs,
                            int offset,
                            int length)
                     throws IOException
Throws:
IOException

send

public int send(ByteBuffer src,
                InetSocketAddress dst)
         throws IOException
Send a datagram to the given address.

Parameters:
src - The source buffer.
dst - The destination address.
Returns:
The number of bytes written.
Throws:
IOException

send

private static int send(int fd,
                        ByteBuffer src,
                        byte[] addr,
                        int port)
                 throws IOException
Throws:
IOException

send6

private static int send6(int fd,
                         ByteBuffer src,
                         byte[] addr,
                         int port)
                  throws IOException
Throws:
IOException

write

public void write(int b)
           throws IOException
Write a single byte.

Parameters:
b - The byte to write.
Throws:
IOException

write

private static void write(int fd,
                          int b)
                   throws IOException
Throws:
IOException

initIDs

private static void initIDs()

initSocket

public void initSocket(boolean stream)
                throws IOException
Create a new socket. This method will initialize the native file descriptor state of this instance.

Parameters:
stream - Whether or not to create a streaming socket, or a datagram socket.
Throws:
IOException - If creating a new socket fails, or if this channel already has its native descriptor initialized.

socket

private static int socket(boolean stream)
                   throws IOException
Create a new socket, returning the native file descriptor.

Parameters:
stream - Set to true for streaming sockets; false for datagrams.
Returns:
The native file descriptor.
Throws:
IOException - If creating the socket fails.

connect

public boolean connect(InetSocketAddress saddr,
                       int timeout)
                throws SocketException
Connect the underlying socket file descriptor to the remote host.

Parameters:
saddr - The address to connect to.
timeout - The connect timeout to use for blocking connects.
Returns:
True if the connection succeeded; false if the file descriptor is in non-blocking mode and the connection did not immediately succeed.
Throws:
IOException - If an error occurs while connecting.
SocketException

connect

private static boolean connect(int fd,
                               byte[] addr,
                               int port,
                               int timeout)
                        throws SocketException
Throws:
SocketException

connect6

private static boolean connect6(int fd,
                                byte[] addr,
                                int port,
                                int timeout)
                         throws SocketException
Throws:
SocketException

disconnect

public void disconnect()
                throws IOException
Disconnect this channel, if it is a datagram socket. Disconnecting a datagram channel will disassociate it from any address, so the socket will remain open, but can send and receive datagrams from any address.

Throws:
IOException - If disconnecting this channel fails, or if this channel is not a datagram channel.

disconnect

private static void disconnect(int fd)
                        throws IOException
Throws:
IOException

getLocalAddress

public InetSocketAddress getLocalAddress()
                                  throws IOException
Throws:
IOException

getsockname

private static int getsockname(int fd,
                               ByteBuffer name)
                        throws IOException
Throws:
IOException

getPeerAddress

public InetSocketAddress getPeerAddress()
                                 throws IOException
Returns the socket address of the remote peer this channel is connected to, or null if this channel is not yet connected.

Returns:
The peer address.
Throws:
IOException

getpeername

private static int getpeername(int fd,
                               ByteBuffer name)
                        throws IOException
Throws:
IOException

accept

public VMChannel accept()
                 throws IOException
Accept an incoming connection, returning a new VMChannel, or null if the channel is nonblocking and no connection is pending.

Returns:
The accepted connection, or null.
Throws:
IOException - If an IO error occurs.

accept

private static int accept(int native_fd)
                   throws IOException
Throws:
IOException

openFile

public void openFile(String path,
                     int mode)
              throws IOException
Open a file at PATH, initializing the native state to operate on that open file.

Parameters:
path - The absolute file path.
Throws:
IOException - If the file cannot be opened, or if this channel was previously initialized.

open

private static int open(String path,
                        int mode)
                 throws IOException
Throws:
IOException

position

public long position()
              throws IOException
Throws:
IOException

position

private static long position(int fd)
                      throws IOException
Throws:
IOException

seek

public void seek(long pos)
          throws IOException
Throws:
IOException

seek

private static void seek(int fd,
                         long pos)
                  throws IOException
Throws:
IOException

truncate

public void truncate(long length)
              throws IOException
Throws:
IOException

truncate

private static void truncate(int fd,
                             long len)
                      throws IOException
Throws:
IOException

lock

public boolean lock(long pos,
                    long len,
                    boolean shared,
                    boolean wait)
             throws IOException
Throws:
IOException

lock

private static boolean lock(int fd,
                            long pos,
                            long len,
                            boolean shared,
                            boolean wait)
                     throws IOException
Throws:
IOException

unlock

public void unlock(long pos,
                   long len)
            throws IOException
Throws:
IOException

unlock

private static void unlock(int fd,
                           long pos,
                           long len)
                    throws IOException
Throws:
IOException

size

public long size()
          throws IOException
Throws:
IOException

size

private static long size(int fd)
                  throws IOException
Throws:
IOException

map

public MappedByteBuffer map(char mode,
                            long position,
                            int size)
                     throws IOException
Throws:
IOException

map

private static MappedByteBuffer map(int fd,
                                    char mode,
                                    long position,
                                    int size)
                             throws IOException
Throws:
IOException

flush

public boolean flush(boolean metadata)
              throws IOException
Throws:
IOException

flush

private static boolean flush(int fd,
                             boolean metadata)
                      throws IOException
Throws:
IOException

close

public void close()
           throws IOException
Close this socket. The socket is also automatically closed when this object is finalized.

Throws:
IOException - If closing the socket fails, or if this object has no open socket.

close

static void close(int native_fd)
           throws IOException
Throws:
IOException

isThreadInterrupted

static boolean isThreadInterrupted()

Provides a simple mean for the JNI code to find out whether the current thread was interrupted by a call to Thread.interrupt().

Returns:
true if the current thread was interrupted, false otherwise