// Definitely not an IPv6, return untouched input. if (!mayBeIPv6Address(ipv6Address)) { return ipv6Address;
}
// Length without zone ID (%zone) or IPv4 address int ipv6AddressLength = ipv6Address.length(); if (ipv6Address.contains(".")) { // IPv4 in IPv6 // e.g. 0:0:0:0:0:FFFF:127.0.0.1 int lastColonPos = ipv6Address.lastIndexOf(':'); int lastColonsPos = ipv6Address.lastIndexOf("::"); if (lastColonsPos >= 0 && lastColonPos == lastColonsPos + 1) { /* *IPv6partendswithtwoconsecutivecolons, *lastcolonispartofIPv6format. *e.g.::127.0.0.1
*/
ipv6AddressLength = lastColonPos + 1;
} else { /* *IPv6partendswithonlyonecolon, *lastcolonisnotpartofIPv6format. *e.g.::FFFF:127.0.0.1
*/
ipv6AddressLength = lastColonPos;
}
} elseif (ipv6Address.contains("%")) { // Zone ID // e.g. fe80:0:0:0:f0f0:c0c0:1919:1234%4
ipv6AddressLength = ipv6Address.lastIndexOf('%');
}
StringBuilder result = new StringBuilder(); char [][] groups = newchar[MAX_NUMBER_OF_GROUPS][MAX_GROUP_LENGTH]; int groupCounter = 0; int charInGroupCounter = 0;
// Index of the current zeroGroup, -1 means not found. int zeroGroupIndex = -1; int zeroGroupLength = 0;
// maximum length zero group, if there is more then one, then first one int maxZeroGroupIndex = -1; int maxZeroGroupLength = 0;
// Processing one char at the time for (int charCounter = 0; charCounter < length; charCounter++) { char c = expanded.charAt(charCounter); if (c >= 'A' && c <= 'F') {
c = (char) (c + 32);
} if (c != ':') {
groups[groupCounter][charInGroupCounter] = c; if (!(groupStart && c == '0')) {
++charInGroupCounter;
groupStart = false;
} if (c != '0') {
isZero = false;
}
} if (c == ':' || charCounter == (length - 1)) { // We reached end of current group if (isZero) {
++zeroGroupLength; if (zeroGroupIndex == -1) {
zeroGroupIndex = groupCounter;
}
}
if (!isZero || charCounter == (length - 1)) { // We reached end of zero group if (zeroGroupLength > maxZeroGroupLength) {
maxZeroGroupLength = zeroGroupLength;
maxZeroGroupIndex = zeroGroupIndex;
}
zeroGroupLength = 0;
zeroGroupIndex = -1;
}
++groupCounter;
charInGroupCounter = 0;
isZero = true;
groupStart = true;
}
}
// Solve problem with three colons in IPv4 in IPv6 format // e.g. 0:0:0:0:0:0:127.0.0.1 -> :::127.0.0.1 -> ::127.0.0.1 int resultLength = result.length(); if (result.charAt(resultLength - 1) == ':' && ipv6AddressLength < ipv6Address.length()
&& ipv6Address.charAt(ipv6AddressLength) == ':') {
result.delete(resultLength - 1, resultLength);
}
/* *AppendIPv4fromIPv4-in-IPv6formatorZoneID
*/ for (int i = ipv6AddressLength; i < ipv6Address.length(); i++) {
result.append(ipv6Address.charAt(i));
}
int colonsCounter = 0; int length = input.length(); for (int i = 0; i < length; i++) { char c = input.charAt(i); if (c == '.' || c == '%') { // IPv4 in IPv6 or Zone ID detected, end of checking. break;
} if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')
|| (c >= 'A' && c <= 'F') || c == ':')) { returnfalse;
} elseif (c == ':') {
colonsCounter++;
}
} if (colonsCounter < 2) { returnfalse;
} returntrue;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.