Class Format

java.lang.Object
ecmwf.common.text.Format

public final class Format extends Object
The Class Format.
  • Method Details

    • unix2sqlWildcards

      public static String unix2sqlWildcards(String unix)
      Convert unix 2 sql wildcards and also escape the special sql characters '%' and '_'.
      Parameters:
      unix - the unix
      Returns:
      the string
    • isValidEmail

      public static boolean isValidEmail(String email)
      Check if this is a valid email.
      Parameters:
      email - the email
      Returns:
      valid
    • isValidId

      public static boolean isValidId(String id, String allowed)
      Check if this is a valid identifier (with letters, digits and allowed characters).
      Parameters:
      id - the identifier
      allowed - the allowed
      Returns:
      valid
    • escapeHTML

      public static String escapeHTML(String s)
      Escape html.
      Parameters:
      s - the s
      Returns:
      the string
    • formatSize

      public static String formatSize(long bytes)
      Format size.
      Parameters:
      bytes - the bytes
      Returns:
      the string
    • toMBits

      public static double toMBits(long bytes)
      Convert Bytes in Mbits.
      Parameters:
      bytes - the bytes
      Returns:
      the string
    • formatTime

      public static String formatTime(Timestamp date)
      Format time.
      Parameters:
      date - the date
      Returns:
      the string
    • formatTime

      public static String formatTime(long date)
      Format time.
      Parameters:
      date - the date
      Returns:
      the string
    • formatCurrentTime

      public static String formatCurrentTime()
      Format current time.
      Returns:
      the string
    • formatTime

      public static String formatTime(String format, long date)
      Format time.
      Parameters:
      format - the format
      date - the date
      Returns:
      the string
    • formatDuration

      public static String formatDuration(long start, long stop)
      Format duration.
      Parameters:
      start - the start
      stop - the stop
      Returns:
      the string
    • toList

      public static <E> String toList(Vector<E> vector)
      Converts into list.
      Type Parameters:
      E - the element type
      Parameters:
      vector - the vector
      Returns:
      the string
    • toList

      public static <E> String toList(List<E> list)
      Converts into list.
      Type Parameters:
      E - the element type
      Parameters:
      list - the vector
      Returns:
      the string
    • formatDuration

      public static String formatDuration(long duration)
      Format duration.
      Parameters:
      duration - duration in milliseconds
      Returns:
      formatted string, e.g., "1h 5m 30s" or "250ms"
    • formatBoolean

      public static String formatBoolean(boolean bool)
      Format boolean.
      Parameters:
      bool - the bool
      Returns:
      the string
    • formatLong

      public static String formatLong(long n, int width)
      Format long.
      Parameters:
      n - the n
      width - the width
      Returns:
      the string
    • formatLong

      public static String formatLong(long n, int width, boolean zero)
      Format long.
      Parameters:
      n - the n
      width - the width
      zero - the zero
      Returns:
      the string
    • formatRate

      public static String formatRate(long size, long duration)
      Format the transfer rate in GBytes/s or MBytes/s or KBytes/s or Bytes/s depending of the size.
      Parameters:
      size - the size
      duration - the duration
      Returns:
      the string
    • getMBitsPerSeconds

      public static double getMBitsPerSeconds(long size, long duration)
      Get the transfer rate in Mbits/s.
      Parameters:
      size - the size
      duration - the duration
      Returns:
      the string
    • formatPercentage

      public static String formatPercentage(long size, long sent)
      Format percentage.
      Parameters:
      size - the size
      sent - the sent
      Returns:
      the string
    • formatPercentageOf

      public static String formatPercentageOf(long size, long sent)
      Format percentage with the of, over and left to comment.
      Parameters:
      size - the size
      sent - the sent
      Returns:
      the string
    • formatString

      public static String formatString(String text, int width)
      Format string.
      Parameters:
      text - the text
      width - the width
      Returns:
      the string
    • formatString

      public static String formatString(Object text, int width)
      Format string.
      Parameters:
      text - the text
      width - the width
      Returns:
      the string
    • formatString

      public static String formatString(Object text, int width, boolean onRight)
      Format string.
      Parameters:
      text - the text
      width - the width
      onRight - the on right
      Returns:
      the string
    • formatString

      public static String formatString(String text, int width, char empty)
      Format string.
      Parameters:
      text - the text
      width - the width
      empty - the empty
      Returns:
      the string
    • formatString

      public static String formatString(String text, int width, char empty, boolean onRight)
      Format string.
      Parameters:
      text - the text
      width - the width
      empty - the empty
      onRight - the on right
      Returns:
      the string
    • parseDurationWithDefaultInMillis

      public static long parseDurationWithDefaultInMillis(String duration) throws NumberFormatException
      Parse the duration with default value in milliseconds.
      Parameters:
      duration - the duration string
      Returns:
      the duration
      Throws:
      NumberFormatException - the number format exception
    • getDuration

      public static long getDuration(String duration) throws NumberFormatException
      Gets the duration with default value in hours.
      Parameters:
      duration - the duration
      Returns:
      the duration
      Throws:
      NumberFormatException - the number format exception
    • formatValue

      public static String formatValue(long value, int width)
      Format value.
      Parameters:
      value - the value
      width - the width
      Returns:
      the string
    • trimString

      public static String trimString(String text, int maxSize)
      Trim string.
      Parameters:
      text - the text
      maxSize - the max size
      Returns:
      the string
    • formatUntil

      public static String formatUntil(long lastDate)
      Format until.
      Parameters:
      lastDate - the last date
      Returns:
      the string
    • fromXMLValue

      public static String fromXMLValue(String target)
      From xml value.
      Parameters:
      target - the target
      Returns:
      the string
    • hide

      public static String hide(Object value)
      Hide.
      Parameters:
      value - the value
      Returns:
      the string
    • windowsToUnix

      public static String windowsToUnix(String text)
      Windows to unix.
      Parameters:
      text - the text
      Returns:
      the string
    • windowsToUnix

      public static byte[] windowsToUnix(byte[] data, int offset, int len)
      Windows to unix.
      Parameters:
      data - the data
      offset - the offset
      len - the len
      Returns:
      the byte[]
    • unixToWindows

      public static String unixToWindows(String text)
      Unix to windows.
      Parameters:
      text - the text
      Returns:
      the string
    • replaceAll

      public static void replaceAll(StringBuilder target, String name, Object value)
      Replace all.
      Parameters:
      target - the target
      name - the name
      value - the value
    • replaceAllExt

      public static void replaceAllExt(StringBuilder target, String name, String value) throws IOException
      Replace all. Also check if there are no parameters configured after the parameter name (.e.g $target[2..3]) in target.
      Parameters:
      target - the target
      name - the name
      value - the value
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • replaceAllExt

      public static String replaceAllExt(String target, String name, String value) throws IOException
      Replace all. Also check if there are no parameters configured after the parameter name (.e.g $target[2..3]) in target.
      Parameters:
      target - the target
      name - the name
      value - the value
      Returns:
      the string
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • replaceAll

      public static String replaceAll(String target, String name, Object value)
      Replace all.
      Parameters:
      target - the target
      name - the name
      value - the value
      Returns:
      the string
    • toTime

      public static long toTime(String time)
      Converts into time.
      Parameters:
      time - the time
      Returns:
      the long
    • toTime

      public static long toTime(String format, String time)
      Converts into time.
      Parameters:
      format - the format
      time - the time
      Returns:
      the long
    • toXMLValue

      public static String toXMLValue(String target)
      Converts into xml value.
      Parameters:
      target - the target
      Returns:
      the string
    • trimString

      public static String trimString(String message, String empty)
      Trim string.
      Parameters:
      message - the message
      empty - the empty
      Returns:
      the string
    • toHexa

      public static String toHexa(byte[] bytes)
      Converts into hexa.
      Parameters:
      bytes - the bytes
      Returns:
      the string
    • getHash

      public static String getHash(byte[] bytes)
      Return a hash of the string.
      Parameters:
      bytes - the bytes
      Returns:
      the string
    • toBytes

      public static byte[] toBytes(String hexa) throws NumberFormatException
      Converts into bytes.
      Parameters:
      hexa - the hexa
      Returns:
      the byte[]
      Throws:
      NumberFormatException - the number format exception
    • toBytes

      public static byte[] toBytes(Object object) throws IOException
      Converts into bytes.
      Parameters:
      object - the object
      Returns:
      the byte[]
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • toObject

      public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException
      Converts into object.
      Parameters:
      bytes - the bytes
      Returns:
      the object
      Throws:
      IOException - Signals that an I/O exception has occurred.
      ClassNotFoundException - the class not found exception
    • getHostAddress

      public static String getHostAddress(String host)
      Gets the host address.
      Parameters:
      host - the host
      Returns:
      the host address
    • getHostName

      public static String getHostName(String host)
      Gets the host name.
      Parameters:
      host - the host
      Returns:
      the host name
    • getParameters

      public static String[] getParameters(String message, String token)
      Gets the parameters.
      Parameters:
      message - the message
      token - the token
      Returns:
      the parameters
    • getLastMessage

      public static String getLastMessage(Throwable t, String defaultMessage)
      Gets the message.
      Parameters:
      t - the t
      defaultMessage - the default message
      Returns:
      the message
    • getMessage

      public static String getMessage(Throwable t, String defaultMessage, int limit)
      Gets the message.
      Parameters:
      t - the t
      defaultMessage - the default message
      limit - the limit
      Returns:
      the message
    • getMessage

      public static String getMessage(Throwable t)
      Gets the message.
      Parameters:
      t - the t
      Returns:
      the message
    • getRemoteException

      public static RemoteException getRemoteException(String root, String message)
      Creates a RemoteException containing a concise message and the local hostname, intended for use across RMI boundaries. The stack trace is explicitly cleared to reduce native memory usage and minimize data transfer during remote exception serialization.
      Parameters:
      root - the root
      message - the message
      Returns:
      a RemoteException with a trimmed stack trace and formatted message
    • getRemoteException

      public static RemoteException getRemoteException(String root, String message, Throwable t)
      Creates a RemoteException containing a concise message and the local hostname, intended for use across RMI boundaries. The stack trace is explicitly cleared to reduce native memory usage and minimize data transfer during remote exception serialization.
      Parameters:
      root - the root
      message - the message
      t - the original Throwable to extract the message from
      Returns:
      a RemoteException with a trimmed stack trace and formatted message
    • getRemoteException

      public static RemoteException getRemoteException(String root, Throwable t)
      Creates a RemoteException containing a concise message and the local hostname, intended for use across RMI boundaries. The stack trace is explicitly cleared to reduce native memory usage and minimize data transfer during remote exception serialization.
      Parameters:
      root - the root
      t - the original Throwable to extract the message from
      Returns:
      a RemoteException with a trimmed stack trace and formatted message
    • decode

      public static String decode(String s) throws UnsupportedEncodingException
      Decode.
      Parameters:
      s - the s
      Returns:
      the string
      Throws:
      UnsupportedEncodingException - the unsupported encoding exception
    • getClassName

      public static String getClassName(Object object)
      Gets the class name.
      Parameters:
      object - the object
      Returns:
      the class name
    • choose

      public static String choose(String options) throws Format.DuplicatedChooseScore, ScriptException
      Find the best score to select the line. If no match is found return the first line. /data1/ecmwf/atmos/ ($dataTransfer[target].=DA) /data1/ecmwf/atmos/DA/ ($dataTransfer[target].=DW) /data1/ecmwf/atmos/DW/ ($dataTransfer[target].=EA) /data1/ecmwf/atmos/EA/ ($dataTransfer[target].=EW) /data1/ecmwf/atmos/EW/ ($dataTransfer[target].=S1) /data1/ecmwf/atmos/S1/ ($dataTransfer[target].=M1) /data1/ecmwf/atmos/M1/ ($dataTransfer[target].=T1) /data1/ecmwf/atmos/T1/
      Parameters:
      options - the options to choose from.
      Returns:
      the selection
      Throws:
      Format.DuplicatedChooseScore - the duplicated choose score
      ScriptException - the script exception
    • choose

      public static String choose(String key, String options) throws IOException
      The options are in the following format: (.= /readme.txt) standby=yes (== {avhrr_n.*}) standby=never;delay=2h And this method looks for the first line which apply to the selected key. For example, if the key is /etc/readme.txt then the first line will be selected and the method will return "standby=yes". The operator for comparison are the following: ".=" starts with; "==" equals to, if the second part is between {} then it is considered as a regular expression (regex); "=." ends with; "!=" different than.
      Parameters:
      key - the key to look for.
      options - the options to choose from.
      Returns:
      the selection or null if nothing is found
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • objectToString

      public static String objectToString(Object object)
      Object to string.
      Parameters:
      object - the object
      Returns:
      the string
    • getCleanPath

      public static String getCleanPath(String path)
      Clean the path by removing all occurrences of "///" by "/", all "\" by "/" and all trailing "/".
      Parameters:
      path - the path
      Returns:
      the string
    • normalizePath

      public static String normalizePath(String path) throws FileNotFoundException
      Normalize path.
      Parameters:
      path - the path
      Returns:
      the string
      Throws:
      FileNotFoundException - the file not found exception
    • normalizePath

      public static String normalizePath(String path, boolean keepEndTrailer) throws FileNotFoundException
      Normalize path.
      Parameters:
      path - the path
      keepEndTrailer - the keep end trailer
      Returns:
      the string
      Throws:
      FileNotFoundException - the file not found exception
    • compressBase64

      public static String compressBase64(StringBuilder str) throws IOException
      Compresses a text using GZIP and then encodes the result in Base64.

      This method is useful when you need to transmit or store a large string efficiently in a text-only medium (e.g., JSON, database, XML), since the GZIP compression reduces size and Base64 ensures safe encoding in environments that expect plain text.

      Parameters:
      str - the input text to compress and encode (as a StringBuilder). If null or empty, null is returned.
      Returns:
      the compressed and Base64-encoded string, or null if input was empty.
      Throws:
      IOException - if an I/O error occurs during compression.
    • uncompressBase64

      public static String uncompressBase64(String str) throws IOException
      Decodes a Base64 string and then decompresses it with GZIP to restore the original text.

      This method reverses the compressBase64(StringBuilder) process: it first decodes the Base64-encoded bytes, then inflates them using GZIP, and finally reconstructs the original string with UTF-8 decoding.

      Parameters:
      str - the Base64-encoded compressed string. If null or empty, null is returned.
      Returns:
      the uncompressed original string, or null if input was empty.
      Throws:
      IOException - if an I/O error occurs during decompression.
    • uncompress

      public static String uncompress(byte[] bytes) throws IOException
      Uncompress.
      Parameters:
      bytes - the bytes
      Returns:
      the string
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • cleanTextContent

      public static String cleanTextContent(String text)
      Clean text content.
      Parameters:
      text - the text
      Returns:
      the string
    • getFtpList

      public static String getFtpList(String permission, String user, String group, String size, long time, String name)
      Gets the ftp list.
      Parameters:
      permission - the permission
      user - the user
      group - the group
      size - the size
      time - the time
      name - the name
      Returns:
      the ftp list
    • matches

      public static boolean matches(String pattern, String text)
      _matches.
      Parameters:
      pattern - the pattern
      text - the text
      Returns:
      true, if successful
    • removeUnknownMetadata

      public static String removeUnknownMetadata(String value) throws IOException
      Removes all the unknown metadata tags (e.g. replace $metadata[...] with '').
      Parameters:
      value - the value
      Returns:
      the string
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • extractSearchParameters

      public static Map<String,String> extractSearchParameters(String defaultOptionName, String input)
      Extract search parameters. This method is extracting the parameters in the following form: "param1=value1 param2=space\\ char param3=equal\\=char" (the ' ' and = characters can be escaped)
      Parameters:
      defaultOptionName - the default option name
      input - the input
      Returns:
      the map
    • matchesGlobPattern

      public static boolean matchesGlobPattern(String pattern, String input)
      Matches glob pattern. This method uses the PathMatcher interface with the glob syntax to perform matching based on Unix-style wildcard patterns ('*' and '?').
      Parameters:
      pattern - the pattern
      input - the input
      Returns:
      true, if successful