Top Description Inners Fields Constructors Methods
jnr.unixsocket

public Class UnixSocketChannel

extends AbstractNativeSocketChannel
Class Inheritance
Imports
java.io.IOException, java.net.SocketAddress, .SocketOption, java.nio.ByteBuffer, java.nio.channels.ClosedChannelException, .SocketChannel, .UnsupportedAddressTypeException, java.util.Collections, .HashSet, .Set, java.util.concurrent.locks.ReadWriteLock, .ReentrantReadWriteLock, jnr.constants.platform.Errno, .ProtocolFamily, .Sock, jnr.unixsocket.impl.AbstractNativeSocketChannel, jnr.ffi.LastError

A java.nio.channels.Channel implementation that uses a native unix socket

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
pack-priv static enum

Field Summary

Modifier and TypeField and Description
private final BindHandler
private UnixSocketAddress
private UnixSocketAddress
private UnixSocketChannel.State
private final ReadWriteLock

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv
pack-priv
UnixSocketChannel(int fd, UnixSocketChannel.State initialState, boolean initialBoundState)

Method Summary

Modifier and TypeMethod and Description
public synchronized UnixSocketChannel
bind(SocketAddress
The address to bind the socket, or null to bind the socket to an automatically assigned socket address
local
)

Implements abstract java.nio.channels.SocketChannel.bind.

Implements java.nio.channels.NetworkChannel.bind.

Binds the channel's socket to a local address.
public boolean
public boolean
connect(SocketAddress
The remote address to which this channel is to be connected
remote
)

Implements abstract java.nio.channels.SocketChannel.connect.

Connects this channel's socket.
public static final UnixSocketChannel
private boolean
public boolean
finishConnect()

Implements abstract java.nio.channels.SocketChannel.finishConnect.

Finishes the process of connecting a socket channel.
public static final UnixSocketChannel

Returns:

the new UnixSocketChannel instance
fromFD
(int
the file descriptor to wrap
fd
)

Create a UnixSocketChannel to wrap an existing file descriptor (presumably itself a UNIX socket).

public SocketAddress
getLocalAddress()

Implements abstract java.nio.channels.SocketChannel.getLocalAddress.

Implements java.nio.channels.NetworkChannel.getLocalAddress.

Returns the socket address that this channel's socket is bound to.
public final UnixSocketAddress
public <T> T
getOption(SocketOption<T>
The socket option
name
)

Implements java.nio.channels.NetworkChannel.getOption.

Returns the value of a socket option.
public SocketAddress
getRemoteAddress()

Implements abstract java.nio.channels.SocketChannel.getRemoteAddress.

Returns the remote address to which this channel's socket is connected.
public final UnixSocketAddress
pack-priv boolean
public boolean
isConnected()

Implements abstract java.nio.channels.SocketChannel.isConnected.

Tells whether or not this channel's network socket is connected.
public boolean
isConnectionPending()

Implements abstract java.nio.channels.SocketChannel.isConnectionPending.

Tells whether or not a connection operation is in progress on this channel.
private boolean
public static final UnixSocketChannel
open()

Hides java.nio.channels.SocketChannel.open.

Opens a socket channel for an Internet protocol socket.
public static final UnixSocketChannel
public static final UnixSocketChannel[]
pair()

public int
read(ByteBuffer
The buffer into which bytes are to be transferred
dst
)

Overrides jnr.unixsocket.impl.AbstractNativeSocketChannel.read.

Implements java.nio.channels.ReadableByteChannel.read.

Reads a sequence of bytes from this channel into the given buffer.
public <T> SocketChannel
setOption(SocketOption<T>
The socket option
name
,
T
The value of the socket option. A value of null may be a valid value for some socket options.
value
)

Implements abstract java.nio.channels.SocketChannel.setOption.

Implements java.nio.channels.NetworkChannel.setOption.

Sets the value of a socket option.
public UnixSocket
socket()

Implements abstract java.nio.channels.SocketChannel.socket.

Retrieves a socket associated with this channel.
public final Set<SocketOption<?>>
supportedOptions()

Implements java.nio.channels.NetworkChannel.supportedOptions.

Returns a set of the socket options supported by this channel.
public long
write(ByteBuffer[]
The buffers from which bytes are to be retrieved
srcs
,
int
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
offset
,
int
The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
length
)

Overrides jnr.unixsocket.impl.AbstractNativeSocketChannel.write.

Implements java.nio.channels.GatheringByteChannel.write.

Writes a sequence of bytes to this channel from a subsequence of the given buffers.
public int
write(ByteBuffer
The buffer from which bytes are to be retrieved
src
)

