Java InetAddress compareAddresses(InetAddress one, InetAddress two)

Here you can find the source of compareAddresses(InetAddress one, InetAddress two)

Description

compareTo for InetAddresses

License

Apache License

Declaration

public static int compareAddresses(InetAddress one, InetAddress two) 

Method Source Code


//package com.java2s;
/*/*from  ww  w  . ja v  a2 s. c o m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.InetAddress;

public class Main {
    /** compareTo for InetAddresses */
    public static int compareAddresses(InetAddress one, InetAddress two) {
        byte[] oneBytes = one.getAddress();
        byte[] twoBytes = two.getAddress();

        if (oneBytes != twoBytes) {
            for (int i = 0; i < oneBytes.length; i++) {
                if (i >= twoBytes.length)
                    return -1; // same as far as they go, but shorter...
                if (oneBytes[i] < twoBytes[i])
                    return -1;
                if (oneBytes[i] > twoBytes[i])
                    return 1;
            }
            if (oneBytes.length > twoBytes.length)
                return 1; // same as far as they go, but longer...
        }
        return 0;
    }
}

Related

  1. canonicalIpAddress(InetAddress addr)
  2. checkConnection(InetAddress address, int port, int timeout)
  3. compareInetAddresses(InetAddress a, InetAddress b)
  4. compareInetAddresses(InetAddress a1, InetAddress a2)
  5. CompareIP(InetAddress ip1, InetAddress ip2)
  6. contains(InetAddress network, InetAddress netmask, InetAddress ip)