com.sun.sgs.impl.nio
Class AsyncSocketChannelImpl

java.lang.Object
  extended by com.sun.sgs.nio.channels.AsynchronousChannel
      extended by com.sun.sgs.nio.channels.AsynchronousSocketChannel
          extended by com.sun.sgs.impl.nio.AsyncSocketChannelImpl
All Implemented Interfaces:
AsynchronousByteChannel, NetworkChannel, Closeable, Channel

 class AsyncSocketChannelImpl
extends AsynchronousSocketChannel

An implementation of AsynchronousSocketChannel. Most interesting methods are delegated to the AsyncKey returned by this channel's channel group.


Field Summary
(package private)  SocketChannel channel
          The underlying SocketChannel.
(package private)  AsyncKey key
          The AsyncKey for the underlying channel.
 
Constructor Summary
AsyncSocketChannelImpl(AsyncGroupImpl group)
          Creates a new instance registered with the given channel group.
AsyncSocketChannelImpl(AsyncGroupImpl group, SocketChannel channel)
          Creates a new instance registered with the given channel group, with the given underlying SocketChannel.
 
Method Summary
 AsyncSocketChannelImpl bind(SocketAddress local)
          Binds the channel's socket to a local address.
 void close()
          Closes this channel.
<A> IoFuture<Void,A>
connect(SocketAddress remote, A attachment, CompletionHandler<Void,? super A> handler)
          Connects this channel.
 SocketAddress getConnectedAddress()
          Returns the remote address to which this channel's socket is connected, or null if the channel's socket is not connected.
 SocketAddress getLocalAddress()
          Returns the socket address that this channel's socket is bound to, or null if the socket is not bound.
 Object getOption(SocketOption name)
          Returns the value of a socket option.
 boolean isConnectionPending()
          Tells whether or not a connect is pending for this channel.
 boolean isOpen()
          
 boolean isReadPending()
          Tells whether or not a read is pending for this channel.
 boolean isWritePending()
          Tells whether or not a write is pending for this channel.
 Set<SocketOption> options()
          Returns a set of the socket options supported by this channel.
<A> IoFuture<Long,A>
read(ByteBuffer[] dsts, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler<Long,? super A> handler)
          Reads a sequence of bytes from this channel into a subsequence of the given buffers.
<A> IoFuture<Integer,A>
read(ByteBuffer dst, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer,? super A> handler)
          Reads a sequence of bytes from this channel into the given buffer.
 AsyncSocketChannelImpl setOption(SocketOption name, Object value)
          Sets the value of a socket option.
 AsyncSocketChannelImpl shutdown(ShutdownType how)
          Shutdown a connection for reading and/or writing without closing the channel.
 String toString()
          
<A> IoFuture<Long,A>
write(ByteBuffer[] srcs, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler<Long,? super A> handler)
          Writes a sequence of bytes to this channel from a subsequence of the given buffers.
<A> IoFuture<Integer,A>
write(ByteBuffer src, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer,? super A> handler)
          Writes a sequence of bytes to this channel from the given buffer.
 
Methods inherited from class com.sun.sgs.nio.channels.AsynchronousSocketChannel
connect, open, open, read, read, write, write
 
Methods inherited from class com.sun.sgs.nio.channels.AsynchronousChannel
provider
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

channel

final SocketChannel channel
The underlying SocketChannel.


key

final AsyncKey key
The AsyncKey for the underlying channel.

Constructor Detail

AsyncSocketChannelImpl

AsyncSocketChannelImpl(AsyncGroupImpl group)
                 throws IOException
Creates a new instance registered with the given channel group.

Parameters:
group - the channel group
Throws:
IOException - if an I/O error occurs

AsyncSocketChannelImpl

AsyncSocketChannelImpl(AsyncGroupImpl group,
                       SocketChannel channel)
                 throws IOException
Creates a new instance registered with the given channel group, with the given underlying SocketChannel. Used by an AsyncServerSocketChannelImpl when a new connection is accepted.

