Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Compress Ipv6 Addresses

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Compress Ipv6 Addresses

    Hi all,

    How to compress Ipv6 addresses? Are there any existing API's in Java which offer the functionality?

    For example the compressed equivalent of the Ipv6 address 3ffe:0000:0000:0000:0000:0000:0000:0001
    is 3ffe::1

    Regards,
    Last edited by abhay8nitt; May 19th, 2010 at 11:46 AM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Compress Ipv6 Addresses

    I took the liberty of studying the RFC for IPv6 and then I created this method.

        /**
         * This method takes an IPv6 address and compresses it according to the RFC (http://www.ietf.org/rfc/rfc2373.txt).
         *
         * @param uncompressed the uncompressed IPv6 address
         * @return a compressed IPv6 address
         */
        public static String compressIpv6(final String uncompressed) {
            if (uncompressed == null) {
                return null;
            }
     
            if (uncompressed.contains("::")) {
                return uncompressed;
            }
     
            if (uncompressed.equals("0:0:0:0:0:0:0:0")) {
                return "::";
            }
     
            int start = -1;
            int end = -1;
     
            for (int i = 0; i < uncompressed.length(); ++i) {
                final char character = uncompressed.charAt(i);
     
                if (start == -1) {
                    if (character == '0' && i > 0 && uncompressed.charAt(i - 1) == ':') {
                        start = i - 1;
                    } else if (character == '0' && i == 0) {
                        start = 0;
                    }
                } else {
                    if (character != '0' && character != ':') {
                        if (uncompressed.charAt(i - 1) == ':') {
                            end = i;
                        }
     
                        break;
                    }
                }
            }
     
            if (start > -1 && end > start) {
                return uncompressed.substring(0, start) + "::" + uncompressed.substring(end, uncompressed.length());
            }
     
            return uncompressed;
        }

    It might be flawed but I tested it with the following addresses:

    FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
    3ffe:0000:0000:0000:0000:0000:0000:0001
    1080:0:0:0:8:800:200C:417A
    FF01:0:0:0:0:0:0:101
    0:0:0:0:0:0:0:1
    0:0:0:0:0:0:0:0

    Feel free to use it if you dare

    // Json
    Last edited by Json; May 19th, 2010 at 10:41 AM.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compress Ipv6 Addresses

    Thanks Json,
    But unfortunately the code doesn't work fine for lot of cases. I have written the code. It works fine for all cases. For example
    "2001:0:000:00:1:0000:1:2",
    "2001:000:1:000:1:0:1:1"

    it fails.

    Thanks,

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Compress Ipv6 Addresses

    Could you explain, when you say fail, do you mean that you get an exception or that the output is simply not what you expect it to be?

    Could you also give me a few examples of some addresses you say are not working, what they are uncompressed and what you expect to see once compressed?

    // Json

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compress Ipv6 Addresses

    I am sorry to say 'fail' , I meant that the compression is not proper for e.g.
    2001:0:000:00:1:0000:1:2-Compressed form should be 2001::1:0:2
    "2001:000:1:000:1:0:1:1 -Compressed form should be 2001:0:1:0:1:0:1:1

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Compress Ipv6 Addresses

    I see so all :0000: which are not affected by the :: should still be truncated down to :0:

    Let's see if we can fix that

    // Json

Similar Threads

  1. IPv6 validation
    By subhvi in forum Java Networking
    Replies: 1
    Last Post: November 27th, 2009, 07:19 AM