content: /*
   * Copyright (C) 2018 AlexMofer
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package io.github.alexmofer.projectx.utils;
  
  import android.os.StrictMode;
  
  import java.io.IOException;
  import java.net.InetAddress;
  import java.net.InetSocketAddress;
  import java.net.Socket;
  
  /**
   * 工具类
   * Created by Alex on 2017/12/27.
   */
  
  public class Utils {
  
      private Utils() {
          //no instance
      }
  
      private static void bindPort(String host, int port) throws IOException {
          Socket s = new Socket();
          s.bind(new InetSocketAddress(host, port));
          s.close();
      }
  
      /**
       * 检查端口是否可用
       *
       * @param port 端口
       * @return 是否可用
       */
      public static boolean isPortAvailable(int port) {
          boolean available;
          StrictMode.ThreadPolicy defaultPolicy = StrictMode.getThreadPolicy();
          StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
          try {
              bindPort("0.0.0.0", port);
              bindPort(InetAddress.getLocalHost().getHostAddress(), port);
              available = true;
          } catch (IOException e) {
              available = false;
          }
          StrictMode.setThreadPolicy(defaultPolicy);
          return available;
      }
  }
  