diff --git a/transport-native-unix-common/src/main/java/io/netty5/channel/unix/Errors.java b/transport-native-unix-common/src/main/java/io/netty5/channel/unix/Errors.java index 4f880496df9..fb0ff4e8f22 100644 --- a/transport-native-unix-common/src/main/java/io/netty5/channel/unix/Errors.java +++ b/transport-native-unix-common/src/main/java/io/netty5/channel/unix/Errors.java @@ -64,10 +64,11 @@ public final class Errors { * Holds the mappings for errno codes to String messages. * This eliminates the need to call back into JNI to get the right String message on an exception * and thus is faster. - *

- * The array length of 512 should be more then enough because errno.h only holds < 200 codes. + * + * Choose an array length which should give us enough space in the future even when more errno codes + * will be added. */ - private static final String[] ERRORS = new String[512]; + private static final String[] ERRORS = new String[2048]; /** * Internal usage only! @@ -82,7 +83,7 @@ public NativeIoException(String method, int expectedErr) { } public NativeIoException(String method, int expectedErr, boolean fillInStackTrace) { - super(method + "(..) failed: " + ERRORS[-expectedErr]); + super(method + "(..) failed: " + errnoString(-expectedErr)); this.expectedErr = expectedErr; this.fillInStackTrace = fillInStackTrace; } @@ -116,6 +117,14 @@ static boolean handleConnectErrno(String method, int err) throws IOException { throw newConnectException0(method, err); } + private static String errnoString(int err) { + // Check first if we had it cached, if not we need to do a JNI call. + if (err < ERRORS.length - 1) { + return ERRORS[err]; + } + return strError(err); + } + private static IOException newConnectException0(String method, int err) { if (err == ERROR_ENETUNREACH_NEGATIVE) { return new NoRouteToHostException(); @@ -126,7 +135,7 @@ private static IOException newConnectException0(String method, int err) { if (err == ERRNO_ENOENT_NEGATIVE) { return new FileNotFoundException(); } - return new ConnectException(method + "(..) failed: " + ERRORS[-err]); + return new ConnectException(method + "(..) failed: " + errnoString(-err)); } public static NativeIoException newConnectionResetException(String method, int errnoNegative) {