Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
    /**
     * Calculates the network mask address from the value
     * 
     * @param numericMask
     *            the network mask
     * @return
     */
    public static String networkMaskFromInt(int numericMask) {
        //        StringBuffer sb = new StringBuffer(15);
        //        for (int shift = 24; shift > 0; shift -= 8) {
        //            // process 3 bytes, from high order byte down.
        //            sb.append(Integer.toString((numericMask >>> shift) & 0xff));
        //            sb.append('.');
        //        }
        //        sb.append(Integer.toString(numericMask & 0xff));
        //        return sb.toString();
        //    }
        //    
        //    public String getMaskAsString() {
        int value = 0xffffffff << (32 - numericMask);
        byte[] bytes = new byte[] { (byte) (value >>> 24), (byte) (value >> 16 & 0xff), (byte) (value >> 8 & 0xff),
                (byte) (value & 0xff) };

        InetAddress netAddr = null;
        try {
            netAddr = InetAddress.getByAddress(bytes);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return netAddr.getHostAddress();
    }
}