/** * Formats input address. For IPV4 returns simply host address, for IPV6 formats address according to <a * href="http://tools.ietf.org/html/rfc5952">RFC5952</a> rules. It does not embed IPV6 address in '[', ']', since those are part of IPV6 URI literal. * * @param inet * @return */ public static String formatAddress(InetAddress inet){ if(inet == null){ throw new IllegalArgumentException(); } if(inet instanceof Inet4Address){ return inet.getHostAddress(); } else if (inet instanceof Inet6Address){ byte[] byteRepresentation = inet.getAddress(); int[] hexRepresentation = new int[IPV6_LEN]; for(int i=0;i < hexRepresentation.length;i++){ hexRepresentation[i] = ( byteRepresentation[2*i] & 0xFF) << 8 | ( byteRepresentation[2*i+1] & 0xFF ); } compactLongestZeroSequence(hexRepresentation); return formatAddress6(hexRepresentation); } else { return inet.getHostAddress(); } }
/** * Formats input address. For IPV4 returns simply host address, for IPV6 formats address according to <a * href="http://tools.ietf.org/html/rfc5952">RFC5952</a> rules. It does not embed IPV6 address in '[', ']', since those are part of IPV6 URI literal. * * @param inet * @return */ public static String formatAddress(InetAddress inet){ if(inet == null){ throw new NullPointerException(); } if(inet instanceof Inet4Address){ return inet.getHostAddress(); } else if (inet instanceof Inet6Address){ byte[] byteRepresentation = inet.getAddress(); int[] hexRepresentation = new int[IPV6_LEN]; for(int i=0;i < hexRepresentation.length;i++){ hexRepresentation[i] = ( byteRepresentation[2*i] & 0xFF) << 8 | ( byteRepresentation[2*i+1] & 0xFF ); } compactLongestZeroSequence(hexRepresentation); return formatAddress6(hexRepresentation); } else { return inet.getHostAddress(); } }
/** * Formats input address. For IPV4 returns simply host address, for IPV6 formats address according to <a * href="http://tools.ietf.org/html/rfc5952">RFC5952</a> rules. It does not embed IPV6 address in '[', ']', since those are part of IPV6 URI literal. * * @param inet * @return */ public static String formatAddress(InetAddress inet){ if(inet == null){ throw new NullPointerException(); } if(inet instanceof Inet4Address){ return inet.getHostAddress(); } else if (inet instanceof Inet6Address){ byte[] byteRepresentation = inet.getAddress(); int[] hexRepresentation = new int[IPV6_LEN]; for(int i=0;i < hexRepresentation.length;i++){ hexRepresentation[i] = ( byteRepresentation[2*i] & 0xFF) << 8 | ( byteRepresentation[2*i+1] & 0xFF ); } compactLongestZeroSequence(hexRepresentation); return formatAddress6(hexRepresentation); } else { return inet.getHostAddress(); } }