public static boolean available(int port) {
    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
      ss = new ServerSocket(port);
      ss.setReuseAddress(true);
      ds = new DatagramSocket(port);
      ds.setReuseAddress(true);
      return true;
    } catch (IOException e) {
      // Do nothing
    } finally {
      if (ds != null) {
        ds.close();
      }

      if (ss != null) {
        try {
          ss.close();
        } catch (IOException e) {
          /* should not be thrown */
        }
      }
    }

    return false;
  }
Checks to see if a specific port is available.
@param port the port number to check for availability
@return <tt>true</tt> if the port is available, or <tt>false</tt> if not
hbase-testing-util/src/main/java/org/apache/hadoop/hbase/HBaseTestingUtility.java