java.lang.Object
java.net.SocketAddress
java.net.InetSocketAddress
org.newsclub.net.unix.AFSocketAddress
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
AFTIPCSocketAddress,AFUNIXSocketAddress
Some
SocketAddress that is supported by junixsocket, such as AFUNIXSocketAddress.- Author:
- Christian Kohlschütter
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interfaceA reference to the constructor of an AFSocketAddress subclass. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAFSocketAddress(int port, byte[] socketAddress, ByteBuffer nativeAddress, AFAddressFamily<?> af) Creates a new socket address.protectedAFSocketAddress(Class<org.newsclub.net.unix.SentinelSocketAddress> clazz, int port) Only forSentinelSocketAddress. -
Method Summary
Modifier and TypeMethodDescriptionfinal AFAddressFamily<?>Returns the correspondingAFAddressFamily.protected final byte[]getBytes()Returns the (non-native) byte-level representation of this address.abstract FilegetFile()Returns theFilecorresponding with this address, if possible.protected final InetAddressWraps this address as anInetAddress.protected static final InetAddressgetInetAddress(FileDescriptor fdesc, boolean peerName, AFAddressFamily<?> af) Wraps the socket name/peer name of a file descriptor as anInetAddress.protected final InetAddressgetInetAddress(AFAddressFamily<?> af) Wraps an address as anInetAddress.protected static final <A extends AFSocketAddress>
@Nullable AgetSocketAddress(FileDescriptor fdesc, boolean peerName, int port, AFAddressFamily<A> af) Gets the socket name/peer name of a file descriptor as anAFSocketAddress.abstract booleanChecks if the address can be resolved to aFile.protected static final booleanisSupportedAddress(InetAddress addr, AFAddressFamily<?> af) Checks if the given address is supported by this address family.Creates a new server socket bound to this address.AFSocket<?>Creates a new socket connected to this address.Creates a new server socket force-bound to this address (i.e., any additional call toServerSocket.bind(SocketAddress)will ignore the passed address and use this one instead.static AFSocketAddressTries to parse the given URI and return a correspondingAFSocketAddressfor it.static AFSocketAddressTries to parse the given URI and return a correspondingAFSocketAddressfor it.protected static final <A extends AFSocketAddress>
AresolveAddress(byte[] socketAddress, int port, AFAddressFamily<A> af) Resolves a junixsocket-specific byte-array representation of anAFSocketAddressto an actualAFSocketAddressinstance, possibly reusing a cached instance.@Nullable StringtoSocatAddressString(AFSocketType socketType, AFSocketProtocol socketProtocol) Returns a address string that can be used withsocat'sSOCKET-CONNECT,SOCKET-LISTEN,SOCKET-DATAGRAM, etc., address types, ornullif the address type is not natively supported by this platform.Tries to create a URI based on thisAFSocketAddress.protected static final <A extends AFSocketAddress>
@NonNull Aunwrap(String hostname, int port, AFAddressFamily<A> af) Returns anAFSocketAddressgiven a specialInetAddresshostname that encodes the byte sequence of an AF_UNIX etc. socket address, like those returned bywrapAddress().protected static final <A extends AFSocketAddress>
@NonNull Aunwrap(InetAddress address, int port, AFAddressFamily<A> af) Returns anAFSocketAddressgiven a specialInetAddressthat encodes the byte sequence of an AF_UNIX etc. socket address, like those returned bywrapAddress().final InetAddressReturns a "special"InetAddressthat contains information about thisAFSocketAddress.final voidWrites the native (system-level) representation of this address to the given buffer.Methods inherited from class java.net.InetSocketAddress
createUnresolved, equals, getAddress, getHostName, getHostString, getPort, hashCode, isUnresolved, toString
-
Constructor Details
-
AFSocketAddress
protected AFSocketAddress(int port, byte[] socketAddress, ByteBuffer nativeAddress, AFAddressFamily<?> af) throws SocketException Creates a new socket address.- Parameters:
port- The port.socketAddress- The socket address in junixsocket-specific byte-array representation.nativeAddress- The socket address in system-native representation.af- The address family.- Throws:
SocketException- on error.
-
AFSocketAddress
Only forSentinelSocketAddress.- Parameters:
clazz- TheSentinelSocketAddressclass.port- A sentinel port number.
-
-
Method Details
-
hasFilename
public abstract boolean hasFilename()Checks if the address can be resolved to aFile.- Returns:
trueif the address has a filename.
-
getFile
Returns theFilecorresponding with this address, if possible. AFileNotFoundExceptionis thrown if there is no filename associated with the address, which applies to addresses in the abstract namespace, for example.- Returns:
- The filename.
- Throws:
FileNotFoundException- if the address is not associated with a filename.
-
getAddressFamily
Returns the correspondingAFAddressFamily.- Returns:
- The address family instance.
-
getInetAddress
protected static final InetAddress getInetAddress(FileDescriptor fdesc, boolean peerName, AFAddressFamily<?> af) Wraps the socket name/peer name of a file descriptor as anInetAddress.- Parameters:
fdesc- The file descriptor.peerName- Iftrue, the remote peer name (instead of the local name) is retrieved.af- The address family.- Returns:
- The
InetAddress.
-
getSocketAddress
protected static final <A extends AFSocketAddress> @Nullable A getSocketAddress(FileDescriptor fdesc, boolean peerName, int port, AFAddressFamily<A> af) Gets the socket name/peer name of a file descriptor as anAFSocketAddress.- Type Parameters:
A- The corresponding address type.- Parameters:
fdesc- The file descriptor.peerName- Iftrue, the remote peer name (instead of the local name) is retrieved.port- The port.af- The address family.- Returns:
- The
InetAddress.
-
getBytes
protected final byte[] getBytes()Returns the (non-native) byte-level representation of this address.- Returns:
- The byte array.
-
wrapAddress
Returns a "special"InetAddressthat contains information about thisAFSocketAddress. IMPORTANT: ThisInetAddressdoes not properly compare (usingInetAddress.equals(Object)andInetAddress.hashCode()). It should be used exclusively to circumvent existing APIs likeDatagramSocketthat only accept/returnInetAddressand not arbitrarySocketAddresstypes.- Returns:
- The "special"
InetAddress.
-
resolveAddress
protected static final <A extends AFSocketAddress> A resolveAddress(byte[] socketAddress, int port, AFAddressFamily<A> af) throws SocketException Resolves a junixsocket-specific byte-array representation of anAFSocketAddressto an actualAFSocketAddressinstance, possibly reusing a cached instance.- Type Parameters:
A- The concreteAFSocketAddressthat is supported by this type.- Parameters:
socketAddress- The socket address in junixsocket-specific byte-array representation.port- The port.af- The address family.- Returns:
- The instance.
- Throws:
SocketException- on error.
-
getInetAddress
Wraps an address as anInetAddress.- Parameters:
af- The address family.- Returns:
- The
InetAddress.
-
getInetAddress
Wraps this address as anInetAddress.- Returns:
- The
InetAddress.
-
unwrap
protected static final <A extends AFSocketAddress> @NonNull A unwrap(InetAddress address, int port, AFAddressFamily<A> af) throws SocketException Returns anAFSocketAddressgiven a specialInetAddressthat encodes the byte sequence of an AF_UNIX etc. socket address, like those returned bywrapAddress().- Type Parameters:
A- The corresponding address type.- Parameters:
address- The "special"InetAddress.port- The port (use 0 for "none").af- The address family.- Returns:
- The
AFSocketAddressinstance. - Throws:
SocketException- if the operation fails, for example when an unsupported address is specified.
-
unwrap
protected static final <A extends AFSocketAddress> @NonNull A unwrap(String hostname, int port, AFAddressFamily<A> af) throws SocketException Returns anAFSocketAddressgiven a specialInetAddresshostname that encodes the byte sequence of an AF_UNIX etc. socket address, like those returned bywrapAddress().- Type Parameters:
A- The corresponding address type.- Parameters:
hostname- The "special" hostname, as provided byInetAddress.getHostName().port- The port (use 0 for "none").af- The address family.- Returns:
- The
AFSocketAddressinstance. - Throws:
SocketException- if the operation fails, for example when an unsupported address is specified.
-
isSupportedAddress
Checks if the given address is supported by this address family.- Parameters:
addr- The address.af- The address family.- Returns:
trueif supported.
-
writeNativeAddressTo
Writes the native (system-level) representation of this address to the given buffer. The position of the target buffer will be at the end (i.e., after) the written data.- Parameters:
buf- The target buffer.- Throws:
IOException- on error.
-
newConnectedSocket
Creates a new socket connected to this address.- Returns:
- The socket instance.
- Throws:
IOException- on error.
-
newBoundServerSocket
Creates a new server socket bound to this address.- Returns:
- The server socket instance.
- Throws:
IOException- on error.
-
newForceBoundServerSocket
Creates a new server socket force-bound to this address (i.e., any additional call toServerSocket.bind(SocketAddress)will ignore the passed address and use this one instead.- Returns:
- The server socket instance.
- Throws:
IOException- on error.
-
of
Tries to parse the given URI and return a correspondingAFSocketAddressfor it. NOTE: Only certain URI schemes are supported, such asunix://(forAFUNIXSocketAddress) andtipc://forAFTIPCSocketAddress.- Parameters:
u- The URI.- Returns:
- The address.
- Throws:
SocketException- on error.- See Also:
-
of
Tries to parse the given URI and return a correspondingAFSocketAddressfor it. NOTE: Only certain URI schemes are supported, such asunix://(forAFUNIXSocketAddress) andtipc://forAFTIPCSocketAddress.- Parameters:
u- The URI.overridePort- The port to forcibly use, or-1for "don't override".- Returns:
- The address.
- Throws:
SocketException- on error.- See Also:
-
toURI
Tries to create a URI based on thisAFSocketAddress.- Parameters:
scheme- The target scheme.template- An optional template to reuse certain parameters (e.g., the "path" component for anhttprequest), ornull.- Returns:
- The URI.
- Throws:
IOException- on error.
-
toSocatAddressString
public @Nullable String toSocatAddressString(AFSocketType socketType, AFSocketProtocol socketProtocol) throws IOException Returns a address string that can be used withsocat'sSOCKET-CONNECT,SOCKET-LISTEN,SOCKET-DATAGRAM, etc., address types, ornullif the address type is not natively supported by this platform. This call is mostly suited for debugging purposes. The resulting string is specific to the platform the code is executed on, and thus may be different among platforms (ornull).- Parameters:
socketType- The socket type, ornullto omit from string.socketProtocol- The socket protocol, ornullto omit from string.- Returns:
- The string (such as 1:0:x2f746d702f796f), or
nullif unable to retrieve. - Throws:
IOException- on error.
-