Overrides jnr.unixsocket.impl.AbstractNativeSocketChannel.write.

Implements java.nio.channels.WritableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.
Inherited from jnr.unixsocket.impl.AbstractNativeSocketChannel:
getFDimplCloseSelectableChannelimplConfigureBlockingreadsetFDshutdownInputshutdownOutput

Field Detail

bindHandlerback to summary
private final BindHandler bindHandler
localAddressback to summary
private UnixSocketAddress localAddress
remoteAddressback to summary
private UnixSocketAddress remoteAddress
stateback to summary
private UnixSocketChannel.State state
stateLockback to summary
private final ReadWriteLock stateLock

Constructor Detail

UnixSocketChannelback to summary
pack-priv UnixSocketChannel() throws IOException
UnixSocketChannelback to summary
pack-priv UnixSocketChannel(int fd)
UnixSocketChannelback to summary
pack-priv UnixSocketChannel(int fd, UnixSocketChannel.State initialState, boolean initialBoundState)

Method Detail

bindback to summary
public synchronized UnixSocketChannel bind(SocketAddress local) throws IOException

Implements abstract java.nio.channels.SocketChannel.bind.

Implements java.nio.channels.NetworkChannel.bind.

Doc from java.nio.channels.SocketChannel.bind.

Binds the channel's socket to a local address.

This method is used to establish an association between the socket and a local address. For Internet Protocol sockets, once an association is established then the socket remains bound until the channel is closed. If the local parameter has the value null then the socket will be bound to an address that is assigned automatically.

Parameters
local:SocketAddress

The address to bind the socket, or null to bind the socket to an automatically assigned socket address

Returns:UnixSocketChannel

This channel

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
connectback to summary
public boolean connect(UnixSocketAddress remote) throws IOException
connectback to summary
public boolean connect(SocketAddress remote) throws IOException

Implements abstract java.nio.channels.SocketChannel.connect.

Doc from java.nio.channels.SocketChannel.connect.

Connects this channel's socket.

If this channel is in non-blocking mode then an invocation of this method initiates a non-blocking connection operation. If the connection is established immediately, as can happen with a local connection, then this method returns true. Otherwise this method returns false and the connection operation must later be completed by invoking the finishConnect method.

If this channel is in blocking mode then an invocation of this method will block until the connection is established or an I/O error occurs.

For channels to Internet protocol sockets, this method performs exactly the same security checks as the java.net.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.

For channels to Unix Domain sockets, this method checks NetPermission("accessUnixDomainSocket") with the security manager's checkPermission method.

This method may be invoked at any time. If a read or write operation upon this channel is invoked while an invocation of this method is in progress then that operation will first block until this invocation is complete. If a connection attempt is initiated but fails, that is, if an invocation of this method throws a checked exception, then the channel will be closed.

Parameters
remote:SocketAddress

The remote address to which this channel is to be connected

Returns:boolean

true if a connection was established, false if this channel is in non-blocking mode and the connection operation is in progress

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
createback to summary
public static final UnixSocketChannel create() throws IOException
doConnectback to summary
private boolean doConnect(SockAddrUnix remote) throws IOException
finishConnectback to summary
public boolean finishConnect() throws IOException

Implements abstract java.nio.channels.SocketChannel.finishConnect.

Doc from java.nio.channels.SocketChannel.finishConnect.

Finishes the process of connecting a socket channel.

A non-blocking connection operation is initiated by placing a socket channel in non-blocking mode and then invoking its connect method. Once the connection is established, or the attempt has failed, the socket channel will become connectable and this method may be invoked to complete the connection sequence. If the connection operation failed then invoking this method will cause an appropriate java.io.IOException to be thrown.

If this channel is already connected then this method will not block and will immediately return true. If this channel is in non-blocking mode then this method will return false if the connection process is not yet complete. If this channel is in blocking mode then this method will block until the connection either completes or fails, and will always either return true or throw a checked exception describing the failure.

This method may be invoked at any time. If a read or write operation upon this channel is invoked while an invocation of this method is in progress then that operation will first block until this invocation is complete. If a connection attempt fails, that is, if an invocation of this method throws a checked exception, then the channel will be closed.

Returns:boolean

true if, and only if, this channel's socket is now connected

Exceptions
IOException:
If some other I/O error occurs
fromFDback to summary
public static final UnixSocketChannel fromFD(int fd)

Create a UnixSocketChannel to wrap an existing file descriptor (presumably itself a UNIX socket).

Parameters
fd:int

the file descriptor to wrap

Returns:UnixSocketChannel

the new UnixSocketChannel instance

getLocalAddressback to summary
public SocketAddress getLocalAddress() throws IOException

