Class PortRange


  • public class PortRange
    extends Object
    This class manages the port ranges. It keeps track of which ports are used and which ones are open.
    • Constructor Detail

      • PortRange

        protected PortRange()
    • Method Detail

      • getInstance

        public static PortRange getInstance()
        Returns PortRange instance for TCP listening sockets.
        See Also:
        getTcpInstance()
      • getTcpInstance

        public static PortRange getTcpInstance()
        Returns PortRange instance for TCP listening sockets. If the tcp.port.range property is set, the class will be initialized with the specified port ranges.
        Returns:
        PortRange the default instace of this class.
      • getTcpSourceInstance

        public static PortRange getTcpSourceInstance()
        Returns PortRange instance for TCP source sockets. If the tcp.source.port.range property is set, the class will be initialized with the specified port ranges.
        Returns:
        PortRange the default instace of this class.
      • getUdpSourceInstance

        public static PortRange getUdpSourceInstance()
        Returns PortRange instance for UDP source sockets. If the udp.source.port.range property is set, the class will be initialized with the specified port ranges.
        Returns:
        PortRange the default instace of this class.
      • isEnabled

        public boolean isEnabled()
        Checks if the port range is set.
        Returns:
        true if the port range is set, false otherwise.
      • getFreePort

        public int getFreePort​(int lastPortNumber)
                        throws IOException
        Returns first available port.
        Parameters:
        lastPortNumber - port number to start finding the next available port from. Set it to 0 if called initialy.
        Returns:
        the next available port number from the lastPortNumber.
        Throws:
        IOException - if there is no more free ports available or if the lastPortNumber is incorrect.
      • setUsed

        public void setUsed​(int portNumber)
        Sets the port number as used.
        Parameters:
        portNumber - port number
      • free

        public void free​(int portNumber)
        Releases or frees the port number. (Mark it as unused)
        Parameters:
        portNumber - port number
      • isInRange

        public boolean isInRange​(int portNumber)
        Checks if the port number if within port range. Does not check if it is a free, but only checks if it is within said range.