Class AFUNIXSocket
- All Implemented Interfaces:
Closeable, AutoCloseable, AFSocketExtensions, AFSomeSocket, AFSomeSocketThing, AFUNIXSocketExtensions, FileDescriptorAccess
public final class AFUNIXSocket
extends AFSocket<AFUNIXSocketAddress>
implements AFUNIXSocketExtensions
Implementation of an AF_UNIX domain socket.
- Author:
- Christian Kohlschütter
-
Nested Class Summary
Nested classes/interfaces inherited from class AFSocket
AFSocket.Constructor<A>Modifier and TypeClassDescriptionstatic interfaceAFSocket.Constructor<A extends AFSocketAddress>The reference to the constructor of anAFSocketsubclass. -
Method Summary
Modifier and TypeMethodDescriptionvoidClears the queue of incomingFileDescriptors that were sent as ancillary messages.static AFUNIXSocketconnectTo(AFUNIXSocketAddress addr) Creates a newAFSocketand connects it to the givenAFUNIXSocketAddress.Retrieves the "peer credentials" for this connection.Retrieves an array of incomingFileDescriptors that were sent as ancillary messages, along with a call toInputStream.read(), etc.booleanReturnstrueif there are pending file descriptors to be sent as part of an ancillary message.static booleanReturnstrueiffAFUNIXSockets are supported by the current Java VM.static voidVery basic self-test function.protected AFUNIXSocketChannelCreates a newAFSocketChannelfor this socket.static AFUNIXSocketCreates a new, unboundAFSocket.static AFUNIXSocketCreates a new, unbound, "strict"AFSocket.voidsetOutboundFileDescriptors(FileDescriptor... fdescs) Sets a list ofFileDescriptors that should be sent as an ancillary message along with the next write.Methods inherited from class AFSocket
addCloseable, bind, checkConnectionClosed, close, connect, connect, connectHook, connectTo, connectTo, ensureAncillaryReceiveBufferSize, ensureSupported, ensureUnsafeSupported, forceConnectAddress, getAncillaryReceiveBufferSize, getFileDescriptor, getImplExtensions, getInputStream, getLoadedLibrary, getLocalSocketAddress, getOutputStream, getRemoteSocketAddress, getVersion, isBound, isClosed, isConnected, isRunningOnAndroid, newInstance, removeCloseable, setAncillaryReceiveBufferSize, setShutdownOnClose, socketAddressClass, supports, supports, toStringModifier and TypeMethodDescriptionfinal voidaddCloseable(Closeable closeable) Registers aCloseablethat should be closed when this socket is closed.final voidbind(SocketAddress bindpoint) Not fully supported, since it's not necessary for client sockets.booleanProbes the status of the socket connection.final voidclose()final voidconnect(SocketAddress endpoint) final voidconnect(SocketAddress endpoint, int timeout) final AFSocket<AFUNIXSocketAddress> Sets the hook for any subsequent call toAFSocket.connect(SocketAddress)orAFSocket.connect(SocketAddress, int)to be the given function.static final <A extends AFSocketAddress>
AFSocket<?> connectTo(@NonNull A addr) Creates a newAFSocketand connects it to the givenAFSocketAddressusing the default implementation suited for that address type.protected static final <A extends AFSocketAddress>
@NonNull AFSocket<A> connectTo(AFSocket.Constructor<A> constr, A addr) Creates a newAFSocketand connects it to the givenAFSocketAddress.final voidensureAncillaryReceiveBufferSize(int minSize) Ensures a minimum ancillary receive buffer size.static voidChecks ifAFSockets are supported by the current Java VM.static final voidChecks if the current environment (system platform, native library, etc.) supports "unsafe" operations (as controlled via theAFSocketCapability.CAPABILITY_UNSAFEcapability).final AFSocket<AFUNIXSocketAddress> forceConnectAddress(SocketAddress endpoint) Forces the address to be used for any subsequent call toAFSocket.connect(SocketAddress)to be the given one, regardless of what'll be passed there.final intReturns the size of the receive buffer for ancillary messages (in bytes).final FileDescriptorReturns the correspondingFileDescriptor.protected final AFSocketImplExtensions<AFUNIXSocketAddress> Returns the internal helper instance for address-specific extensions.final AFInputStreamstatic final StringReturns an identifier of the loaded native library, ornullif the library hasn't been loaded yet.final AFUNIXSocketAddressReturns the socket's local socket address, ornullif unavailable or if there was a problem retrieving it.final AFOutputStreamfinal AFUNIXSocketAddressReturns the socket's remote socket address, ornullif unavailable/not connected, or if there was a problem retrieving it.static final StringReturns the version of the junixsocket library, as a string, for debugging purposes.final booleanisBound()final booleanisClosed()final booleanstatic booleanChecks if we're running on Android (as far as junixsocket is concerned).protected static final <A extends AFSocketAddress>
AFSocket<A> newInstance(AFSocket.Constructor<A> constr, AFSocketFactory<A> factory) Creates a new, unboundAFSocket.final voidremoveCloseable(Closeable closeable) Unregisters a previously registeredCloseable.final voidsetAncillaryReceiveBufferSize(int size) Sets the size of the receive buffer for ancillary messages (in bytes).voidsetShutdownOnClose(boolean enabled) Configures whether the socket should be shutdown uponCloseable.close(), which is the default.protected final Class<? extends AFSocketAddress> Returns theAFSocketAddresstype supported by this socket.static final booleansupports(AFSocketCapability capability) Checks if the current environment (system platform, native library, etc.) supports a given junixsocket capability.static final booleansupports(AFUNIXSocketCapability capability) Deprecated.final StringtoString()Methods inherited from class Socket
getInetAddress, getKeepAlive, getLocalAddress, getLocalPort, getOOBInline, getOption, getPort, getReceiveBufferSize, getReuseAddress, getSendBufferSize, getSoLinger, getSoTimeout, getTcpNoDelay, getTrafficClass, isInputShutdown, isOutputShutdown, sendUrgentData, setKeepAlive, setOOBInline, setOption, setPerformancePreferences, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setSocketImplFactory, setSoLinger, setSoTimeout, setTcpNoDelay, setTrafficClass, shutdownInput, shutdownOutput, supportedOptions
-
Method Details
-
newChannel
Description copied from class:AFSocketCreates a newAFSocketChannelfor this socket.- Specified by:
newChannelin classAFSocket<AFUNIXSocketAddress>- Returns:
- The new instance.
-
newInstance
Creates a new, unboundAFSocket. This "default" implementation is a bit "lenient" with respect to the specification. In particular, we ignore calls toSocket.getTcpNoDelay()andSocket.setTcpNoDelay(boolean).- Returns:
- A new, unbound socket.
- Throws:
IOException- if the operation fails.
-
newStrictInstance
Creates a new, unbound, "strict"AFSocket. This call uses an implementation that tries to be closer to the specification thannewInstance(), at least for some cases.- Returns:
- A new, unbound socket.
- Throws:
IOException- if the operation fails.
-
connectTo
Creates a newAFSocketand connects it to the givenAFUNIXSocketAddress.- Parameters:
addr- The address to connect to.- Returns:
- A new, connected socket.
- Throws:
IOException- if the operation fails.
-
getChannel
- Overrides:
getChannelin classAFSocket<AFUNIXSocketAddress>
-
getPeerCredentials
Description copied from interface:AFUNIXSocketExtensionsRetrieves the "peer credentials" for this connection. These credentials may be useful to authenticate the other end of the socket (client or server). Depending on the socket/connection/environment, you may not receive any or all credentials. For example, on Linux,AFUNIXDatagramSocketandAFUNIXDatagramChannelmay not be able to retrieve credentials at all.- Specified by:
getPeerCredentialsin interfaceAFUNIXSocketExtensions- Returns:
- The peer's credentials, or
nullif they couldn't be retrieved. - Throws:
IOException- If there was an error returning these credentials.
-
getReceivedFileDescriptors
Description copied from interface:AFUNIXSocketExtensionsRetrieves an array of incomingFileDescriptors that were sent as ancillary messages, along with a call toInputStream.read(), etc. NOTE: Another call to this method will not return the same file descriptors again (most likely, an empty array will be returned).- Specified by:
getReceivedFileDescriptorsin interfaceAFUNIXSocketExtensions- Returns:
- The file descriptors, or an empty array if none were available.
- Throws:
IOException- if the operation fails.
-
clearReceivedFileDescriptors
public void clearReceivedFileDescriptors()Description copied from interface:AFUNIXSocketExtensionsClears the queue of incomingFileDescriptors that were sent as ancillary messages.- Specified by:
clearReceivedFileDescriptorsin interfaceAFUNIXSocketExtensions
-
setOutboundFileDescriptors
Description copied from interface:AFUNIXSocketExtensionsSets a list ofFileDescriptors that should be sent as an ancillary message along with the next write. Important: There can only be one set of file descriptors active until the write completes. The socket also needs to be connected for this operation to succeed. It is also important to know that there may be an upper limit imposed by the operation system as to how many file descriptors can be sent at once. Linux, for example, may support up to 253. If the number of file descriptors exceeds the limit, an exception may be thrown when sending data along with the ancillary message containing the file descriptors.- Specified by:
setOutboundFileDescriptorsin interfaceAFUNIXSocketExtensions- Parameters:
fdescs- The file descriptors, ornullif none.- Throws:
IOException- if the operation fails.
-
hasOutboundFileDescriptors
public boolean hasOutboundFileDescriptors()Description copied from interface:AFUNIXSocketExtensionsReturnstrueif there are pending file descriptors to be sent as part of an ancillary message.- Specified by:
hasOutboundFileDescriptorsin interfaceAFUNIXSocketExtensions- Returns:
trueif there are file descriptors pending.
-
isSupported
public static boolean isSupported()ReturnstrueiffAFUNIXSockets are supported by the current Java VM. To supportAFSockets, a custom JNI library must be loaded that is supplied with junixsocket, and the system must support AF_UNIX sockets. This call is equivalent to checkingAFSocket.isSupported()andAFSocket.supports(AFSocketCapability)withAFSocketCapability.CAPABILITY_UNIX_DOMAIN.- Returns:
trueiff supported.
-
main
Very basic self-test function. Prints "supported" and "capabilities" status to System.out.- Parameters:
args- ignored.
-