private static int scanHexSequence(String address,int index,int end,int[] counter){
  char testChar;
  int numDigits=0;
  int start=index;
  for (; index < end; ++index) {
    testChar=address.charAt(index);
    if (testChar == ':') {
      if (numDigits > 0 && ++counter[0] > 8) {
        return -1;
      }
      if (numDigits == 0 || ((index + 1 < end) && address.charAt(index + 1) == ':')) {
        return index;
      }
      numDigits=0;
    }
 else     if (!isHex(testChar)) {
      if (testChar == '.' && numDigits < 4 && numDigits > 0 && counter[0] <= 6) {
        int back=index - numDigits - 1;
        return (back >= start) ? back : (back + 1);
      }
      return -1;
    }
 else     if (++numDigits > 4) {
      return -1;
    }
  }
  return (numDigits > 0 && ++counter[0] <= 8) ? end : -1;
}

private static int scanHexSequence ( String address , int index , int end , int [ ] counter ) { char testChar ; int numDigits = 0 ; int start = index ; for ( ; index < end ; ++ index ) { testChar = address . charAt ( index ) ; if ( testChar == ':' ) { if ( numDigits > 0 && ++ counter [ 0 ] > 8 ) { return - 1 ; } if ( numDigits == 0 || ( ( index + 1 < end ) && address . charAt ( index + 1 ) == ':' ) ) { return index ; } numDigits = 0 ; } else if ( ! isHex ( testChar ) ) { if ( testChar == '.' && numDigits < 4 && numDigits > 0 && counter [ 0 ] <= 6 ) { int back = index - numDigits - 1 ; return ( back >= start ) ? back : ( back + 1 ) ; } return - 1 ; } else if ( ++ numDigits > 4 ) { return - 1 ; } } return ( numDigits > 0 && ++ counter [ 0 ] <= 8 ) ? end : - 1 ; }
helper method for iswellformedipv6reference which scans the hex sequences of an ipv6 address . it returns the index of the next character to scan in the address , or - 1 if the string cannot match a valid ipv6 address .
