Skip to content

Commit 6fc53ec

Browse files
authored
Add IPv6Converter algorithm (#5783)
1 parent 009c2b3 commit 6fc53ec

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
* [IntegerToEnglish](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToEnglish.java)
101101
* [IntegerToRoman](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java)
102102
* [IPConverter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IPConverter.java)
103+
* [IPv6Converter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IPv6Converter.java)
103104
* [MorseCodeConverter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/MorseCodeConverter.java)
104105
* [OctalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToBinary.java)
105106
* [OctalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java)
@@ -784,6 +785,7 @@
784785
* [IntegerToEnglishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IntegerToEnglishTest.java)
785786
* [IntegerToRomanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IntegerToRomanTest.java)
786787
* [IPConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IPConverterTest.java)
788+
* [IPv6ConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java)
787789
* [MorseCodeConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/MorseCodeConverterTest.java)
788790
* [OctalToBinaryTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToBinaryTest.java)
789791
* [OctalToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.thealgorithms.conversions;
2+
3+
import java.net.InetAddress;
4+
import java.net.UnknownHostException;
5+
import java.util.Arrays;
6+
7+
/**
8+
* A utility class for converting between IPv6 and IPv4 addresses.
9+
*
10+
* - Converts IPv4 to IPv6-mapped IPv6 address.
11+
* - Extracts IPv4 address from IPv6-mapped IPv6.
12+
* - Handles exceptions for invalid inputs.
13+
*
14+
* @author Hardvan
15+
*/
16+
public final class IPv6Converter {
17+
private IPv6Converter() {
18+
}
19+
20+
/**
21+
* Converts an IPv4 address (e.g., "192.0.2.128") to an IPv6-mapped IPv6 address.
22+
* Example: IPv4 "192.0.2.128" -> IPv6 "::ffff:192.0.2.128"
23+
*
24+
* @param ipv4Address The IPv4 address in string format.
25+
* @return The corresponding IPv6-mapped IPv6 address.
26+
* @throws UnknownHostException If the IPv4 address is invalid.
27+
* @throws IllegalArgumentException If the IPv6 address is not a mapped IPv4 address.
28+
*/
29+
public static String ipv4ToIpv6(String ipv4Address) throws UnknownHostException {
30+
if (ipv4Address == null || ipv4Address.isEmpty()) {
31+
throw new UnknownHostException("IPv4 address is empty.");
32+
}
33+
34+
InetAddress ipv4 = InetAddress.getByName(ipv4Address);
35+
byte[] ipv4Bytes = ipv4.getAddress();
36+
37+
// Create IPv6-mapped IPv6 address (starts with ::ffff:)
38+
byte[] ipv6Bytes = new byte[16];
39+
ipv6Bytes[10] = (byte) 0xff;
40+
ipv6Bytes[11] = (byte) 0xff;
41+
System.arraycopy(ipv4Bytes, 0, ipv6Bytes, 12, 4);
42+
43+
// Manually format to "::ffff:x.x.x.x" format
44+
StringBuilder ipv6String = new StringBuilder("::ffff:");
45+
for (int i = 12; i < 16; i++) {
46+
ipv6String.append(ipv6Bytes[i] & 0xFF);
47+
if (i < 15) {
48+
ipv6String.append('.');
49+
}
50+
}
51+
return ipv6String.toString();
52+
}
53+
54+
/**
55+
* Extracts the IPv4 address from an IPv6-mapped IPv6 address.
56+
* Example: IPv6 "::ffff:192.0.2.128" -> IPv4 "192.0.2.128"
57+
*
58+
* @param ipv6Address The IPv6 address in string format.
59+
* @return The extracted IPv4 address.
60+
* @throws UnknownHostException If the IPv6 address is invalid or not a mapped IPv4 address.
61+
*/
62+
public static String ipv6ToIpv4(String ipv6Address) throws UnknownHostException {
63+
InetAddress ipv6 = InetAddress.getByName(ipv6Address);
64+
byte[] ipv6Bytes = ipv6.getAddress();
65+
66+
// Check if the address is an IPv6-mapped IPv4 address
67+
if (isValidIpv6MappedIpv4(ipv6Bytes)) {
68+
byte[] ipv4Bytes = Arrays.copyOfRange(ipv6Bytes, 12, 16);
69+
InetAddress ipv4 = InetAddress.getByAddress(ipv4Bytes);
70+
return ipv4.getHostAddress();
71+
} else {
72+
throw new IllegalArgumentException("Not a valid IPv6-mapped IPv4 address.");
73+
}
74+
}
75+
76+
/**
77+
* Helper function to check if the given byte array represents
78+
* an IPv6-mapped IPv4 address (prefix 0:0:0:0:0:ffff).
79+
*
80+
* @param ipv6Bytes Byte array representation of the IPv6 address.
81+
* @return True if the address is IPv6-mapped IPv4, otherwise false.
82+
*/
83+
private static boolean isValidIpv6MappedIpv4(byte[] ipv6Bytes) {
84+
// IPv6-mapped IPv4 addresses are 16 bytes long, with the first 10 bytes set to 0,
85+
// followed by 0xff, 0xff, and the last 4 bytes representing the IPv4 address.
86+
if (ipv6Bytes.length != 16) {
87+
return false;
88+
}
89+
90+
for (int i = 0; i < 10; i++) {
91+
if (ipv6Bytes[i] != 0) {
92+
return false;
93+
}
94+
}
95+
96+
return ipv6Bytes[10] == (byte) 0xff && ipv6Bytes[11] == (byte) 0xff;
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.net.UnknownHostException;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class IPv6ConverterTest {
10+
11+
private static final String VALID_IPV4 = "192."
12+
+ "0."
13+
+ "2."
14+
+ "128";
15+
private static final String EXPECTED_IPV6_MAPPED = ":"
16+
+ ":ff"
17+
+ "ff"
18+
+ ":19"
19+
+ "2."
20+
+ "0."
21+
+ "2.128";
22+
private static final String INVALID_IPV6_MAPPED = "2001:"
23+
+ "db8"
24+
+ ":"
25+
+ ":1";
26+
private static final String INVALID_IPV4 = "999."
27+
+ "999."
28+
+ "999."
29+
+ "999";
30+
private static final String INVALID_IPV6_FORMAT = "invalid:ipv6"
31+
+ "::address";
32+
private static final String EMPTY_STRING = "";
33+
34+
@Test
35+
public void testIpv4ToIpv6ValidInput() throws UnknownHostException {
36+
String actualIpv6 = IPv6Converter.ipv4ToIpv6(VALID_IPV4);
37+
assertEquals(EXPECTED_IPV6_MAPPED, actualIpv6);
38+
}
39+
40+
@Test
41+
public void testIpv6ToIpv4InvalidIPv6MappedAddress() {
42+
assertThrows(IllegalArgumentException.class, () -> IPv6Converter.ipv6ToIpv4(INVALID_IPV6_MAPPED));
43+
}
44+
45+
@Test
46+
public void testIpv4ToIpv6InvalidIPv4Address() {
47+
assertThrows(UnknownHostException.class, () -> IPv6Converter.ipv4ToIpv6(INVALID_IPV4));
48+
}
49+
50+
@Test
51+
public void testIpv6ToIpv4InvalidFormat() {
52+
assertThrows(UnknownHostException.class, () -> IPv6Converter.ipv6ToIpv4(INVALID_IPV6_FORMAT));
53+
}
54+
55+
@Test
56+
public void testIpv4ToIpv6EmptyString() {
57+
assertThrows(UnknownHostException.class, () -> IPv6Converter.ipv4ToIpv6(EMPTY_STRING));
58+
}
59+
60+
@Test
61+
public void testIpv6ToIpv4EmptyString() {
62+
assertThrows(IllegalArgumentException.class, () -> IPv6Converter.ipv6ToIpv4(EMPTY_STRING));
63+
}
64+
}

0 commit comments

Comments
 (0)