Parameters:
group - the channel group
channel - the SocketChannel for this async channel
Throws:
IOException - if an I/O error occurs
Method Detail

toString

public String toString()

Overrides:
toString in class Object

isOpen

public boolean isOpen()


close

public void close()
           throws IOException
Closes this channel.

Any outstanding asynchronous operations upon this channel will complete by throwing ExecutionException with cause AsynchronousCloseException.

This method otherwise behaves exactly as specified by the Channel interface.

Specified by:
close in interface Closeable
Specified by:
close in interface Channel
Specified by:
close in class AsynchronousChannel
Throws:
IOException - if an I/O error occurs

bind

public AsyncSocketChannelImpl bind(SocketAddress local)
                            throws IOException
Binds the channel's socket to a local address.

This method is used to establish an association between the socket and a local address. Once an association is established then the socket remains bound until the channel is closed. An attempt to bind a socket that is already bound throws AlreadyBoundException. If the local parameter has the value null then the socket will be bound to an address that is assigned automatically.

An implementation of this interface should specify if a permission is required when a security manager is installed.

Specified by:
bind in interface NetworkChannel
Specified by:
bind in class AsynchronousSocketChannel
Parameters:
local - the address to bind the socket, or null to bind the socket to an automatically assigned socket address
Returns:
this channel
Throws:
ClosedChannelException - if the channel is closed
IOException - if some other I/O error occurs

getLocalAddress

public SocketAddress getLocalAddress()
                              throws IOException
Returns the socket address that this channel's socket is bound to, or null if the socket is not bound.

Returns:
the socket address that the socket is bound to, or null if the channel is not open or the channel's socket is not bound
Throws:
IOException - if an I/O error occurs

setOption

public AsyncSocketChannelImpl setOption(SocketOption name,
                                        Object value)
                                 throws IOException
Sets the value of a socket option.

The name parameter is the name of the socket option. The value parameter is the value of the option and is of the type specified by the option. A value of null may be a valid value for some socket options.

Specified by:
setOption in interface NetworkChannel
Specified by:
setOption in class AsynchronousSocketChannel
Parameters:
name - the name of the socket option
value - the value of the socket option
Returns:
this channel
Throws:
ClosedChannelException - if this channel is closed
IOException - if an I/O error occurs
See Also:
SocketOption

getOption

public Object getOption(SocketOption name)
                 throws IOException
Returns the value of a socket option.

The return type is specific to the socket option and null may be a valid value for some socket options.

Parameters:
name - the socket option
Returns:
the value of the socket option
Throws:
ClosedChannelException - if this channel is closed
IOException - if an I/O error occurs
See Also:
SocketOption

options

public Set<SocketOption> options()
Returns a set of the socket options supported by this channel.

This method will continue to return the set of options even after the channel has been closed.

Returns:
a set of the socket options supported by this channel

shutdown

public AsyncSocketChannelImpl shutdown(ShutdownType how)
                                throws IOException
Shutdown a connection for reading and/or writing without closing the channel.

The how parameter specifies if the input, output, or both sides of the connection is shutdown. If the input side of the connection is shutdown then further read operations on the channel will return -1, the end-of-stream indication. If the input side of the connection is already shutdown then invoking this method to shutdown the input side of the connection has no effect. If the output side of the connection is shutdown then further write operations on the channel will complete immediately by throwning ExecutionException with cause ClosedChannelException. If the output side of the connection is already shutdown then invoking this method to shutdown the output side of the connection has no effect.

Specified by:
shutdown in class AsynchronousSocketChannel
Parameters:
how - specifies if the input, output, or both sides of the connection is shutdown
Returns:
this channel
Throws:
ClosedChannelException - if this channel is closed
IOException - if some other I/O error occurs

getConnectedAddress

public SocketAddress getConnectedAddress()
                                  throws IOException
Returns the remote address to which this channel's socket is connected, or null if the channel's socket is not connected.

Specified by:
getConnectedAddress in class AsynchronousSocketChannel
Returns:
the remote address; null if the channel is not open or the channel's socket is not connected
Throws:
IOException - if an I/O error occurs

