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 notes
Socket#setSoTimeout(int)) after obtaining the socket.AFSocketAddress implementations that do not
encode this information directly (such as AFUNIXSocketAddress and
AFTIPCSocketAddress).Socket, DatagramSocket or ServerSocket
and any of their subclasses where "blocking" is the expected state.using(FileDescriptor) for a FileDescriptor obtained from
another socket or other resource in the same JVM (i.e., not from another process), especially for
sockets provided by junixsocket itself, there is a chance that the garbage collector may clean up
the original socket at an opportune moment, thereby closing the resource underlying the shared
FileDescriptor prematurely.
This is considered an edge-case, and deliberately not handled automatically for performance and
portability reasons: We would have to do additional reference counting on all FileDescriptor
instances, either through patching FileCleanable or a shared data structure.
The issue can be prevented by keeping a reference to the original object, such as keeping it in
an enclosing try-with-resources block or as a member variable, for example. Alternatively, using
a "duplicate" file descriptor (via duplicating(FileDescriptor)) circumvents this
problem, at the cost of using additional system resources.
using(FileDescriptor): when casting file
descriptors that belong to a junixsocket-controlled sockets, the target socket is configured in a
way such that garbage collection will not automatically close the target's underlying file
descriptor (but still potentially any file descriptors received from other processes via
ancillary messages).using(FileDescriptor) apply to
unsafeUsing(int) as well.| Modifier and Type | Class and Description |
|---|---|
| private static interface | |
| private abstract static class | |
| private static interface | |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private final FileDescriptorCast. | |
| private static final int | |
| private static final int | |
| private static final AFFunction | |
| private static final int | |
| private final FileDescriptor | |
| private static final FileDescriptorCast. | |
| private static final FileDescriptorCast. | |
| private int | |
| private static final Map | |
| private int |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public < The desired type. K> @NonNull K | |
| public Set | Returns: The collection of available types.Returns a collection of available types this instance can be cast to (using
|
| public static FileDescriptorCast | Returns: TheFileDescriptorCast instance.The file descriptor to duplicate. fdObj)Creates a |
| private static int | |
| public FileDescriptor | getFileDescriptor()
Implements org. Returns the corresponding |
| public boolean | Returns: true if the cast can be made.The class of the desired type. desiredType)Checks if the instance can be cast as the given desired type (using |
| private static <S extends AFSocket | |
| private static <S extends AFServerSocket | |
| private static <S extends AFDatagramSocket | |
| private static < The type. S extends AFSomeSocketChannel> void | reconfigure(boolean
The desired cast type (socket=set to blocking, or channel=preserve state). isChannel, S The channel. socketChannel)Reconfigures the Java-side of the socket/socket channel such that its state is compatible with the native socket's state. |
| private static <S extends AFSomeSocketChannel> void | |
| private static <S extends AFSomeSocketChannel> void | |
| private static void | |
| pack-priv static <A extends AFSocketAddress> void | |
| private static void | |
| private static void | |
| public static FileDescriptorCast | Returns: TheFileDescriptorCast instance.The system-native file descriptor value. fd)Creates a |
| public static FileDescriptorCast | Returns: TheFileDescriptorCast instance.The file descriptor. fdObj)Creates a |
| public FileDescriptorCast | Returns: This instance.The port to assign to (must be >= 0). port)Registers the given port number as the "local port" for this file descriptor. |
| public FileDescriptorCast | Returns: This instance.The port to assign to (must be >= 0). port)Registers the given port number as the "remote port" for this file descriptor. |
| cpm | back to summary |
|---|---|
| private final FileDescriptorCast. | |
| FD_ERR | back to summary |
|---|---|
| private static final int FD_ERR | |
| FD_IN | back to summary |
|---|---|
| private static final int FD_IN | |
| FD_IS_PROVIDER | back to summary |
|---|---|
| private static final AFFunction<FileDescriptor, FileInputStream> FD_IS_PROVIDER | |
| FD_OUT | back to summary |
|---|---|
| private static final int FD_OUT | |
| fdObj | back to summary |
|---|---|
| private final FileDescriptor fdObj | |
| GLOBAL_PROVIDERS | back to summary |
|---|---|
| private static final FileDescriptorCast. | |
| GLOBAL_PROVIDERS_FINAL | back to summary |
|---|---|
| private static final FileDescriptorCast. | |
| localPort | back to summary |
|---|---|
| private int localPort | |
| PRIMARY_TYPE_PROVIDERS_MAP | back to summary |
|---|---|
| private static final Map<Class<?>, FileDescriptorCast. | |
| remotePort | back to summary |
|---|---|
| private int remotePort | |
| FileDescriptorCast | back to summary |
|---|---|
| private FileDescriptorCast(FileDescriptor fdObj, FileDescriptorCast. | |
| as | back to summary |
|---|---|
| public <K> @NonNull K as(Class<K> desiredType) throws IOException Casts this instance to the desired type.
| |
| availableTypes | back to summary |
|---|---|
| public Set Returns a collection of available types this instance can be cast to (using
| |
| duplicating | back to summary |
|---|---|
| public static FileDescriptorCast duplicating(FileDescriptor fdObj) throws IOException Creates a Duplicating a file descriptor is performed at the system-level, which means an additional file descriptor pointing to the same resource as the original is created by the operating system.
The advantage of using
| |
| getFdIfPossible | back to summary |
|---|---|
| private static int getFdIfPossible(FileDescriptor fd) | |
| getFileDescriptor | back to summary |
|---|---|
| public FileDescriptor getFileDescriptor() Implements org. Doc from org. Returns the corresponding
| |
| isAvailable | back to summary |
|---|---|
| public boolean isAvailable(Class<?> desiredType) throws IOException Checks if the instance can be cast as the given desired type (using
| |
| reconfigure | back to summary |
|---|---|
private static <S extends AFSocket
| |
| reconfigure | back to summary |
|---|---|
private static <S extends AFServerSocket
| |
| reconfigure | back to summary |
|---|---|
private static <S extends AFDatagramSocket
| |
| reconfigure | back to summary |
|---|---|
| private static <S extends AFSomeSocketChannel> void reconfigure(boolean isChannel, S socketChannel) throws IOException Reconfigures the Java-side of the socket/socket channel such that its state is compatible with the native socket's state. This is necessary to properly configure blocking/non-blocking state, as that is cached on the Java side.
If
If Note that on Windows, it may be impossible to query the blocking state from an external socket, so the state is always forcibly set to "blocking".
| |
| reconfigureKeepBlockingState | back to summary |
|---|---|
| private static <S extends AFSomeSocketChannel> void reconfigureKeepBlockingState(S socketChannel) throws IOException | |
| reconfigureSetBlocking | back to summary |
|---|---|
| private static <S extends AFSomeSocketChannel> void reconfigureSetBlocking(S socketChannel) throws IOException | |
| registerCastingProviders | back to summary |
|---|---|
| private static void registerCastingProviders(Class<?> primaryType, FileDescriptorCast. | |
| registerCastingProviders | back to summary |
|---|---|
| pack-priv static <A extends AFSocketAddress> void registerCastingProviders(AFAddressFamilyConfig<A> config) | |
| registerGenericSocketSupport | back to summary |
|---|---|
| private static void registerGenericSocketSupport() Add support for otherwise unsupported sockets. | |
| triggerInit | back to summary |
|---|---|
| private static void triggerInit() | |
| unsafeUsing | back to summary |
|---|---|
| public static FileDescriptorCast unsafeUsing(int fd) throws IOException Creates a This method is inherently unsafe as it may
Note that attempts are made to reuse
| |
| using | back to summary |
|---|---|
| public static FileDescriptorCast using(FileDescriptor fdObj) throws IOException Creates a
Note that if any resource that also references this
| |
| withLocalPort | back to summary |
|---|---|
| public FileDescriptorCast withLocalPort(int port) Registers the given port number as the "local port" for this file descriptor. Important Note This only changes the state of this instance. The actual file descriptor is not affected.
| |
| withRemotePort | back to summary |
|---|---|
| public FileDescriptorCast withRemotePort(int port) Registers the given port number as the "remote port" for this file descriptor. Important Note This only changes the state of this instance. The actual file descriptor is not affected.
| |
| Modifier and Type | Method and Description |
|---|---|
| public T |
| provideAs | back to summary |
|---|---|
| public T provideAs(FileDescriptorCast fdc, Class<? super T> desiredType) throws IOException | |
| Modifier and Type | Field and Description |
|---|---|
| private final Set | |
| private final Map |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| protected final <T> void | |
| private void | |
| protected abstract void | |
| protected final void | |
| public <T> FileDescriptorCast. | |
| protected void | |
| protected void |
| classes | back to summary |
|---|---|
| private final Set<Class<?>> classes | |
| providers | back to summary |
|---|---|
| private final Map<Class<?>, FileDescriptorCast. | |
| CastingProviderMap | back to summary |
|---|---|
protected CastingProviderMap()
| |
| addProvider | back to summary |
|---|---|
| protected final <T> void addProvider(Class<T> type, FileDescriptorCast. | |
| addProvider0 | back to summary |
|---|---|
| private void addProvider0(Class<?> type, FileDescriptorCast. | |
| addProviders | back to summary |
|---|---|
| protected abstract void addProviders() | |
| addProviders | back to summary |
|---|---|
| protected final void addProviders(FileDescriptorCast. | |
| get | back to summary |
|---|---|
public <T> FileDescriptorCast.
| |
| registerGenericDatagramSocketProviders | back to summary |
|---|---|
protected void registerGenericDatagramSocketProviders()
| |
| registerGenericSocketProviders | back to summary |
|---|---|
protected void registerGenericSocketProviders()
| |
| Modifier and Type | Method and Description |
|---|---|
| public T |
| provideAs | back to summary |
|---|---|
| public T provideAs(FileDescriptorCast fdc, Class<? super T> desiredType, boolean isChannel) throws IOException | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public int | available()
Overrides java. Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. |
| LenientFileInputStream | back to summary |
|---|---|
| private LenientFileInputStream(FileDescriptor fdObj) | |
| available | back to summary |
|---|---|
| public int available() throws IOException Overrides java. Doc from java. Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. Returns 0 when the file position is beyond EOF. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes. In some cases, a non-blocking read (or skip) may appear to be blocked when it is merely slow, for example when reading large files over slow networks.
| |