Class PortRange


  • public class PortRange
    extends java.lang.Object
    This class manages the port ranges. It keeps track of which ports are used and which ones are open.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static byte UNUSED  
      protected static byte USED  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected PortRange()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void free​(int portNumber)
      Releases or frees the port number.
      int getFreePort​(int lastPortNumber)
      Returns first available port.
      static PortRange getInstance()
      Returns PortRange instance for TCP listening sockets.
      static PortRange getTcpInstance()
      Returns PortRange instance for TCP listening sockets.
      static PortRange getTcpSourceInstance()
      Returns PortRange instance for TCP source sockets.
      static PortRange getUdpSourceInstance()
      Returns PortRange instance for UDP source sockets.
      boolean isEnabled()
      Checks if the port range is set.
      boolean isInRange​(int portNumber)
      Checks if the port number if within port range.
      void setUsed​(int portNumber)
      Sets the port number as used.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 java.io.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:
        java.io.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.