package com.filestack.internal;

import com.filestack.HttpException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okio.Buffer;
import retrofit2.Response;

/**
 * Small helper functions that don't need their own class.
 */
public class Util {

  /**
   * Loads version string from properties file in resources folder.
   *
   * @return Version string
   */
  public static String getVersion() {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    InputStream inputStream = loader.getResourceAsStream("com/filestack/version.properties");
    Properties prop = new Properties();
    String version;

    try {
      prop.load(inputStream);
      version = prop.getProperty("version");
    } catch (IOException e) {
      version = "x.y.z";
    }

    return version;
  }

  /**
   * Creates {@link RequestBody Request Body} from String.
   * For multipart form uploads.
   */
  public static RequestBody createStringPart(String content) {
    return RequestBody.create(MultipartBody.FORM, content);
  }

  /**
   * Converts {@link RequestBody} to String.
   */
  public static String partToString(RequestBody body) throws IOException {
    Buffer buffer = new Buffer();
    body.writeTo(buffer);
    return buffer.readUtf8();
  }

  /**
   * Throws an {@link HttpException} with the code and error body from a {@link Response}.
   *
   * @param response response from a backend call
   * @throws HttpException always unless error reading response body
   * @throws IOException           on error reading response body
   */
  public static void throwHttpResponseException(Response response) throws IOException {
    ResponseBody errorBody = response.errorBody();
    if (errorBody != null) {
      throw new HttpException(response.code(), errorBody.string());
    } else {
      throw new HttpException(response.code());
    }
  }

  /**
   * Checks status of backend responses.
   * Throws a {@link HttpException} if response isn't in 200 range.
   *
   * @param response response from a backend call
   * @throws HttpException on response code not in 200 range
   * @throws IOException           on error reading response body
   */
  public static void checkResponseAndThrow(Response response) throws IOException {
    if (response.isSuccessful()) {
      return;
    }
    throwHttpResponseException(response);
  }

  /**
   * Creates and validates a new {@link File} for writing.
   *
   * @param pathname path to file
   * @return file pointing to pathname
   * @throws FileNotFoundException if path isn't usable
   * @throws IOException           if a file can't be created
   */
  public static File createWriteFile(String pathname) throws IOException {
    File file = new File(pathname);

    if (!file.createNewFile()) {
      if (file.isDirectory()) {
        throw new FileNotFoundException("Can't write to directory: " + file.getPath());
      } else if (!file.isFile()) {
        throw new FileNotFoundException("Can't write to special file: " + file.getPath());
      } else if (!file.canWrite()) {
        throw new FileNotFoundException("No write access: " + file.getAbsolutePath());
      }
    }

    return file;
  }

  /**
   * Creates and validates a new {@link File} for reading.
   *
   * @param pathname path to file
   * @return file pointing to pathname
   * @throws FileNotFoundException if path doesn't exist or isn't usable
   */
  public static File createReadFile(String pathname) throws IOException {
    File file = new File(pathname);

    if (!file.exists()) {
      throw new FileNotFoundException(file.getPath());
    } else if (file.isDirectory()) {
      throw new FileNotFoundException("Can't read from directory: " + file.getPath());
    } else if (!file.isFile()) {
      throw new FileNotFoundException("Can't read from special file: " + file.getPath());
    }

    return file;
  }

  public static boolean isUnitTest() {
    String env = System.getenv("TEST_TYPE");
    return env != null && env.equals("unit");
  }

  public static boolean isNullOrEmpty(String value) {
    return value == null || value.equals("");
  }

  public static void throwIfNullOrEmpty(String value, String message) {
    if (isNullOrEmpty(value)) {
      throw new IllegalArgumentException(message);
    }
  }
}

@@ -4164,32 +4164,75 @@
 s(%22unit%22);%0A  %7D%0A%0A
+  /** Check if String is null or empty. */%0A
   public static 
@@ -4316,24 +4316,134 @@
 s(%22%22);%0A  %7D%0A%0A
+  /**%0A   * Throw an %7B@link java.lang.IllegalArgumentException%7D with message if String is mull or empty.%0A   */%0A
   public sta

fix checkstyle warning