MCPcopy Create free account
hub / github.com/davidgiven/luje / isValidIP6Address

Method isValidIP6Address

lib/java/net/URI.java:668–789  ·  view source on GitHub ↗
(String ipAddress)

Source from the content-addressed store, hash-verified

666 }
667
668 private boolean isValidIP6Address(String ipAddress) {
669 int length = ipAddress.length();
670 boolean doubleColon = false;
671 int numberOfColons = 0;
672 int numberOfPeriods = 0;
673 String word = ""; //$NON-NLS-1$
674 char c = 0;
675 char prevChar = 0;
676 int offset = 0; // offset for [] ip addresses
677
678 if (length < 2) {
679 return false;
680 }
681
682 for (int i = 0; i < length; i++) {
683 prevChar = c;
684 c = ipAddress.charAt(i);
685 switch (c) {
686
687 // case for an open bracket [x:x:x:...x]
688 case '[':
689 if (i != 0) {
690 return false; // must be first character
691 }
692 if (ipAddress.charAt(length - 1) != ']') {
693 return false; // must have a close ]
694 }
695 if ((ipAddress.charAt(1) == ':')
696 && (ipAddress.charAt(2) != ':')) {
697 return false;
698 }
699 offset = 1;
700 if (length < 4) {
701 return false;
702 }
703 break;
704
705 // case for a closed bracket at end of IP [x:x:x:...x]
706 case ']':
707 if (i != length - 1) {
708 return false; // must be last character
709 }
710 if (ipAddress.charAt(0) != '[') {
711 return false; // must have a open [
712 }
713 break;
714
715 // case for the last 32-bits represented as IPv4
716 // x:x:x:x:x:x:d.d.d.d
717 case '.':
718 numberOfPeriods++;
719 if (numberOfPeriods > 3) {
720 return false;
721 }
722 if (!isValidIP4Word(word)) {
723 return false;
724 }
725 if (numberOfColons != 6 && !doubleColon) {

Callers 1

isValidHostMethod · 0.95

Calls 5

isValidIP4WordMethod · 0.95
lengthMethod · 0.95
isValidHexCharMethod · 0.95
lengthMethod · 0.65
charAtMethod · 0.65

Tested by

no test coverage detected