{"record":{"id":"aa95682b5d46bb6d","repo":"TheAlgorithms/Java","slug":"not-a-valid-ipv6-mapped-ipv4-address","errorCode":null,"errorMessage":"Not a valid IPv6-mapped IPv4 address.","messagePattern":"Not a valid IPv6-mapped IPv4 address\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/IPv6Converter.java","lineNumber":72,"sourceCode":"    /**\n     * Extracts the IPv4 address from an IPv6-mapped IPv6 address.\n     * Example: IPv6 \"::ffff:192.0.2.128\" -> IPv4 \"192.0.2.128\"\n     *\n     * @param ipv6Address The IPv6 address in string format.\n     * @return The extracted IPv4 address.\n     * @throws UnknownHostException If the IPv6 address is invalid or not a mapped IPv4 address.\n     */\n    public static String ipv6ToIpv4(String ipv6Address) throws UnknownHostException {\n        InetAddress ipv6 = InetAddress.getByName(ipv6Address);\n        byte[] ipv6Bytes = ipv6.getAddress();\n\n        // Check if the address is an IPv6-mapped IPv4 address\n        if (isValidIpv6MappedIpv4(ipv6Bytes)) {\n            byte[] ipv4Bytes = Arrays.copyOfRange(ipv6Bytes, 12, 16);\n            InetAddress ipv4 = InetAddress.getByAddress(ipv4Bytes);\n            return ipv4.getHostAddress();\n        } else {\n            throw new IllegalArgumentException(\"Not a valid IPv6-mapped IPv4 address.\");\n        }\n    }\n\n    /**\n     * Helper function to check if the given byte array represents\n     * an IPv6-mapped IPv4 address (prefix 0:0:0:0:0:ffff).\n     *\n     * @param ipv6Bytes Byte array representation of the IPv6 address.\n     * @return True if the address is IPv6-mapped IPv4, otherwise false.\n     */\n    private static boolean isValidIpv6MappedIpv4(byte[] ipv6Bytes) {\n        // IPv6-mapped IPv4 addresses are 16 bytes long, with the first 10 bytes set to 0,\n        // followed by 0xff, 0xff, and the last 4 bytes representing the IPv4 address.\n        if (ipv6Bytes.length != 16) {\n            return false;\n        }\n\n        for (int i = 0; i < 10; i++) {","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/IPv6Converter.java#L54-L90","documentation":"Thrown by IPv6Converter.ipv6ToIpv4 as an IllegalArgumentException when the resolved address does not have the IPv6-mapped IPv4 prefix (80 zero bits followed by 0xFFFF). Only addresses matching the pattern ::ffff:x.x.x.x are accepted; all other IPv6 addresses are rejected. The internal isValidIpv6MappedIpv4 checks that the 16-byte array has bytes 0–9 set to zero and bytes 10–11 set to 0xFF.","triggerScenarios":"Calling ipv6ToIpv4 with a native IPv6 address like '2001:db8::1'. Passing an address that is actually an IPv4 literal (4 bytes), which gets resolved to a 4-byte array that fails the 16-byte length check. Passing '::1' (loopback) or any address without the ::ffff: prefix.","commonSituations":"Receiving an address from a socket or HTTP header that may be IPv4, IPv4-mapped IPv6, or native IPv6. Accepting user-entered addresses that could be either format. Logs from a dual-stack server where some connections arrive as native IPv6.","solutions":["Before calling ipv6ToIpv4, check that the address string starts with '::ffff:' or use InetAddress to inspect whether it is IPv4-mapped.","If you need to handle both native IPv6 and IPv4-mapped addresses, branch on the address type first using InetAddress.getByName(addr).getAddress().length.","Catch IllegalArgumentException and fall back to returning the original address if it is not IPv4-mapped."],"exampleFix":"// before\nString ipv4 = IPv6Converter.ipv6ToIpv4(addr); // addr may be native IPv6\n\n// after\nString ipv4;\ntry {\n    ipv4 = IPv6Converter.ipv6ToIpv4(addr);\n} catch (IllegalArgumentException e) {\n    // Not an IPv4-mapped address; handle as native IPv6 or IPv4\n    ipv4 = addr; // or log and skip\n}","handlingStrategy":"try-catch","validationCode":"InetAddress addr = InetAddress.getByName(ipv6Address);\nbyte[] bytes = addr.getAddress();\nboolean isMapped = bytes.length == 16\n    && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0 && bytes[3] == 0\n    && bytes[4] == 0 && bytes[5] == 0 && bytes[6] == 0 && bytes[7] == 0\n    && bytes[8] == 0 && bytes[9] == 0 && bytes[10] == (byte) 0xff && bytes[11] == (byte) 0xff;\nif (!isMapped) {\n    throw new IllegalArgumentException(\"Not an IPv4-mapped IPv6 address\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    String ipv4 = IPv6Converter.ipv6ToIpv4(ipv6Address);\n} catch (IllegalArgumentException e) {\n    // not an IPv4-mapped address; treat as native IPv6 or return original\n    String ipv4 = ipv6Address;\n}","preventionTips":["Check the address type before conversion using InetAddress.getAddress().length (4 = IPv4, 16 = IPv6).","Handle both native IPv6 and IPv4-mapped addresses in dual-stack code.","Document which address formats each code path expects."],"tags":["network","ipv4","ipv6","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}