Class Utils

java.lang.Object
eu.europa.esig.dss.utils.Utils

public final class Utils extends Object
Contains common utils
  • Field Details

  • Method Details

    • isStringEmpty

      public static boolean isStringEmpty(String text)
      Checks if the string is empty Ex. "nowina" = false; "" = true NOTE: blank string (e.g. " ") is not empty!
      Parameters:
      text - String
      Returns:
      TRUE if the string is empty, FALSE otherwise
    • isStringNotEmpty

      public static boolean isStringNotEmpty(String text)
      Checks if the string is not blank Ex. "nowina" = true; " " = false
      Parameters:
      text - String
      Returns:
      TRUE if the string is not blank, FALSE otherwise
    • areAllStringsEmpty

      public static boolean areAllStringsEmpty(String... values)
      Checks if all strings are empty
      Parameters:
      values - Strings to check
      Returns:
      TRUE if all strings are empty, FALSE otherwise
    • isAtLeastOneStringNotEmpty

      public static boolean isAtLeastOneStringNotEmpty(String... values)
      Checks if at least one string is not empty
      Parameters:
      values - Strings to check
      Returns:
      TRUE if at least one string is not empty, FALSE otherwise
    • isStringBlank

      public static boolean isStringBlank(String text)
      Checks if the string is blank Ex. "nowina" = false; " " = true
      Parameters:
      text - String
      Returns:
      TRUE if the string is blank, FALSE otherwise
    • isStringNotBlank

      public static boolean isStringNotBlank(String text)
      Checks if the string is not blank Ex. "nowina" = true; " " = false
      Parameters:
      text - String
      Returns:
      TRUE if the string is not blank, FALSE otherwise
    • areStringsEqual

      public static boolean areStringsEqual(String text1, String text2)
      Checks if the strings are equal Ex. "nowina" == "nowina" = true; "nowina" == "Nowina" = false
      Parameters:
      text1 - String
      text2 - String
      Returns:
      TRUE if the strings are equal, FALSE otherwise
    • areStringsEqualIgnoreCase

      public static boolean areStringsEqualIgnoreCase(String text1, String text2)
      Checks if the strings are equal with case ignore Ex. "nowina" == "Nowina" = true; "water" == "fire" = false
      Parameters:
      text1 - String
      text2 - String
      Returns:
      TRUE if the strings are equal with case ignore, FALSE otherwise
    • isStringDigits

      public static boolean isStringDigits(String text)
      Checks if the string contains only digits Ex. "123" = true; "1a2b" = false
      Parameters:
      text - String
      Returns:
      TRUE if the string contains only digits, FALSE otherwise
    • trim

      public static String trim(String text)
      Trims the string, by removing all leading and descending spaces and lines Ex. " 123 " = "123"
      Parameters:
      text - String
      Returns:
      the trimmed string
    • joinStrings

      public static String joinStrings(List<String> strings, String separator)
      Joins the strings with the given separator Ex. "Nowina" "123", "," = "Nowina,123"
      Parameters:
      strings - a collection of Strings
      separator - String
      Returns:
      the joined string
    • substringAfter

      public static String substringAfter(String text, String after)
      Returns a substring after the specified character Ex. "aaaaa?bbb", "?" = "bbb"
      Parameters:
      text - String to get a substring from
      after - String string to get a substring after which
      Returns:
      the substring
    • endsWithIgnoreCase

      public static boolean endsWithIgnoreCase(String text, String expected)
      Checks if the string ends with case ignore Ex. "hello", "LO" = true; "hello", "a" = false
      Parameters:
      text - String to check
      expected - String the end string to check
      Returns:
      TRUE if the string ends with case ignore, FALSE otherwise
    • getFileNameExtension

      public static String getFileNameExtension(String filename)
      This method returns an extension for the given filename Ex. "file.xml" = "xml"; "document.pdf" = "pdf"
      Parameters:
      filename - String representing a filename extracted from a file
      Returns:
      String extension
    • lowerCase

      public static String lowerCase(String text)
      Converts a string to its lower case representation Ex. "Nowina" = "nowina"
      Parameters:
      text - String
      Returns:
      the lower case string
    • upperCase

      public static String upperCase(String text)
      Converts a string to its upper case representation Ex. "Nowina" = "NOWINA"
      Parameters:
      text - String
      Returns:
      the upper case string
    • isArrayEmpty

      public static boolean isArrayEmpty(Object[] array)
      Checks if the array is null or empty
      Parameters:
      array - to check
      Returns:
      TRUE if the array is null or empty, FALSE otherwise
    • isArrayNotEmpty

      public static boolean isArrayNotEmpty(Object[] array)
      Checks if the array is not null nor empty
      Parameters:
      array - to check
      Returns:
      TRUE if the array is not null nor empty, FALSE otherwise
    • isArrayEmpty

      public static boolean isArrayEmpty(byte[] array)
      Checks if the byte array is null or empty
      Parameters:
      array - byte array to check
      Returns:
      TRUE if the byte array is null or empty, FALSE otherwise
    • isArrayNotEmpty

      public static boolean isArrayNotEmpty(byte[] array)
      Checks if the byte array is not null nor empty
      Parameters:
      array - byte array to check
      Returns:
      TRUE if the byte array is not null nor empty, FALSE otherwise
    • isArrayEmpty

      public static boolean isArrayEmpty(char[] array)
      Checks if the char array is null or empty
      Parameters:
      array - char array to check
      Returns:
      TRUE if the char array is null or empty, FALSE otherwise
    • isArrayNotEmpty

      public static boolean isArrayNotEmpty(char[] array)
      Checks if the char array is not null nor empty
      Parameters:
      array - char array to check
      Returns:
      TRUE if the char array is not null nor empty, FALSE otherwise
    • subarray

      public static byte[] subarray(byte[] array, int start, int length)
      Returns a subArray, starting from position start with the defined length length
      Parameters:
      array - byte array to get subArray from
      start - the start index of the subArray
      length - the length of the subArray
      Returns:
      subArray
    • isTrue

      public static boolean isTrue(Boolean bool)
      Checks if the Boolean value is set to true NOTE: if null, returns false!
      Parameters:
      bool - Boolean
      Returns:
      TRUE if the value is set to true, FALSE otherwise
    • isCollectionEmpty

      public static boolean isCollectionEmpty(Collection collection)
      Checks if the collection is null or empty
      Parameters:
      collection - to check
      Returns:
      TRUE if the collection is null or empty, FALSE otherwise
    • isCollectionNotEmpty

      public static boolean isCollectionNotEmpty(Collection collection)
      Checks if the collection is not null nor empty
      Parameters:
      collection - to check
      Returns:
      TRUE if the collection is not null nor empty, FALSE otherwise
    • isMapEmpty

      public static boolean isMapEmpty(Map<?,?> map)
      Checks if the map is null or empty
      Parameters:
      map - to check
      Returns:
      TRUE if the map is null or empty, FALSE otherwise
    • isMapNotEmpty

      public static boolean isMapNotEmpty(Map<?,?> map)
      Checks if the map is not null nor empty
      Parameters:
      map - to check
      Returns:
      TRUE if the map is not null nor empty, FALSE otherwise
    • collectionSize

      public static int collectionSize(Collection collection)
      Gets the size of the collection
      Parameters:
      collection - Collection
      Returns:
      size of the collection
    • isHexEncoded

      public static boolean isHexEncoded(String hexString)
      Checks if the string is HEX (base16) encoded
      Parameters:
      hexString - String
      Returns:
      TRUE if he string is HEX encoded, FALSE otherwise
    • toHex

      public static String toHex(byte[] bytes)
      Transforms the binaries to String HEX representation
      Parameters:
      bytes - byte array
      Returns:
      String HEX-encoded
    • fromHex

      public static byte[] fromHex(String hex)
      Transforms a HEX-encoded string to byte array
      Parameters:
      hex - String hex-encoded
      Returns:
      byte array
    • isBase64Encoded

      public static boolean isBase64Encoded(String base64String)
      Checks if the string is base64-encoded
      Parameters:
      base64String - String
      Returns:
      TRUE if the string is base64-encoded, FALSE otherwise
    • toBase64

      public static String toBase64(byte[] bytes)
      Transforms the binaries to String base64-encoded representation
      Parameters:
      bytes - byte array
      Returns:
      String base64-encoded
    • fromBase64

      public static byte[] fromBase64(String base64)
      Transforms a base64-encoded string to byte array
      Parameters:
      base64 - String base64-encoded
      Returns:
      byte array
    • toByteArray

      public static byte[] toByteArray(InputStream is) throws IOException
      Reads the InputStream and returns the resulting byte array
      Parameters:
      is - InputStream
      Returns:
      byte array
      Throws:
      IOException - if an exception occurs
    • closeQuietly

      public static void closeQuietly(Closeable closeable)
      Closes quietly
      Parameters:
      closeable - Closeable
    • copy

      public static void copy(InputStream is, OutputStream os) throws IOException
      Copies is to os
      Parameters:
      is - InputStream to copy
      os - OutputStream to write the value into
      Throws:
      IOException - if an exception occurs
    • write

      public static void write(byte[] content, OutputStream os) throws IOException
      Writes content to os
      Parameters:
      content - byte array to write
      os - OutputStream to write the value into
      Throws:
      IOException - if an exception occurs
    • getInputStreamSize

      public static long getInputStreamSize(InputStream is) throws IOException
      Gets the size of the InputStream content
      Parameters:
      is - InputStream
      Returns:
      size
      Throws:
      IOException - if an exception occurs
    • compareInputStreams

      public static boolean compareInputStreams(InputStream stream1, InputStream stream2) throws IOException
      Compares content of two InputStreams
      Parameters:
      stream1 - InputStream
      stream2 - InputStream
      Returns:
      TRUE if the content of two InputStream is equal, FALSE otherwise
      Throws:
      IOException - if an exception on InputStream read occurs
    • cleanDirectory

      public static void cleanDirectory(File directory) throws IOException
      Cleans the directory
      Parameters:
      directory - File
      Throws:
      IOException - if an exception occurs
    • listFiles

      public static Collection<File> listFiles(File folder, String[] extensions, boolean recursive)
      Lists all files from the folder with the given extensions
      Parameters:
      folder - File the directory to get files from
      extensions - Strings to get files with
      recursive - defines if the files has to be obtained recursively (from children directories)
      Returns:
      a collection of Files
    • reverseList

      public static <T> List<T> reverseList(List<T> list)
      Creates a reversed copy of the list
      Type Parameters:
      T - the class of list elements
      Parameters:
      list - List to get a reversed copy of
      Returns:
      reversed list
    • containsAny

      public static <T> boolean containsAny(Collection<T> superCollection, Collection<T> subCollection)
      This method returns boolean result whether the superCollection contains any element of the subCollection Ex. {'A', 'B', 'C'}, {'B', 'C', 'D'} = TRUE
      Type Parameters:
      T - object
      Parameters:
      superCollection - first collection
      subCollection - second collection
      Returns:
      TRUE if both collection intersect each other, FALSE otherwise