Package org.kiwiproject.net
Class LocalPortChecker
java.lang.Object
org.kiwiproject.net.LocalPortChecker
Utility to check port availability and find open ports.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfindFirstOpenPortAbove(int port) Find the first open port above the given port (i.e. if port is 1024 then the first port to be checked will be 1025).findFirstOpenPortFrom(int port) Find the first open port starting at the given port inclusive, i.e. ifportis 1024 then it is the first port checked.Find a random open port.findRandomOpenPortAbove(int port) Find a random open port starting above the given port.findRandomOpenPortFrom(int port) Find a random open port starting at the given port.booleanisPortAvailable(int port) Check port availability.
-
Field Details
-
MAX_PORT
public static final int MAX_PORT- See Also:
-
-
Constructor Details
-
LocalPortChecker
public LocalPortChecker()
-
-
Method Details
-
isPortAvailable
public boolean isPortAvailable(int port) Check port availability. Note specifically that zero is not a valid port for the reasons described here and here.- Parameters:
port- the port to check on the local machine- Returns:
trueif the port is available;falseotherwise- Throws:
IllegalArgumentException- if port is not valid
-
findFirstOpenPortFrom
Find the first open port starting at the given port inclusive, i.e. ifportis 1024 then it is the first port checked. If no available port is found, return an emptyOptionalInt.- Parameters:
port- the port to start from- Returns:
- an optional containing the first open port, or an empty optional
- Throws:
IllegalArgumentException- if port is not between 1 andMAX_PORT- Implementation Note:
- Since this method looks for availability of a specific port, zero is invalid because it is a reserved port used to indicate a random port and is never bound directly to.
-
findFirstOpenPortAbove
Find the first open port above the given port (i.e. if port is 1024 then the first port to be checked will be 1025). If no available port is found, return an emptyOptionalInt.- Parameters:
port- the port to check above- Returns:
- an optional containing the first open port, or an empty optional
- Throws:
IllegalArgumentException- if port is not between 0 and (MAX_PORT- 1)
-
findRandomOpenPort
Find a random open port. If no available port is found, return an emptyOptionalInt.- Returns:
- an optional containing the first random port found, or an empty optional
-
findRandomOpenPortFrom
Find a random open port starting at the given port. If no available port is found, return an emptyOptionalInt.- Parameters:
port- the port to start from (inclusive)- Returns:
- an optional containing the first open port, or an empty optional
- Throws:
IllegalArgumentException- if port is not between 1 andMAX_PORT- Implementation Note:
- Since this method looks for availability of a specific port, zero is invalid because it is a reserved port used to indicate a random port and is never bound directly to.
-
findRandomOpenPortAbove
Find a random open port starting above the given port. If no available port is found, return an emptyOptionalInt.- Parameters:
port- the port to start from (exclusive)- Returns:
- an optional containing the first open port, or an empty optional
- Throws:
IllegalArgumentException- if port is not between 0 and (MAX_PORT- 1)- Implementation Note:
- Makes up to 100 attempts to find an open port before giving up.
-