Java InetAddress inetAddressCompare(final InetAddress address1, final InetAddress address2)

Here you can find the source of inetAddressCompare(final InetAddress address1, final InetAddress address2)

Description

inet Address Compare

License

LGPL

Declaration

public static int inetAddressCompare(final InetAddress address1, final InetAddress address2) 

Method Source Code

//package com.java2s;
/*/*  w  ww  .ja  v  a2s.  c  o m*/
 * Cacheonix Systems licenses this file to You under the LGPL 2.1
 * (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.cacheonix.org/products/cacheonix/license-lgpl-2.1.htm
 *
 * 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 {
    public static int inetAddressCompare(final InetAddress address1, final InetAddress address2) {

        if (address1 == null || address2 == null) {
            return -1;
        }

        if (address1.equals(address2)) {
            return 0;
        }

        final byte[] ab1 = address1.getAddress();
        final byte[] ab2 = address2.getAddress();

        if (ab1.length > ab2.length) {
            return 1;
        }

        if (ab1.length < ab1.length) {
            return -1;
        }

        for (int i = 0; i < ab1.length; i++) {
            final byte b1 = ab1[i];
            final byte b2 = ab2[i];
            if (b1 > b2) {
                return 1;
            }
            if (b1 < b2) {
                return -1;
            }
        }
        return 0;
    }
}

Related

  1. formatHostAddress(final InetAddress localHost)
  2. greaterThan(InetAddress inetAddress1, InetAddress inetAddress2)
  3. hasIp(InetAddress ip)
  4. hasLocalScope(InetAddress addr)
  5. increment(InetAddress address)
  6. inetAddressesComparator(final boolean sameHost)
  7. inetAddressesCompare(final InetAddress[] addresses1, final InetAddress[] addresses2)
  8. inetAddressFromThriftString(String ipAddress)
  9. inetAddressGt(InetAddress a, InetAddress b)