Class FileDescriptorCast

java.lang.Object
org.newsclub.net.unix.FileDescriptorCast
All Implemented Interfaces:
FileDescriptorAccess

public final class FileDescriptorCast extends Object implements FileDescriptorAccess
Provides object-oriented access to file descriptors via InputStream, Socket, etc., depending on the file descriptor type.

Typical usage:


 FileDescriptor fd;

 // succeeds if fd refers to an AF_UNIX stream socket
 AFUNIXSocket socket = FileDescriptorCast.using(fd).as(AFUNIXSocket.class);

 // succeeds if fd refers to an AF_UNIX datagram socket
 AFUNIXDatagramChannel channel = FileDescriptorCast.using(fd).as(AFUNIXDatagramChannel.class);

 // always succeeds
 InputStream in = FileDescriptorCast.using(fd).as(InputStream.class);
 OutputStream in = FileDescriptorCast.using(fd).as(OutputStream.class);
 

IMPORTANT: On some platforms (e.g., Solaris, Illumos) you may need to re-apply a read timeout (e.g., using Socket.setSoTimeout(int)) after obtaining the socket.

Note that you may also lose Java port information for AFSocketAddress implementations that do not encode this information directly (such as AFUNIXSocketAddress and AFTIPCSocketAddress).

Author:
Christian Kohlschütter
  • Method Details

    • using

      public static FileDescriptorCast using(FileDescriptor fdObj) throws IOException
      Creates a FileDescriptorCast using the given file descriptor.
      Parameters:
      fdObj - The file descriptor.
      Returns:
      The FileDescriptorCast instance.
      Throws:
      IOException - on error, especially if the given file descriptor is invalid or unsupported.
    • withLocalPort

      public FileDescriptorCast withLocalPort(int port)
      Registers the given port number as the "local port" for this file descriptor. Important: This only changes the state of this instance. The actual file descriptor is not affected.
      Parameters:
      port - The port to assign to (must be >= 0).
      Returns:
      This instance.
    • withRemotePort

      public FileDescriptorCast withRemotePort(int port)
      Registers the given port number as the "remote port" for this file descriptor. Important: This only changes the state of this instance. The actual file descriptor is not affected.
      Parameters:
      port - The port to assign to (must be >= 0).
      Returns:
      This instance.
    • as

      public <K> @NonNull K as(Class<K> desiredType) throws IOException
      Casts this instance to the desired type.
      Type Parameters:
      K - The desired type.
      Parameters:
      desiredType - The class of the desired type.
      Returns:
      s An instance of the desired type.
      Throws:
      IOException - if there was a problem while casting.
      ClassCastException - if the cast cannot be legally made.
      See Also:
    • isAvailable

      public boolean isAvailable(Class<?> desiredType) throws IOException
      Checks if the instance can be cast as the given desired type (using as(Class)).
      Parameters:
      desiredType - The class of the desired type.
      Returns:
      true if the cast can be made.
      Throws:
      IOException - on error.
      See Also:
    • availableTypes

      public Set<Class<?>> availableTypes()
      Returns a collection of available types this instance can be cast to (using as(Class)).
      Returns:
      The collection of available types.
    • getFileDescriptor

      public FileDescriptor getFileDescriptor()
      Description copied from interface: FileDescriptorAccess
      Returns the corresponding FileDescriptor.
      Specified by:
      getFileDescriptor in interface FileDescriptorAccess
      Returns:
      The corresponding FileDescriptor.