package org.folio.rest.tools.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.URL;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author shale
 *
 */
public class NetworkUtils {

  @SuppressWarnings("java:S2245")//Suppress "Weak Cryptography" warning, ThreadLocalRandom is good enough for finding a free port
  public static int nextFreePort() {
    int maxTries = 10000;
    int port = ThreadLocalRandom.current().nextInt(49152 , 65535);
    while (true) {
        if (isLocalPortFree(port)) {
            return port;
        } else {
            port = ThreadLocalRandom.current().nextInt(49152 , 65535);
        }
        maxTries--;
        if(maxTries == 0){
          return 8081;
        }
    }
  }

  /**
   * Check a local TCP port.
   * @param port  the TCP port number, must be from 1 ... 65535
   * @return true if the port is free (unused), false if the port is already in use
   */
  public static boolean isLocalPortFree(int port) {
      try {
          new ServerSocket(port).close();
          return true;
      } catch (IOException e) {
          return false;
      }
  }

  public static byte[] object2Bytes(Object obj) throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
      out = new ObjectOutputStream(bos);
      out.writeObject(obj);
      byte[] bytes = bos.toByteArray();
      return bytes;
    } finally {
      try {
        if (out != null) { out.close(); }
      } catch (IOException ex) {/*ignore*/}
      try {
        bos.close();
      } catch (IOException ex) {/*ignore*/}
    }
  }

  public static String readURL(String path) throws Exception {
    URL url = new URL(path);
    BufferedReader in = null;
    StringBuffer content = new StringBuffer();
    String input;
    try {
      in = new BufferedReader(new InputStreamReader(url.openStream()));
      while ((input = in.readLine()) != null)
        content.append(input);
      return content.toString();
    } finally {
      if(in != null){
        in.close();
      }
    }
  }

  public static boolean isValidURL(String path){
    try {
      new URL(path);
      return true;
    }
    catch(Exception e){
      return false;
    }
  }

}

@@ -49,186 +49,19 @@
 .io.
-BufferedReader;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader;%0Aimport java.io.ObjectOutput;%0Aimport java.io.ObjectOutputStream
+IOException
 ;%0Aim
@@ -92,29 +92,8 @@
 et;%0A
-import java.net.URL;%0A
 impo
@@ -1093,24 +1093,24 @@
 turn false;%0A
+
       %7D%0A  %7D%0A
@@ -1114,1120 +1114,6 @@
  %7D%0A%0A
-  public static byte%5B%5D object2Bytes(Object obj) throws Exception %7B%0A    ByteArrayOutputStream bos = new ByteArrayOutputStream();%0A    ObjectOutput out = null;%0A    try %7B%0A      out = new ObjectOutputStream(bos);%0A      out.writeObject(obj);%0A      byte%5B%5D bytes = bos.toByteArray();%0A      return bytes;%0A    %7D finally %7B%0A      try %7B%0A        if (out != null) %7B out.close(); %7D%0A      %7D catch (IOException ex) %7B/*ignore*/%7D%0A      try %7B%0A        bos.close();%0A      %7D catch (IOException ex) %7B/*ignore*/%7D%0A    %7D%0A  %7D%0A%0A  public static String readURL(String path) throws Exception %7B%0A    URL url = new URL(path);%0A    BufferedReader in = null;%0A    StringBuffer content = new StringBuffer();%0A    String input;%0A    try %7B%0A      in = new BufferedReader(new InputStreamReader(url.openStream()));%0A      while ((input = in.readLine()) != null)%0A        content.append(input);%0A      return content.toString();%0A    %7D finally %7B%0A      if(in != null)%7B%0A        in.close();%0A      %7D%0A    %7D%0A  %7D%0A%0A  public static boolean isValidURL(String path)%7B%0A    try %7B%0A      new URL(path);%0A      return true;%0A    %7D%0A    catch(Exception e)%7B%0A      return false;%0A    %7D%0A  %7D%0A%0A
 %7D%0A

Remove unused object2Bytes, readURL, isValidURL in NetworkUtils