Class ErrnoCapture
java.lang.Object
express.mvp.roray.utils.functions.ErrnoCapture
Helper for capturing and interpreting
errno from native calls.
When a native function fails, it typically returns -1 and sets errno to indicate the
specific error. This class provides utilities to capture and interpret these error codes.
Usage Example
// Create a downcall that captures errno
MethodHandle open = factory.downcall("open", descriptor,
ErrnoCapture.captureOption());
// Make the call with errno capture
try (Arena arena = Arena.ofConfined()) {
MemorySegment capturedState = ErrnoCapture.allocateCaptureState(arena);
int fd = (int) open.invokeExact(capturedState, path, flags, mode);
if (fd < 0) {
int errno = ErrnoCapture.getErrno(capturedState);
throw new IOException(ErrnoCapture.strerror(errno));
}
}
Socket Connection Example
MethodHandle connect = factory.downcall("connect", connectDesc,
ErrnoCapture.captureOption());
try (Arena arena = Arena.ofConfined()) {
MemorySegment state = ErrnoCapture.allocateCaptureState(arena);
int result = (int) connect.invokeExact(state, sockfd, addr, addrlen);
if (result < 0) {
int errno = ErrnoCapture.getErrno(state);
switch (errno) {
case ErrnoCapture.EINPROGRESS -> {} // Non-blocking connect started
case ErrnoCapture.ECONNREFUSED -> throw new ConnectException("Connection refused");
case ErrnoCapture.ETIMEDOUT -> throw new SocketTimeoutException("Connect timed out");
default -> throw new IOException(ErrnoCapture.strerror(errno));
}
}
}
Important Notes
- When using
captureCallState, the MethodHandle signature changes: aMemorySegmentparameter is prepended for the captured state. - The captured state segment must be allocated before each call.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intPermission denied.static final intResource temporarily unavailable (same as EWOULDBLOCK).static final intOperation already in progress.static final intBad file descriptor.static final intDevice or resource busy.static final intConnection refused.static final intConnection reset by peer.static final intResource deadlock avoided.static final intFile exists.static final intBad address.static final intOperation now in progress.static final intInterrupted system call.static final intInvalid argument.static final intI/O error.static final intToo many open files.static final intNo such file or directory.static final intCannot allocate memory.static final intNo space left on device.static final intFunction not implemented.static final intOperation not permitted.static final intBroken pipe.static final intNo such process.static final intConnection timed out. -
Method Summary
Modifier and TypeMethodDescriptionstatic MemorySegmentallocateCaptureState(Arena arena) Allocates a memory segment suitable for capturing call state.static Linker.OptionReturns theLinker.Optionto capture errno.static longGets the size required for the capture state segment.static intgetErrno(MemorySegment capturedState) Extracts the errno value from a captured call state.static Stringstrerror(int errno) Returns a human-readable description of an errno value.
-
Field Details
-
EPERM
public static final int EPERMOperation not permitted.- See Also:
-
ENOENT
public static final int ENOENTNo such file or directory.- See Also:
-
ESRCH
public static final int ESRCHNo such process.- See Also:
-
EINTR
public static final int EINTRInterrupted system call.- See Also:
-
EIO
public static final int EIOI/O error.- See Also:
-
EBADF
public static final int EBADFBad file descriptor.- See Also:
-
EAGAIN
public static final int EAGAINResource temporarily unavailable (same as EWOULDBLOCK).- See Also:
-
ENOMEM
public static final int ENOMEMCannot allocate memory.- See Also:
-
EACCES
public static final int EACCESPermission denied.- See Also:
-
EFAULT
public static final int EFAULTBad address.- See Also:
-
EBUSY
public static final int EBUSYDevice or resource busy.- See Also:
-
EEXIST
public static final int EEXISTFile exists.- See Also:
-
EINVAL
public static final int EINVALInvalid argument.- See Also:
-
EMFILE
public static final int EMFILEToo many open files.- See Also:
-
ENOSPC
public static final int ENOSPCNo space left on device.- See Also:
-
EPIPE
public static final int EPIPEBroken pipe.- See Also:
-
EDEADLK
public static final int EDEADLKResource deadlock avoided.- See Also:
-
ENOSYS
public static final int ENOSYSFunction not implemented.- See Also:
-
ECONNREFUSED
public static final int ECONNREFUSEDConnection refused.- See Also:
-
ECONNRESET
public static final int ECONNRESETConnection reset by peer.- See Also:
-
ETIMEDOUT
public static final int ETIMEDOUTConnection timed out.- See Also:
-
EINPROGRESS
public static final int EINPROGRESSOperation now in progress.- See Also:
-
EALREADY
public static final int EALREADYOperation already in progress.- See Also:
-
-
Method Details
-
captureOption
Returns theLinker.Optionto capture errno.When this option is used, the resulting MethodHandle will have a
MemorySegmentprepended to its parameter list. This segment must be allocated usingallocateCaptureState(Arena)before each invocation.- Returns:
- Linker option for capturing errno
-
allocateCaptureState
Allocates a memory segment suitable for capturing call state.- Parameters:
arena- Arena to allocate from- Returns:
- MemorySegment for capturing errno
-
captureStateSize
public static long captureStateSize()Gets the size required for the capture state segment.- Returns:
- Size in bytes
-
getErrno
Extracts the errno value from a captured call state.- Parameters:
capturedState- The segment passed to the downcall- Returns:
- The errno value set by the native function
-
strerror
Returns a human-readable description of an errno value.- Parameters:
errno- The error number- Returns:
- String description of the error
-