isConnectionPending

public boolean isConnectionPending()
Tells whether or not a connect is pending for this channel.

The result of this method is a snapshot of the channel state. It may be invalid when the caller goes to examine the result and should not be used for purposes of coordination.

Specified by:
isConnectionPending in class AsynchronousSocketChannel
Returns:
true if, and only if, a connect is pending for this channel but has not yet completed

isReadPending

public boolean isReadPending()
Tells whether or not a read is pending for this channel.

The result of this method is a snapshot of the channel state. It may be invalid when the caller goes to examine the result and should not be used for purposes of coordination.

Specified by:
isReadPending in class AsynchronousSocketChannel
Returns:
true if, and only if, a read is pending for this this channel but has not yet completed
See Also:
ReadPendingException

isWritePending

public boolean isWritePending()
Tells whether or not a write is pending for this channel.

The result of this method is a snapshot of the channel state. It may be invalid when the caller goes to examine the result and should not be used for purposes of coordination.

Specified by:
isWritePending in class AsynchronousSocketChannel
Returns:
true if, and only if, a write is pending for this this channel but has not yet completed
See Also:
WritePendingException

connect

public <A> IoFuture<Void,A> connect(SocketAddress remote,
                                    A attachment,
                                    CompletionHandler<Void,? super A> handler)
Connects this channel.

This method initiates an operation to connect this channel, returning a IoFuture representing the pending result of the operation. If the connection is successfully established then the IoFuture's get method will return null, otherwise it throws ExecutionException with the approach cause.

This method performs exactly the same security checks as the Socket class. That is, if a security manager has been installed then this method verifies that its checkConnect method permits connecting to the address and port number of the given remote endpoint.

Specified by:
connect in class AsynchronousSocketChannel
Type Parameters:
A - the attachment type
Parameters:
remote - the remote address to which this channel is to be connected
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
See Also:
AsynchronousSocketChannel.getConnectedAddress(), AsynchronousSocketChannel.isConnectionPending()

read

public <A> IoFuture<Integer,A> read(ByteBuffer dst,
                                    long timeout,
                                    TimeUnit unit,
                                    A attachment,
                                    CompletionHandler<Integer,? super A> handler)
Reads a sequence of bytes from this channel into the given buffer.

This method initiates the reading of a sequence of bytes from this channel into the given buffer, returning an IoFuture representing the pending result of the operation. The IoFuture's get method returns the number of bytes read, possibly zero, or -1 if all bytes have been read and channel has reached end-of-stream.

If a timeout is specified and the timeout elapses before the operation completes then it completes with ExecutionException and cause AbortedByTimeoutException. In that case it is guranteed that no bytes have been read from the channel into the given buffer.

Otherwise this method works in the same manner as the AsynchronousByteChannel.read(ByteBuffer,A,CompletionHandler) method.

Specified by:
read in class AsynchronousSocketChannel
Type Parameters:
A - the attachment type
Parameters:
dst - the buffer into which bytes are to be transferred
timeout - the timeout, or 0L for no timeout
unit - the time unit of the timeout argument
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result

read

public <A> IoFuture<Long,A> read(ByteBuffer[] dsts,
                                 int offset,
                                 int length,
                                 long timeout,
                                 TimeUnit unit,
                                 A attachment,
                                 CompletionHandler<Long,? super A> handler)
Reads a sequence of bytes from this channel into a subsequence of the given buffers.

This method initiates the reading of a sequence of bytes from this channel into a subsequence of the given buffers, returning an IoFuture representing the pending result of the operation. The IoFuture's get method returns the number of bytes read, possibly zero, or -1 if all bytes have been read and channel has reached end-of-stream.

This method initiates a read of up to r bytes from this channel, where r is the total number of bytes remaining in the specified subsequence of the given buffer array, that is,

      dsts[offset].remaining()
          + dsts[offset+1].remaining()
          + ... + dsts[offset+length-1].remaining()
 
at the moment that the read is attempted.

