- java.lang.Object
-
- java.net.Socket
-
- org.newsclub.net.unix.AFUNIXSocket
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public final class AFUNIXSocket extends java.net.SocketImplementation of an AF_UNIX domain socket.- Author:
- Christian Kohlschütter
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCloseable(java.io.Closeable closeable)Registers aCloseablethat should be closed when this socket is closed.voidbind(java.net.SocketAddress bindpoint)Binds thisAFUNIXSocketto the given bindpoint.voidclearReceivedFileDescriptors()Clears the queue of incomingFileDescriptors that were sent as ancillary messages.voidclose()voidconnect(java.net.SocketAddress endpoint)voidconnect(java.net.SocketAddress endpoint, int timeout)static AFUNIXSocketconnectTo(AFUNIXSocketAddress addr)Creates a newAFUNIXSocketand connects it to the givenAFUNIXSocketAddress.voidensureAncillaryReceiveBufferSize(int minSize)Ensures a minimum ancillary receive buffer size.intgetAncillaryReceiveBufferSize()Returns the size of the receive buffer for ancillary messages (in bytes).static java.lang.StringgetLoadedLibrary()Returns an identifier of the loaded native library, ornullif the library hasn't been loaded yet.AFUNIXSocketCredentialsgetPeerCredentials()Retrieves the "peer credentials" for this connection.java.io.FileDescriptor[]getReceivedFileDescriptors()Retrieves an array of incomingFileDescriptors that were sent as ancillary messages, along with a call toInputStream.read(), etc.static java.lang.StringgetVersion()Returns the version of the junixsocket library, as a string, for debugging purposes.booleanisClosed()static booleanisSupported()ReturnstrueiffAFUNIXSockets are supported by the current Java VM.static voidmain(java.lang.String[] args)Very basic self-test function.static AFUNIXSocketnewInstance()Creates a new, unboundAFUNIXSocket.static AFUNIXSocketnewStrictInstance()Creates a new, unbound, "strict"AFUNIXSocket.voidremoveCloseable(java.io.Closeable closeable)Unregisters a previously registeredCloseable.voidsetAncillaryReceiveBufferSize(int size)Sets the size of the receive buffer for ancillary messages (in bytes).voidsetOutboundFileDescriptors(java.io.FileDescriptor... fdescs)Sets a list ofFileDescriptors that should be sent as an ancillary message along with the next write.static booleansupports(AFUNIXSocketCapability capability)Checks if the current environment (system platform, native library, etc.) supports a given junixsocket capability.java.lang.StringtoString()-
Methods inherited from class java.net.Socket
getChannel, getInetAddress, getInputStream, getKeepAlive, getLocalAddress, getLocalPort, getLocalSocketAddress, getOOBInline, getOption, getOutputStream, getPort, getReceiveBufferSize, getRemoteSocketAddress, getReuseAddress, getSendBufferSize, getSoLinger, getSoTimeout, getTcpNoDelay, getTrafficClass, isBound, isConnected, isInputShutdown, isOutputShutdown, sendUrgentData, setKeepAlive, setOOBInline, setOption, setPerformancePreferences, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setSocketImplFactory, setSoLinger, setSoTimeout, setTcpNoDelay, setTrafficClass, shutdownInput, shutdownOutput, supportedOptions
-
-
-
-
Method Detail
-
newInstance
public static AFUNIXSocket newInstance() throws java.io.IOException
Creates a new, unboundAFUNIXSocket. 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:
java.io.IOException- if the operation fails.
-
newStrictInstance
public static AFUNIXSocket newStrictInstance() throws java.io.IOException
Creates a new, unbound, "strict"AFUNIXSocket. 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:
java.io.IOException- if the operation fails.
-
connectTo
public static AFUNIXSocket connectTo(AFUNIXSocketAddress addr) throws java.io.IOException
Creates a newAFUNIXSocketand connects it to the givenAFUNIXSocketAddress.- Parameters:
addr- The address to connect to.- Returns:
- A new, connected socket.
- Throws:
java.io.IOException- if the operation fails.
-
bind
public void bind(java.net.SocketAddress bindpoint) throws java.io.IOExceptionBinds thisAFUNIXSocketto the given bindpoint. Only bindpoints of the typeAFUNIXSocketAddressare supported.- Overrides:
bindin classjava.net.Socket- Throws:
java.io.IOException
-
connect
public void connect(java.net.SocketAddress endpoint) throws java.io.IOException- Overrides:
connectin classjava.net.Socket- Throws:
java.io.IOException
-
connect
public void connect(java.net.SocketAddress endpoint, int timeout) throws java.io.IOException- Overrides:
connectin classjava.net.Socket- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.net.Socket
-
isSupported
public static boolean isSupported()
ReturnstrueiffAFUNIXSockets are supported by the current Java VM. To supportAFUNIXSockets, a custom JNI library must be loaded that is supplied with junixsocket.- Returns:
trueiff supported.
-
getVersion
public static java.lang.String getVersion()
Returns the version of the junixsocket library, as a string, for debugging purposes. NOTE: Do not rely on the format of the version identifier, use socket capabilities instead.- Returns:
- String The version identfier, or
nullif it could not be determined. - See Also:
supports(AFUNIXSocketCapability)
-
getLoadedLibrary
public static java.lang.String getLoadedLibrary()
Returns an identifier of the loaded native library, ornullif the library hasn't been loaded yet. The identifier is useful mainly for debugging purposes.- Returns:
- The identifier of the loaded junixsocket-native library, or
null.
-
getPeerCredentials
public AFUNIXSocketCredentials getPeerCredentials() throws java.io.IOException
Retrieves the "peer credentials" for this connection. These credentials may be useful to authenticate the other end of the socket (client or server).- Returns:
- The peer's credentials.
- Throws:
java.io.IOException- If there was an error returning these credentials.
-
isClosed
public boolean isClosed()
- Overrides:
isClosedin classjava.net.Socket
-
getAncillaryReceiveBufferSize
public int getAncillaryReceiveBufferSize()
Returns the size of the receive buffer for ancillary messages (in bytes).- Returns:
- The size.
-
setAncillaryReceiveBufferSize
public void setAncillaryReceiveBufferSize(int size)
Sets the size of the receive buffer for ancillary messages (in bytes). To disable handling ancillary messages, set it to 0 (default).- Parameters:
size- The size.
-
ensureAncillaryReceiveBufferSize
public void ensureAncillaryReceiveBufferSize(int minSize)
Ensures a minimum ancillary receive buffer size.- Parameters:
minSize- The minimum size (in bytes).
-
getReceivedFileDescriptors
public java.io.FileDescriptor[] getReceivedFileDescriptors() throws java.io.IOExceptionRetrieves 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,nullwill be returned).- Returns:
- The file descriptors, or
nullif none were available. - Throws:
java.io.IOException- if the operation fails.
-
clearReceivedFileDescriptors
public void clearReceivedFileDescriptors()
Clears the queue of incomingFileDescriptors that were sent as ancillary messages.
-
setOutboundFileDescriptors
public void setOutboundFileDescriptors(java.io.FileDescriptor... fdescs) throws java.io.IOExceptionSets a list ofFileDescriptors that should be sent as an ancillary message along with the next write. NOTE: There can only be one set of file descriptors active until the write completes.- Parameters:
fdescs- The file descriptors, ornullif none.- Throws:
java.io.IOException- if the operation fails.
-
supports
public static boolean supports(AFUNIXSocketCapability capability)
Checks if the current environment (system platform, native library, etc.) supports a given junixsocket capability.- Parameters:
capability- The capability.- Returns:
- true if supported.
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.net.Socket- Throws:
java.io.IOException
-
addCloseable
public void addCloseable(java.io.Closeable closeable)
Registers aCloseablethat should be closed when this socket is closed.- Parameters:
closeable- The closeable.
-
removeCloseable
public void removeCloseable(java.io.Closeable closeable)
Unregisters a previously registeredCloseable.- Parameters:
closeable- The closeable.
-
main
public static void main(java.lang.String[] args)
Very basic self-test function. Prints "supported" and "capabilities" status to System.out.- Parameters:
args- ignored.
-
-