com.springsource.util.io
Class NetUtils

java.lang.Object
  extended by com.springsource.util.io.NetUtils

public final class NetUtils
extends java.lang.Object

Utility methods for working with network IO code.

Concurrent Semantics
Threadsafe.

Since:
1.0
Author:
Rob Harrop, Ben Hale

Field Summary
private static int MAX_PORT
           
private static int MIN_SAFE_PORT
           
private static java.util.Random random
           
 
Constructor Summary
NetUtils()
           
 
Method Summary
static int getFreePort()
          Gets a random free port in the non-privileged range of 1025-65535.
static int getFreePort(int minPort, int maxPort)
          Gets a random free port in between the minimum and maximum specified port numbers.
private static int getRandomPort(int minPort, int portRange)
           
static boolean isPortAvailable(int port)
          Checks whether the supplied port is available on any local address.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIN_SAFE_PORT

private static final int MIN_SAFE_PORT
See Also:
Constant Field Values

MAX_PORT

private static final int MAX_PORT
See Also:
Constant Field Values

random

private static final java.util.Random random
Constructor Detail

NetUtils

public NetUtils()
Method Detail

isPortAvailable

public static boolean isPortAvailable(int port)
Checks whether the supplied port is available on any local address.

Parameters:
port - the port to check for.
Returns:
true if the port is available, otherwise false.

getFreePort

public static int getFreePort()
Gets a random free port in the non-privileged range of 1025-65535. After this port has been returned once, it cannot be returned again.

Returns:
A free port number

getFreePort

public static int getFreePort(int minPort,
                              int maxPort)
Gets a random free port in between the minimum and maximum specified port numbers.

Parameters:
minPort - The minimum port number
maxPort - The maximum port number
Returns:
After this number has been returned once, it cannot be returned again.

getRandomPort

private static int getRandomPort(int minPort,
                                 int portRange)