集册 Java实例教程 给定一个声明的InetAddress和一个结束的InetAddress计算一个InetAddress实例,该实例将用作isOnNetwork()方法的网络。

给定一个声明的InetAddress和一个结束的InetAddress计算一个InetAddress实例,该实例将用作isOnNetwork()方法的网络。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

473
给定一个声明的InetAddress和一个结束的InetAddress计算一个InetAddress实例,该实例将用作isOnNetwork()方法的网络。
//来 自 N o w J a v a . c o m - 时  代  Java

//package com.nowjava;

import java.net.InetAddress;


public class Main {

    /** Masks used to get bit values in a byte. */

    private static final byte[] BYTE_MASK = { 0x7f, 0x3f, 0x1f, 0x0f, 0x07,

            0x03, 0x01, 0x00 };


    /**

     * Given a stating InetAddress and an ending InetAddress compute an 

     * InetAddress instance that will be used as the network for the 

     * isOnNetwork() method.

     *

     * @param start Starting address.

     * @param end Ending address.

     * @return Address of the network containing start and end, null if

     * there was an error.

     */

    public static InetAddress getNetwork(InetAddress start, InetAddress end) {


        byte[] mask = getSubnetMask(start, end);

        byte[] startAddr = start.getAddress();

        byte[] networkAddr = new byte[mask.length];


        for (int i = 0; i < networkAddr.length; i++) {


            networkAddr[i] = (byte) (startAddr[i] & mask[i]);
            /** from 
            时 代 J     a    v  a - nowjava.com**/

        }


        InetAddress result = null;

        try {

            result = InetAddress.getByAddress(networkAddr);

        } catch (Exception e) {

            result = null;

        }

        return result;

    }


    /**

     * Given a starting InetAddress and an ending InetAddress compute a 

     * raw subnet mask that will be used as the mask for the isOnNetwork()

     * method.

     * 

     * We use the following heuristic approach to calculating the subnet mask:

     * If the bits of the addresses are the same (start[0] = 0 and end[0] = 0; 

     * likewise start[0] = 1 and end[0] = 1), then the corresponding bit

     * of the subnet mask is 1; otherwise the corresponding bit of the subnet

     * mask is 0.  We evaluate each bit of the start and end addresses until

     * we find one that is different.  After this point, the rest of the 

     * subnet mask is 0 (represents the host part of the subnet mask).

     *

     * @param start Starting address.

     * @param end Ending address.

     * @return Raw subnet address.

     */

    public static byte[] getSubnetMask(InetAddress start, InetAddress end) {


        byte[] startAddr = start.getAddress();

        byte[] endAddr = end.getAddress();

        byte[] maskAddr = new byte[startAddr.length];


        for (int i = 0; i < maskAddr.length; i++) {


            for (int j = 0; j < BYTE_MASK.length; j++) {


                /*

                 * Flip the bits of the mask (so instead of 0111, we have 1000)

                 * to make the logic easier to follow.

                 */

                byte byteMask = (byte) ~BYTE_MASK[j];


       
展开阅读全文