Searching...
Friday, 10 May 2013

Different representation of IPV4 in Java

11:22

IPV4 Different representation

Dotted decimal             192.0.2.235     
Dotted hexadecimal 0xC0.0x00.0x02.0xEB
Dotted octal[1] 0300.0000.0002.0353
Hexadecimal 0xC00002EB
Decimal 3221226219
Octal 030000001353

All the above representations are for ip address 192.0.2.235. Below java program normalize each representations and printing the output.

Example

package com.pretech;

import java.net.URL;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

public class Ipv4Example {
private static final Pattern IPV4REGEX = Pattern
.compile("" + "\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.)" +
"{3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");

public static void main(String[] args) {
try {
System.out.println(createHostAddress("http://192.0.2.235"));
System.out.println(createHostAddress("http://0xC0.0x00.0x02.0xEB"));
System.out.println(createHostAddress("http://0300.0000.0002.0353"));
System.out.println(createHostAddress("http://0xC00002EB"));
System.out.println(createHostAddress("http://030000001353"));
} catch (Exception e) {
e.printStackTrace();
}
}

private static String createHostAddress(final String url) throws Exception {
URL urldetails = new URL(url);
String addr = urldetails.getHost();
boolean validHost = true;
StringBuffer hostStringBuffer = new StringBuffer();
try {
StringTokenizer hostTokenizer = new StringTokenizer(addr, ".");
int tokenCount = hostTokenizer.countTokens();
// In the case of non dotted hex,octals
if (isNumber(addr) && tokenCount == 1) {
long decimalIpAddress = Long.decode(addr);
hostStringBuffer.append(longToIpAddress(decimalIpAddress));
if (!IPV4REGEX.matcher(hostStringBuffer.toString()).matches()) {
validHost = false;
}
// In the case of dotted ip address
} else if (isNumber(addr) && tokenCount > 1) {
int i = 0;
while (hostTokenizer.hasMoreTokens()) {
String token = hostTokenizer.nextToken();
hostStringBuffer.append(Integer.toString(Integer.decode(token)));
if (i < 3) {
hostStringBuffer.append(".");
}
i = i + 1;
}
if (!IPV4REGEX.matcher(hostStringBuffer.toString()).matches()) {
validHost = false;
}
} else {
// Other host names
hostStringBuffer.append(addr);
}
// Check the host name is valid or not.
if (!validHost) {
throw new Exception("Invalid Host name");
}
} catch (Exception e) {
throw new Exception("Invalid Host name" + e.getMessage(), e);
}
return hostStringBuffer.toString();
}

/**
    * This Method will convert long value to ip address format.
    *
    *@param ip
    *@return
    */
public static String longToIpAddress(long ipAddress) {
StringBuilder ipStringBuffer = new StringBuilder();
for (int i = 0; i < 4; i++) {
ipStringBuffer.insert(0, Long.toString(ipAddress & 0xff));
if (i < 3) {
ipStringBuffer.insert(0, '.');
}
ipAddress >>= 8;
}
return ipStringBuffer.toString();
}

/**
    *@param addr
    *@return
    */
private static boolean isNumber(final String addr) {
boolean returnValue = false;
try {
StringTokenizer addrTokenizer = new StringTokenizer(addr, ".");
while (addrTokenizer.hasMoreTokens()) {
String token = addrTokenizer.nextToken();
Long.decode(token);
return true;
}
} catch (Exception e) {
returnValue = false;
return returnValue;
}
return returnValue;
}
}
 
Output
192.0.2.235
192.0.2.235
192.0.2.235
192.0.2.235
192.0.2.235

0 comments: