Convert IPv6 adress into RFC 5952 form. E.g. 2001:db8:0:1:0:0:0:1 -> 2001:db8:0:1::1
Method is null safe, and if IPv4 address or host name is passed to the method it is returned wihout any processing.
Method also supports IPv4 in IPv6 (e.g. 0:0:0:0:0:ffff:192.0.2.1 -> ::ffff:192.0.2.1), and zone ID (e.g. fe80:0:0:0:f0f0:c0c0:1919:1234%4 -> fe80::f0f0:c0c0:1919:1234%4).
76 if (ipv6Address == null) {
86 int ipv6AddressLength = ipv6Address.length();
90 int lastColonPos = ipv6Address.lastIndexOf(
":");
91 int lastColonsPos = ipv6Address.lastIndexOf(
"::");
92 if (lastColonsPos >= 0 && lastColonPos == lastColonsPos + 1) {
95 ipv6AddressLength = lastColonPos + 1;
99 ipv6AddressLength = lastColonPos;
101 }
else if (ipv6Address.contains(
":") && ipv6Address.contains(
"%")) {
104 ipv6AddressLength = ipv6Address.lastIndexOf(
"%");
107 StringBuilder result =
new StringBuilder();
109 int groupCounter = 0;
110 int charInGroupCounter = 0;
113 int zeroGroupIndex = -1;
114 int zeroGroupLength = 0;
117 int maxZeroGroupIndex = -1;
118 int maxZeroGroupLength = 0;
120 boolean isZero =
true;
121 boolean groupStart =
true;
127 StringBuilder expanded =
new StringBuilder(ipv6Address);
128 int colonsPos = ipv6Address.indexOf(
"::");
129 int length = ipv6AddressLength;
132 if (colonsPos >= 0 && colonsPos < ipv6AddressLength - 2) {
133 int colonCounter = 0;
134 for (
int i = 0; i < ipv6AddressLength; i++) {
135 if (ipv6Address.charAt(i) ==
':') {
140 if (colonsPos == 0) {
141 expanded.insert(0,
"0");
145 for (
int i = 0; i <
IPV6_LEN - colonCounter; i++) {
146 expanded.insert(colonsPos + 1,
"0:");
151 if (colonsPos == ipv6AddressLength - 2) {
152 expanded.setCharAt(colonsPos + change + 1,
'0');
154 expanded.deleteCharAt(colonsPos + change + 1);
157 length = length + change;
162 for (
int charCounter = 0; charCounter < length; charCounter++) {
163 char c = expanded.charAt(charCounter);
164 if (c >=
'A' && c <=
'F') {
168 groups[groupCounter][charInGroupCounter] = c;
169 if (!(groupStart && c ==
'0')) {
170 ++charInGroupCounter;
177 if (c ==
':' || charCounter == (length - 1)) {
181 if (zeroGroupIndex == -1) {
182 zeroGroupIndex = groupCounter;
186 if (!isZero || charCounter == (length - 1)) {
188 if (zeroGroupLength > maxZeroGroupLength) {
189 maxZeroGroupLength = zeroGroupLength;
190 maxZeroGroupIndex = zeroGroupIndex;
196 charInGroupCounter = 0;
202 int numberOfGroups = groupCounter;
205 for (groupCounter = 0; groupCounter < numberOfGroups; groupCounter++) {
206 if (maxZeroGroupLength <= 1 || groupCounter < maxZeroGroupIndex
207 || groupCounter >= maxZeroGroupIndex + maxZeroGroupLength) {
209 if (groups[groupCounter][j] != 0) {
210 result.append(groups[groupCounter][j]);
213 if (groupCounter < (numberOfGroups - 1)
214 && (groupCounter != maxZeroGroupIndex - 1
215 || maxZeroGroupLength <= 1)) {
218 }
else if (groupCounter == maxZeroGroupIndex) {
225 int resultLength = result.length();
226 if (result.charAt(resultLength - 1) ==
':' && ipv6AddressLength < ipv6Address.length()
227 && ipv6Address.charAt(ipv6AddressLength) ==
':') {
228 result.delete(resultLength - 1, resultLength);
234 for (
int i = ipv6AddressLength; i < ipv6Address.length(); i++) {
235 result.append(ipv6Address.charAt(i));
238 return result.toString();
static boolean isIPv4AddressInIPv6(String ipv6Address)
Definition: NetworkUtils.java:280
static boolean mayBeIPv6Address(String input)
Definition: NetworkUtils.java:247
static final int IPV6_LEN
Definition: NetworkUtils.java:35
static final int MAX_GROUP_LENGTH
Definition: NetworkUtils.java:34