public static Integer findFree ( int lowIncluse , int highInclusive ) { int low = Math . max ( 1 , Math . min ( lowIncluse , highInclusive ) ) ; int high = Math . min ( 65535 , Math . max ( lowIncluse , highInclusive ) ) ; Integer result = null ; int split = RandomUtils . nextInt ( low , high + 1 ) ; for ( int port = split ; port <= high ; port ++ ) { if ( isFree ( port ) ) { result = port ; break ; } } if ( result == null ) { for ( int port = low ; port < split ; port ++ ) { if ( isFree ( port ) ) { result = port ; break ; } } } return result ; }
Returns a free port in the defined range returns null if none is available .