Implements abstract java.nio.channels.SocketChannel.getLocalAddress.

Implements java.nio.channels.NetworkChannel.getLocalAddress.

Doc from java.nio.channels.SocketChannel.getLocalAddress.

Returns the socket address that this channel's socket is bound to.

Where the channel is bound to an Internet Protocol socket address then the return value from this method is of type java.net.InetSocketAddress. If there is a security manager set, its checkConnect method is called with the local address and -1 as its arguments to see if the operation is allowed. If the operation is not allowed, a SocketAddress representing the loopback address and the local port of the channel's socket is returned.

Where the channel is bound to a Unix Domain socket address, the socket address is a UnixDomainSocketAddress. If there is a security manager set, its checkPermission method is called with NetPermission("accessUnixDomainSocket"). If the operation is not allowed an unnamed UnixDomainSocketAddress is returned.

Returns:SocketAddress

The SocketAddress that the socket is bound to, or the SocketAddress representing the loopback address or empty path if denied by the security manager, or null if the channel's socket is not bound

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
getLocalSocketAddressback to summary
public final UnixSocketAddress getLocalSocketAddress()
getOptionback to summary
public <T> T getOption(SocketOption<T> name) throws IOException

Implements java.nio.channels.NetworkChannel.getOption.

Doc from java.nio.channels.NetworkChannel.getOption.

Returns the value of a socket option.

Parameters
<T>
The type of the socket option value
name:SocketOption<T>

The socket option

Returns:T

The value of the socket option. A value of null may be a valid value for some socket options.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
getRemoteAddressback to summary
public SocketAddress getRemoteAddress() throws IOException

Implements abstract java.nio.channels.SocketChannel.getRemoteAddress.

Doc from java.nio.channels.SocketChannel.getRemoteAddress.

Returns the remote address to which this channel's socket is connected.

Where the channel's socket is bound and connected to an Internet Protocol socket address then the return value is of type java.net.InetSocketAddress.

Where the channel's socket is bound and connected to a Unix Domain socket address, the returned address is a UnixDomainSocketAddress.

Returns:SocketAddress

The remote address; null if the channel's socket is not connected

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
getRemoteSocketAddressback to summary
public final UnixSocketAddress getRemoteSocketAddress()
isBoundback to summary
pack-priv boolean isBound()
isConnectedback to summary
public boolean isConnected()

Implements abstract java.nio.channels.SocketChannel.isConnected.

Doc from java.nio.channels.SocketChannel.isConnected.

Tells whether or not this channel's network socket is connected.

Returns:boolean

true if, and only if, this channel's network socket is open and connected

isConnectionPendingback to summary
public boolean isConnectionPending()

Implements abstract java.nio.channels.SocketChannel.isConnectionPending.

Doc from java.nio.channels.SocketChannel.isConnectionPending.

Tells whether or not a connection operation is in progress on this channel.

Returns:boolean

true if, and only if, a connection operation has been initiated on this channel but not yet completed by invoking the finishConnect method

isIdleback to summary
private boolean isIdle()
openback to summary
public static final UnixSocketChannel open() throws IOException

Hides java.nio.channels.SocketChannel.open.

Doc from java.nio.channels.SocketChannel.open.

Opens a socket channel for an Internet protocol socket.

The new channel is created by invoking the openSocketChannel method of the system-wide default java.nio.channels.spi.SelectorProvider object.

Returns:UnixSocketChannel

A new socket channel

Exceptions
IOException:
If an I/O error occurs
openback to summary
public static final UnixSocketChannel open(UnixSocketAddress remote) throws IOException
pairback to summary
public static final UnixSocketChannel[] pair() throws IOException
readback to summary
public int read(ByteBuffer dst) throws IOException

Overrides jnr.unixsocket.impl.AbstractNativeSocketChannel.read.

Implements java.nio.channels.ReadableByteChannel.read.

Doc from java.nio.channels.ReadableByteChannel.read.

Reads a sequence of bytes from this channel into the given buffer.

An attempt is made to read up to r bytes from the channel, where r is the number of bytes remaining in the buffer, that is, dst.remaining(), at the moment this method is invoked.

Suppose that a byte sequence of length n is read, where 0 <= n <= r. This byte sequence will be transferred into the buffer so that the first byte in the sequence is at index p and the last byte is at index p + n - 1, where p is the buffer's position at the moment this method is invoked. Upon return the buffer's position will be equal to p + n; its limit will not have changed.

A read operation might not fill the buffer, and in fact it might not read any bytes at all. Whether or not it does so depends upon the nature and state of the channel. A socket channel in non-blocking mode, for example, cannot read any more bytes than are immediately available from the socket's input buffer; similarly, a file channel cannot read any more bytes than remain in the file. It is guaranteed, however, that if a channel is in blocking mode and there is at least one byte remaining in the buffer then this method will block until at least one byte is read.