Suppose that a byte sequence of length n is read, where 0 <= n <= r. Up to the first dsts[offset].remaining() bytes of this sequence are transferred into buffer dsts[offset], up to the next dsts[offset+1].remaining() bytes are transferred into buffer dsts[offset+1], and so forth, until the entire byte sequence is transferred into the given buffers. As many bytes as possible are transferred into each buffer, hence the final position of each updated buffer, except the last updated buffer, is guaranteed to be equal to that buffer's limit.

If a timeout is specified and the timeout elapses before the operation completes then it completes with ExecutionException and cause AbortedByTimeoutException. In that case it is guranteed that no bytes have been read from the channel into the given buffers.

Specified by:
read in class AsynchronousSocketChannel
Type Parameters:
A - the attachment type
Parameters:
dsts - the buffers into which bytes are to be transferred
offset - the offset within the buffer array of the first buffer into which bytes are to be transferred; must be non-negative and no larger than dsts.length
length - the maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset
timeout - the timeout, or 0L for no timeout
unit - the time unit of the timeout argument
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result

write

public <A> IoFuture<Integer,A> write(ByteBuffer src,
                                     long timeout,
                                     TimeUnit unit,
                                     A attachment,
                                     CompletionHandler<Integer,? super A> handler)
Writes a sequence of bytes to this channel from the given buffer.

This method initiates the writing of a sequence of bytes to this channel from the given buffer, returning an IoFuture representing the pending result of the operation. The IoFuture's get method will return the number of bytes written, possibly zero.

If a timeout is specified and the timeout elapses before the operation completes then it completes with ExecutionException and cause AbortedByTimeoutException. In that case it is guranteed that no bytes have written to the channel from the given buffer.

Otherwise this method works in the same manner as the AsynchronousByteChannel.write(ByteBuffer,A,CompletionHandler) method.

Specified by:
write in class AsynchronousSocketChannel
Type Parameters:
A - the attachment type
Parameters:
src - the buffer from which bytes are to be retrieved
timeout - the timeout, or 0L for no timeout
unit - the time unit of the timeout argument
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result

write

public <A> IoFuture<Long,A> write(ByteBuffer[] srcs,
                                  int offset,
                                  int length,
                                  long timeout,
                                  TimeUnit unit,
                                  A attachment,
                                  CompletionHandler<Long,? super A> handler)
Writes a sequence of bytes to this channel from a subsequence of the given buffers.

This method initiates the writing of a sequence of bytes to this channel from a subsequence of the given buffers, returning an IoFuture representing the pending result of the operation. The IoFuture's get method will return the number of bytes written, possibly zero.

This method initiates a write of up to r bytes to this channel, where r is the total number of bytes remaining in the specified subsequence of the given buffer array, that is,

      srcs[offset].remaining()
          + srcs[offset+1].remaining()
          + ... + srcs[offset+length-1].remaining()
 
at the moment that the write is attempted.

Suppose that a byte sequence of length n is written, where 0 <= n <= r. Up to the first srcs[offset].remaining() bytes of this sequence are written from buffer srcs[offset], up to the next srcs[offset+1].remaining() bytes are written from buffer srcs[offset+1], and so forth, until the entire byte sequence is written. As many bytes as possible are written from each buffer, hence the final position of each updated buffer, except the last updated buffer, is guaranteed to be equal to that buffer's limit.

If a timeout is specified and the timeout elapses before the operation completes then it completes with ExecutionException and cause AbortedByTimeoutException. In that case it is guranteed that no bytes have written to the channel from the given buffers.

Specified by:
write in class AsynchronousSocketChannel
Type Parameters:
A - the attachment type
Parameters:
srcs - the buffers from which bytes are to be retrieved
offset - the offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.length.
length - the maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
timeout - the timeout, or 0L for no timeout
unit - the time unit of the timeout argument
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result

RedDwarf, Version 0.10.1
2010-03-14 10:56:12

Copyright © 2010 The RedDwarf Authors. All rights reserved
Copyright © 2007-2010 Sun Microsystems, Inc. All rights reserved