Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.net.InetAddress;

import java.net.UnknownHostException;

public class Main {
    /**
     * Convert a IPv4 address from an integer to an InetAddress.
     *
     * @param hostAddress an int corresponding to the IPv4 address in network byte order
     * @return {@link InetAddress}
     */
    public static InetAddress intToInetAddress(int hostAddress) {
        byte[] addressBytes = { (byte) (0xff & hostAddress), // SUPPRESS
                // CHECKSTYLE
                (byte) (0xff & (hostAddress >> 8)), // SUPPRESS CHECKSTYLE
                (byte) (0xff & (hostAddress >> 16)), // SUPPRESS CHECKSTYLE
                (byte) (0xff & (hostAddress >> 24)) }; // SUPPRESS CHECKSTYLE

        try {
            return InetAddress.getByAddress(addressBytes);
        } catch (UnknownHostException e) {
            throw new AssertionError();
        }
    }
}