This method may be invoked at any time. If another thread has already initiated a read operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Parameters
dst:ByteBuffer

The buffer into which bytes are to be transferred

Returns:int

The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
setOptionback to summary
public <T> SocketChannel setOption(SocketOption<T> name, T value) throws IOException

Implements abstract java.nio.channels.SocketChannel.setOption.

Implements java.nio.channels.NetworkChannel.setOption.

Doc from java.nio.channels.NetworkChannel.setOption.

Sets the value of a socket option.

Parameters
<T>
The type of the socket option value
name:SocketOption<T>

The socket option

value:T

The value of the socket option. A value of null may be a valid value for some socket options.

Returns:SocketChannel

This channel

Annotations
@Override
Exceptions
IOException:

Doc from java.nio.channels.SocketChannel.setOption.

If an I/O error occurs

socketback to summary
public UnixSocket socket()

Implements abstract java.nio.channels.SocketChannel.socket.

Doc from java.nio.channels.SocketChannel.socket.

Retrieves a socket associated with this channel.

Returns:UnixSocket

A socket associated with this channel

Annotations
@Override
supportedOptionsback to summary
public final Set<SocketOption<?>> supportedOptions()

Implements java.nio.channels.NetworkChannel.supportedOptions.

Doc from java.nio.channels.NetworkChannel.supportedOptions.

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:Set<SocketOption<?>>

A set of the socket options supported by this channel

Annotations
@Override
writeback to summary
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException

Overrides jnr.unixsocket.impl.AbstractNativeSocketChannel.write.

Implements java.nio.channels.GatheringByteChannel.write.

Doc from java.nio.channels.GatheringByteChannel.write.

Writes a sequence of bytes to this channel from a subsequence of the given buffers.

An attempt is made to write 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()
srcs[offset].remaining()
    + srcs[offset+1].remaining()
    + ... + srcs[offset+length-1].remaining()
at the moment that this method is invoked.

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.

Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer.

This method may be invoked at any time. If another thread has already initiated a write operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Parameters
srcs:ByteBuffer[]

The buffers from which bytes are to be retrieved

offset:int

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:int

The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset

Returns:long

The number of bytes written, possibly zero

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
writeback to summary
public int write(ByteBuffer src) throws IOException

Overrides jnr.unixsocket.impl.AbstractNativeSocketChannel.write.

Implements java.nio.channels.WritableByteChannel.write.

Doc from java.nio.channels.WritableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.

An attempt is made to write up to r bytes to the channel, where r is the number of bytes remaining in the buffer, that is, src.remaining(), at the moment this method is invoked.

Suppose that a byte sequence of length n is written, where 0 <= n <= r. This byte sequence will be transferred from the buffer starting at index p, where p is the buffer's position at the moment this method is invoked; the index of the last byte written will be p + n - 1. Upon return the buffer's position will be equal to p + n; its limit will not have changed.

Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer.

This method may be invoked at any time. If another thread has already initiated a write operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Parameters
src:ByteBuffer

The buffer from which bytes are to be retrieved

Returns:int

The number of bytes written, possibly zero

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
jnr.unixsocket back to summary

private Class UnixSocketChannel.DefaultOptionsHolder

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv static final Set<SocketOption<?>>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static Set<SocketOption<?>>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

defaultOptionsback to summary
pack-priv static final Set<SocketOption<?>> defaultOptions

Constructor Detail

DefaultOptionsHolderback to summary
private DefaultOptionsHolder()

Method Detail

defaultOptionsback to summary
private static Set<SocketOption<?>> defaultOptions()
jnr.unixsocket back to summary

pack-priv final Enum UnixSocketChannel.State

extends Enum<UnixSocketChannel.State>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final UnixSocketChannel.State
public static final UnixSocketChannel.State
public static final UnixSocketChannel.State
public static final UnixSocketChannel.State

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static UnixSocketChannel.State
public static UnixSocketChannel.State[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

CONNECTEDback to summary
public static final UnixSocketChannel.State CONNECTED
CONNECTINGback to summary
public static final UnixSocketChannel.State CONNECTING
IDLEback to summary
public static final UnixSocketChannel.State IDLE
UNINITIALIZEDback to summary
public static final UnixSocketChannel.State UNINITIALIZED

Constructor Detail

Stateback to summary
private State()

Method Detail

valueOfback to summary
public static UnixSocketChannel.State valueOf(String name)
valuesback to summary
public static UnixSocketChannel.State[] values()