Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package io.niowire.inspection;
 
This class generates a Uid from an IP/Port combination

Author(s):
Trent Houliston
public class UidGenerator
{

This method builds a UID from a InetSocketAddress

Parameters:
remoteAddress the remote address to build a UID for
Returns:
a String containing the hex representation of the address and port
	public static String addressToUid(InetSocketAddress remoteAddress)
	{
		//Split up the address and call the other method
		return addressToUid(remoteAddress.getAddress(), remoteAddress.getPort());
	}

This method converts an IP and port into a UID

Parameters:
address the address to convert
port the port to convert
Returns:
a String containing the hex representation of the address and port
	public static String addressToUid(InetAddress addressint port)
	{
		return addressToUid(address.getAddress(), port);
	}

This method converts an IP and port into a UID

Parameters:
address the address to convert as a string
port the port to convert
Returns:
a String containing the hex representation of the address and port
Throws:
java.net.UnknownHostException if the passed address is unknown
	public static String addressToUid(String addressint portthrows UnknownHostException
	{
		return addressToUid(InetAddress.getByName(address), port);
	}

This method converts an IP and port into a UID

Parameters:
address the the address to convert in byte form
port the port to convert
Returns:
a String containing the hex representation of the address and port
	public static String addressToUid(byte[] addressint port)
	{
		//Get a new string builder
		StringBuilder str = new StringBuilder();
		//Loop through our bytes (will be a differnt number for IPv4 vs IPV6
		for (byte b : address)
		{
			//Append each byte as hex
			str.append(String.format("%02x"b));
		}
		//Append the port as hex
		str.append(String.format("%04x"port));
		//Return the value
	}

Private constructor as we are a Utility class
	private UidGenerator()
	{
	}
New to GrepCode? Check out our FAQ X