Class AFSocketAddress

All Implemented Interfaces:
Serializable
Direct Known Subclasses:
AFTIPCSocketAddress, AFUNIXSocketAddress

public abstract class AFSocketAddress extends InetSocketAddress
Some SocketAddress that is supported by junixsocket, such as AFUNIXSocketAddress.
Author:
Christian Kohlschütter
See Also:
  • 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

      protected AFSocketAddress(Class<org.newsclub.net.unix.SentinelSocketAddress> clazz, int port)
      Only for SentinelSocketAddress.
      Parameters:
      clazz - The SentinelSocketAddress class.
      port - A sentinel port number.
  • Method Details

    • hasFilename

      public abstract boolean hasFilename()
      Checks if the address can be resolved to a File.
      Returns:
      true if the address has a filename.
    • getFile

      public abstract File getFile() throws FileNotFoundException
      Returns the File corresponding with this address, if possible. A FileNotFoundException is 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

      public final AFAddressFamily<?> getAddressFamily()
      Returns the corresponding AFAddressFamily.
      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 an InetAddress.
      Parameters:
      fdesc - The file descriptor.
      peerName - If true, 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 an AFSocketAddress.
      Type Parameters:
      A - The corresponding address type.
      Parameters:
      fdesc - The file descriptor.
      peerName - If true, 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

      public final InetAddress wrapAddress()
      Returns a "special" InetAddress that contains information about this AFSocketAddress. IMPORTANT: This InetAddress does not properly compare (using InetAddress.equals(Object) and InetAddress.hashCode()). It should be used exclusively to circumvent existing APIs like DatagramSocket that only accept/return InetAddress and not arbitrary SocketAddress types.
      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 an AFSocketAddress to an actual AFSocketAddress instance, possibly reusing a cached instance.
      Type Parameters:
      A - The concrete AFSocketAddress that 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

      protected final InetAddress getInetAddress(AFAddressFamily<?> af)
      Wraps an address as an InetAddress.
      Parameters:
      af - The address family.
      Returns:
      The InetAddress.
    • getInetAddress

      protected final InetAddress getInetAddress()
      Wraps this address as an InetAddress.
      Returns:
      The InetAddress.
    • unwrap

      protected static final <A extends AFSocketAddress> @NonNull A unwrap(InetAddress address, int port, AFAddressFamily<A> af) throws SocketException
      Returns an AFSocketAddress given a special InetAddress that encodes the byte sequence of an AF_UNIX etc. socket address, like those returned by wrapAddress().
      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 AFSocketAddress instance.
      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 an AFSocketAddress given a special InetAddress hostname that encodes the byte sequence of an AF_UNIX etc. socket address, like those returned by wrapAddress().
      Type Parameters:
      A - The corresponding address type.
      Parameters:
      hostname - The "special" hostname, as provided by InetAddress.getHostName().
      port - The port (use 0 for "none").
      af - The address family.
      Returns:
      The AFSocketAddress instance.
      Throws:
      SocketException - if the operation fails, for example when an unsupported address is specified.
    • isSupportedAddress

      protected static final boolean isSupportedAddress(InetAddress addr, AFAddressFamily<?> af)
      Checks if the given address is supported by this address family.
      Parameters:
      addr - The address.
      af - The address family.
      Returns:
      true if supported.
    • writeNativeAddressTo

      public final void writeNativeAddressTo(ByteBuffer buf) throws IOException
      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

      public AFSocket<?> newConnectedSocket() throws IOException
      Creates a new socket connected to this address.
      Returns:
      The socket instance.
      Throws:
      IOException - on error.
    • newBoundServerSocket

      public AFServerSocket<?> newBoundServerSocket() throws IOException
      Creates a new server socket bound to this address.
      Returns:
      The server socket instance.
      Throws:
      IOException - on error.
    • newForceBoundServerSocket

      public AFServerSocket<?> newForceBoundServerSocket() throws IOException
      Creates a new server socket force-bound to this address (i.e., any additional call to ServerSocket.bind(SocketAddress) will ignore the passed address and use this one instead.
      Returns:
      The server socket instance.
      Throws:
      IOException - on error.
    • of

      public static AFSocketAddress of(URI u) throws SocketException
      Tries to parse the given URI and return a corresponding AFSocketAddress for it. NOTE: Only certain URI schemes are supported, such as unix:// (for AFUNIXSocketAddress) and tipc:// for AFTIPCSocketAddress.
      Parameters:
      u - The URI.
      Returns:
      The address.
      Throws:
      SocketException - on error.
      See Also:
    • of

      public static AFSocketAddress of(URI u, int overridePort) throws SocketException
      Tries to parse the given URI and return a corresponding AFSocketAddress for it. NOTE: Only certain URI schemes are supported, such as unix:// (for AFUNIXSocketAddress) and tipc:// for AFTIPCSocketAddress.
      Parameters:
      u - The URI.
      overridePort - The port to forcibly use, or -1 for "don't override".
      Returns:
      The address.
      Throws:
      SocketException - on error.
      See Also:
    • toURI

      public URI toURI(String scheme, URI template) throws IOException
      Tries to create a URI based on this AFSocketAddress.
      Parameters:
      scheme - The target scheme.
      template - An optional template to reuse certain parameters (e.g., the "path" component for an http request), or null.
      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 with socat's SOCKET-CONNECT, SOCKET-LISTEN, SOCKET-DATAGRAM, etc., address types, or null if 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 (or null).
      Parameters:
      socketType - The socket type, or null to omit from string.
      socketProtocol - The socket protocol, or null to omit from string.
      Returns:
      The string (such as 1:0:x2f746d702f796f), or null if unable to retrieve.
      Throws:
      IOException - on error.