java.lang.Object
org.newsclub.net.unix.FileDescriptorCast
- All Implemented Interfaces:
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 Summary
Modifier and TypeMethodDescription<K> @NonNull K
Casts this instance to the desired type.Returns a collection of available types this instance can be cast to (usingas(Class)
).Returns the correspondingFileDescriptor
.boolean
isAvailable
(Class<?> desiredType) Checks if the instance can be cast as the given desired type (usingas(Class)
).static FileDescriptorCast
using
(FileDescriptor fdObj) Creates aFileDescriptorCast
using the given file descriptor.withLocalPort
(int port) Registers the given port number as the "local port" for this file descriptor.withRemotePort
(int port) Registers the given port number as the "remote port" for this file descriptor.
-
Method Details
-
using
Creates aFileDescriptorCast
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
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
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
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
Checks if the instance can be cast as the given desired type (usingas(Class)
).- Parameters:
desiredType
- The class of the desired type.- Returns:
true
if the cast can be made.- Throws:
IOException
- on error.- See Also:
-
availableTypes
Returns a collection of available types this instance can be cast to (usingas(Class)
).- Returns:
- The collection of available types.
-
getFileDescriptor
Description copied from interface:FileDescriptorAccess
Returns the correspondingFileDescriptor
.- Specified by:
getFileDescriptor
in interfaceFileDescriptorAccess
- Returns:
- The corresponding
FileDescriptor
.
-