Test to see if the given address string represents a literal IPv4 address. - Java Network

Java examples for Network:IP Address

Description

Test to see if the given address string represents a literal IPv4 address.

Demo Code

/**/*w ww.ja  va2s.  co  m*/
 * Copyright (C) 2009 Nortel, certain elements licensed under a Contributor Agreement.
 * Contributors retain copyright to elements licensed under a Contributor Agreement.
 * Licensed to the User under the LGPL license.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String address = "java2s.com";
        System.out.println(isLiteralIPAddress(address));
    }

    /**
     * Test to see if the given address string represents a literal IPv4 address.
     * 
     * @param address
     *           The address string to be tested.
     *           
     * @return
     *           True if literal IPv4 address, False otherwise.
     */
    public static boolean isLiteralIPAddress(String address) {
        String[] octets = address.split("\\.", -1);
        if (octets.length != 4) {
            return false;
        }

        for (int i = 0; i < 4; i++) {
            int octetValue = -1;
            try {
                octetValue = Integer.parseInt(octets[i]);
            } catch (NumberFormatException e) {
                return false;
            }
            if (octetValue < 0 || octetValue > 0xff) {
                return false;
            }
        }

        // All checks passed.
        return true;
    }
}

Related Tutorials