Java IP Address Convert To convertIpv6Address(String ip)

Here you can find the source of convertIpv6Address(String ip)

Description

Convert an IPv6 address string representation into a sequence of 16 bytes.

License

Open Source License

Parameter

Parameter Description
ip The IP address string to be converted

Return

The array of 16 bytes corresponding to each encoded value, or a null array if the address is invalid in any way.

Declaration

public static byte[] convertIpv6Address(String ip) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   ww  w.  j a va  2  s.c  o  m*/
     * The number of bytes that comprise an IPv6 address
     */
    private static final int IPV6_BYTE_COUNT = 16;
    /**
     * The number of dotted numeric "segments" that comprise an IPv6 address, each segment being 2-bytes
     */
    private static final int IPV6_SEGMENT_COUNT = 8;
    /**
     * The maximum decimal value that can be assigned a single segment of an IPv6 dotted notation address.
     */
    private static final int IPV6_MAX_SEGMENT_VALUE = 65535;
    /**
     * Number of bits per byte
     */
    private static final int BITS_PER_BYTE = 8;
    private static final int BYTE_MASK = 0xFF;

    /**
     * Convert an IPv6 address string representation into a sequence of 16 bytes.
     * 
     * @param ip
     *            The IP address string to be converted
     * @return The array of 16 bytes corresponding to each encoded value, or a null array if the address is invalid in
     *         any way.
     */
    public static byte[] convertIpv6Address(String ip) {
        String temp = expandIpv6Address(ip);
        String[] tokens = temp.split("\\:");
        if (tokens.length != IPV6_SEGMENT_COUNT) {
            return null;
        }
        byte[] bytes = new byte[IPV6_BYTE_COUNT];
        for (int index = 0, byteIndex = 0; index < IPV6_SEGMENT_COUNT; index++, byteIndex += 2) {
            try {
                int value = Integer.parseInt(tokens[index].toLowerCase(), IPV6_BYTE_COUNT);
                if (value < 0 || value > IPV6_MAX_SEGMENT_VALUE) {
                    return null;
                }
                bytes[byteIndex] = (byte) (value >> BITS_PER_BYTE);
                bytes[byteIndex + 1] = (byte) (value & BYTE_MASK);
            } catch (NumberFormatException e) {
                return null;
            }
        }

        return bytes;
    }

    /**
     * <p>
     * Expand an IPv6 address that uses the shorthand zero-fill notation to be the full 16-byte specification. This is
     * because IPv6 addresses can be quite long and tedious to write, so the standard allows for redundant, adjacent
     * zero pairs to be eliminated and indicated by the use of "::".
     * </p>
     * 
     * @param ip
     */
    public static String expandIpv6Address(String ip) {
        if (ip == null || ip.trim().length() == 0) {
            return null;
        }
        StringBuffer buffer = new StringBuffer(ip.trim());

        /*
         * Determine if there is any zero-fill (::) shorthand notation in the address. If there is, we need to expand
         * the address by insertion of zeroes in the address to replace the shorthand notation to fill it.
         */
        int zeroFill = buffer.indexOf("::");
        if (zeroFill != -1) {
            /*
             * Next, count the number of bit groups already in the address (ignore the zero fill, which will be a null
             * token)
             */
            String[] groups = ip.split("\\:");
            int count = 0;
            for (String group : groups) {
                if (group != null && group.trim().length() > 0) {
                    count++;
                }
            }

            /*
             * Compute the count of bit groups to be inserted, if any
             */
            if (count < IPV6_SEGMENT_COUNT) {
                buffer.delete(zeroFill, zeroFill + 2);
                for (; count < IPV6_SEGMENT_COUNT; count++) {
                    buffer.insert(zeroFill, "0:");
                }
                buffer.insert(zeroFill, ":");
            }
            if (buffer.charAt(buffer.length() - 1) == ':') {
                buffer.delete(buffer.length() - 1, buffer.length());
            }
            if (buffer.charAt(0) == ':') {
                buffer.delete(0, 1);
            }
        }

        return buffer.toString();
    }
}

Related

  1. convertIpPortToUniqueId(byte[] quad, int port)
  2. convertIpv4Address(String ip)
  3. convertIpv4AddressToString(byte[] ipv4Address)
  4. convertIPv4NetworkPrefixLength(short length)
  5. convertIpv4ToIpv6(String ip)
  6. convertIPV6Host(String host)