org.apache.jorphan.util

## Class JOrphanUtils

- java.lang.Object
- - org.apache.jorphan.util.JOrphanUtils

-     public final class JOrphanUtils
      extends java.lang.Object

  This class contains frequently-used static utility methods.

- - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static byte[]` | `baToHexBytes(byte[] ba)` Convert binary byte array to hex string.
    `static java.lang.String` | `baToHexString(byte[] ba)` Convert binary byte array to hex string.
    `static java.lang.String` | `baToHexString(byte[] ba, char separator)` Convert binary byte array to hex string.
    `static java.lang.String` | `booleanToSTRING(boolean value)` Convert a boolean to its upper case string representation.
    `static void` | `canSafelyWriteToFolder(java.io.File folder)` Check whether we can write to a folder.
    `static void` | `canSafelyWriteToFolder(java.io.File folder, boolean deleteFolderContent)` Check whether we can write to a folder.
    `static void` | `canSafelyWriteToFolder(java.io.File folder, boolean deleteFolderIfExists, java.io.FileFilter exporterFileFilter)` Check whether we can write to a folder.
    `static void` | `canSafelyWriteToFolder(java.io.File folder, java.io.FileFilter fileFilter)` Check whether we can write to a folder.
    `static void` | `closeQuietly(java.io.Closeable cl)` Close a Closeable with no error thrown
    `static void` | `closeQuietly(java.net.ServerSocket sock)` close a Socket with no error thrown
    `static void` | `closeQuietly(java.net.Socket sock)` close a Socket with no error thrown
    `static void` | `displayThreads(boolean includeDaemons)` Display currently running threads on system.out This may be expensive to run.
    `static java.lang.String` | `formatDuration(long elapsedSec)`
    `static byte[]` | `getByteArraySlice(byte[] array, int begin, int end)` Returns a slice of a byte array.
    `static boolean` | `isBlank(java.lang.String value)` Check that value is empty (""), `null` or whitespace only.
    `static boolean` | `isXML(byte[] target)` Detects if some content starts with the standard XML prefix.
    `static java.lang.StringBuilder` | `leftAlign(java.lang.StringBuilder in, int len)` Left aligns some text in a StringBuilder N.B. modifies the input buffer
    `static java.lang.String` | `nullifyIfEmptyTrimmed(java.lang.String input)` Returns `null` if input is empty, `null` or contains spaces only
    `static int` | `read(java.io.InputStream is, byte[] buffer, int offset, int length)` Read as much as possible into buffer.
    `static java.lang.String` | `replaceAllChars(java.lang.String source, char search, java.lang.String replace)` Version of String.replaceAll() for JDK1.3 See below for another version which replaces strings rather than chars and provides a fast path which does not allocate memory
    `static java.lang.Object[]` | `replaceAllWithRegex(java.lang.String source, java.lang.String regex, java.lang.String replacement, boolean caseSensitive)` Replace in source all matches of regex by replacement taking into account case if caseSensitive is true
    `static java.lang.String` | `replaceFirst(java.lang.String source, java.lang.String search, java.lang.String replace)` Simple-minded String.replace() for JDK1.3 Should probably be recoded...
    `static int` | `replaceValue(java.lang.String regex, java.lang.String replaceBy, boolean caseSensitive, java.lang.String value, java.util.function.Consumer<java.lang.String> setter)` Replace all occurrences of `regex` in `value` by `replaceBy` if `value` is not blank.
    `static java.lang.StringBuilder` | `rightAlign(java.lang.StringBuilder in, int len)` Right aligns some text in a StringBuilder N.B. modifies the input buffer
    `static java.lang.String[]` | `split(java.lang.String splittee, java.lang.String splitChar)`
    `static java.lang.String[]` | `split(java.lang.String splittee, java.lang.String splitChar, boolean truncate)` This is almost equivalent to the `String.split(String)` method in JDK 1.4.
    `static java.lang.String[]` | `split(java.lang.String splittee, java.lang.String delims, java.lang.String def)` Takes a String and a tokenizer character string, and returns a new array of strings of the string split by the tokenizer character(s).
    `static boolean` | `startsWith(byte[] target, byte[] search, int offset)` Check if a byte array starts with the given byte array.
    `static java.lang.String` | `substitute(java.lang.String input, java.lang.String pattern, java.lang.String sub)` Replace all patterns in a String
    `static java.lang.String` | `trim(java.lang.String input, java.lang.String delims)` Trim a string by the tokens provided.
    `static java.lang.String` | `unsplit(java.lang.Object[] splittee, java.lang.Object splitChar)` Takes an array of strings and a tokenizer character, and returns a string of all the strings concatenated with the tokenizer string in between each one.
    `static void` | `write(byte[] data, java.io.OutputStream output)` Write data to an output stream in chunks with a maximum size of 4K.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### split

          public static java.lang.String[] split(java.lang.String splittee,
                                                 java.lang.String splitChar,
                                                 boolean truncate)

      This is almost equivalent to the `String.split(String)` method in JDK 1.4. It is here to enable us to support earlier JDKs.

      Note that unlike JDK1.4 split(), it optionally ignores leading split Characters, and the splitChar parameter is not a Regular expression

      This piece of code used to be part of JMeterUtils, but was moved here because some JOrphan classes use it too.

      Parameters:
        `splittee` - String to be split
        `splitChar` - Character(s) to split the string on, these are treated as a single unit
        `truncate` - Should adjacent and leading/trailing splitChars be removed?
      Returns:
        Array of all the tokens; empty if the input string is `null` or the splitChar is `null`
      See Also:
        `split(String, String, String)`

    - #### split

          public static java.lang.String[] split(java.lang.String splittee,
                                                 java.lang.String splitChar)

    - #### split

          public static java.lang.String[] split(java.lang.String splittee,
                                                 java.lang.String delims,
                                                 java.lang.String def)

      Takes a String and a tokenizer character string, and returns a new array of strings of the string split by the tokenizer character(s).

      Trailing delimiters are significant (unless the default = null)

      Parameters:
        `splittee` - String to be split.
        `delims` - Delimiter character(s) to split the string on
        `def` - Default value to place between two split chars that have nothing between them. If null, then ignore omitted elements.
      Returns:
        Array of all the tokens.
      Throws:
        `java.lang.NullPointerException` - if splittee or delims are `null`
      See Also:
        `split(String, String, boolean)`, `This is a rewritten version of JMeterUtils.split()`

    - #### rightAlign

          public static java.lang.StringBuilder rightAlign(java.lang.StringBuilder in,
                                                           int len)

      Right aligns some text in a StringBuilder N.B. modifies the input buffer
      Parameters:
        `in` - StringBuilder containing some text
        `len` - output length desired
      Returns:
        input StringBuilder, with leading spaces

    - #### leftAlign

          public static java.lang.StringBuilder leftAlign(java.lang.StringBuilder in,
                                                          int len)

      Left aligns some text in a StringBuilder N.B. modifies the input buffer
      Parameters:
        `in` - StringBuilder containing some text
        `len` - output length desired
      Returns:
        input StringBuilder, with trailing spaces

    - #### booleanToSTRING

          public static java.lang.String booleanToSTRING(boolean value)

      Convert a boolean to its upper case string representation. Equivalent to Boolean.valueOf(boolean).toString().toUpperCase().
      Parameters:
        `value` - boolean to convert
      Returns:
        "TRUE" or "FALSE"

    - #### replaceFirst

          public static java.lang.String replaceFirst(java.lang.String source,
                                                      java.lang.String search,
                                                      java.lang.String replace)

      Simple-minded String.replace() for JDK1.3 Should probably be recoded...
      Parameters:
        `source` - input string
        `search` - string to look for (no regular expressions)
        `replace` - string to replace the search string
      Returns:
        the output string

    - #### replaceAllChars

          public static java.lang.String replaceAllChars(java.lang.String source,
                                                         char search,
                                                         java.lang.String replace)

      Version of String.replaceAll() for JDK1.3 See below for another version which replaces strings rather than chars and provides a fast path which does not allocate memory
      Parameters:
        `source` - input string
        `search` - char to look for (no regular expressions)
        `replace` - string to replace the search string
      Returns:
        the output string

    - #### substitute

          public static java.lang.String substitute(java.lang.String input,
                                                    java.lang.String pattern,
                                                    java.lang.String sub)

      Replace all patterns in a String
      Parameters:
        `input` - - string to be transformed
        `pattern` - - pattern to replace
        `sub` - - replacement
      Returns:
        the updated string
      See Also:
        `- JDK1.4 only`

    - #### trim

          public static java.lang.String trim(java.lang.String input,
                                              java.lang.String delims)

      Trim a string by the tokens provided.
      Parameters:
        `input` - string to trim
        `delims` - list of delimiters
      Returns:
        input trimmed at the first delimiter

    - #### getByteArraySlice

          public static byte[] getByteArraySlice(byte[] array,
                                                 int begin,
                                                 int end)

      Returns a slice of a byte array. TODO - add bounds checking?
      Parameters:
        `array` - - input array
        `begin` - - start of slice
        `end` - - end of slice
      Returns:
        slice from the input array

    - #### closeQuietly

          public static void closeQuietly(java.io.Closeable cl)

      Close a Closeable with no error thrown
      Parameters:
        `cl` - - Closeable (may be null)

    - #### closeQuietly

          public static void closeQuietly(java.net.Socket sock)

      close a Socket with no error thrown
      Parameters:
        `sock` - - Socket (may be null)

    - #### closeQuietly

          public static void closeQuietly(java.net.ServerSocket sock)

      close a Socket with no error thrown
      Parameters:
        `sock` - - ServerSocket (may be null)

    - #### startsWith

          public static boolean startsWith(byte[] target,
                                           byte[] search,
                                           int offset)

      Check if a byte array starts with the given byte array.
      Parameters:
        `target` - array to scan
        `search` - array to search for
        `offset` - starting offset (≥0)
      Returns:
        true if the search array matches the target at the current offset
      See Also:
        `String.startsWith(String, int)`

    - #### isXML

          public static boolean isXML(byte[] target)

      Detects if some content starts with the standard XML prefix.
      Parameters:
        `target` - the content to check
      Returns:
        true if the document starts with the standard XML prefix.

    - #### baToHexString

          public static java.lang.String baToHexString(byte[] ba)

      Convert binary byte array to hex string.
      Parameters:
        `ba` - input binary byte array
      Returns:
        hex representation of binary input

    - #### baToHexString

          public static java.lang.String baToHexString(byte[] ba,
                                                       char separator)

      Convert binary byte array to hex string.
      Parameters:
        `ba` - input binary byte array
        `separator` - the separator to be added between pairs of hex digits
      Returns:
        hex representation of binary input

    - #### baToHexBytes

          public static byte[] baToHexBytes(byte[] ba)

      Convert binary byte array to hex string.
      Parameters:
        `ba` - input binary byte array
      Returns:
        hex representation of binary input

    - #### read

          public static int read(java.io.InputStream is,
                                 byte[] buffer,
                                 int offset,
                                 int length)
                          throws java.io.IOException

      Read as much as possible into buffer.
      Parameters:
        `is` - the stream to read from
        `buffer` - output buffer
        `offset` - offset into buffer
        `length` - number of bytes to read
      Returns:
        the number of bytes actually read
      Throws:
        `java.io.IOException` - if some I/O errors occur

    - #### displayThreads

          public static void displayThreads(boolean includeDaemons)

      Display currently running threads on system.out This may be expensive to run. Mainly designed for use at the end of a non-GUI test to check for threads that might prevent the JVM from exiting.
      Parameters:
        `includeDaemons` - whether to include daemon threads or not.

    - #### nullifyIfEmptyTrimmed

          public static java.lang.String nullifyIfEmptyTrimmed(java.lang.String input)

      Returns `null` if input is empty, `null` or contains spaces only
      Parameters:
        `input` - String
      Returns:
        trimmed input or `null`

    - #### isBlank

          public static boolean isBlank(java.lang.String value)

      Check that value is empty (""), `null` or whitespace only.
      Parameters:
        `value` - Value
      Returns:
        `true` if the String is not empty (""), not `null` and not whitespace only.

    - #### write

          public static void write(byte[] data,
                                   java.io.OutputStream output)
                            throws java.io.IOException

      Write data to an output stream in chunks with a maximum size of 4K. This is to avoid OutOfMemory issues if the data buffer is very large and the JVM needs to copy the buffer for use by native code.
      Parameters:
        `data` - the buffer to be written
        `output` - the output stream to use
      Throws:
        `java.io.IOException` - if there is a problem writing the data

    - #### formatDuration

          public static java.lang.String formatDuration(long elapsedSec)

      Parameters:
        `elapsedSec` - long elapsed time in seconds
      Returns:
        String formatted with format HH:mm:ss

    - #### canSafelyWriteToFolder

          public static void canSafelyWriteToFolder(java.io.File folder)

      Check whether we can write to a folder. A folder can be written to if if does not contain any file or folder Throw `IllegalArgumentException` if folder cannot be written to either:
      - Because it exists but is not a folder
      - Because it exists but is not empty
      - Because it does not exist but cannot be created
      Parameters:
        `folder` - to check
      Throws:
        `java.lang.IllegalArgumentException` - when folder can't be written to

    - #### canSafelyWriteToFolder

          public static void canSafelyWriteToFolder(java.io.File folder,
                                                    java.io.FileFilter fileFilter)

      Check whether we can write to a folder. A folder can be written to if folder.listFiles(exporterFileFilter) does not return any file or folder. Throw `IllegalArgumentException` if folder cannot be written to either:
      - Because it exists but is not a folder
      - Because it exists but is not empty using folder.listFiles(exporterFileFilter)
      - Because it does not exist but cannot be created
      Parameters:
        `folder` - to check
        `fileFilter` - used to filter listing of folder
      Throws:
        `java.lang.IllegalArgumentException` - when folder can't be written to

    - #### canSafelyWriteToFolder

          public static void canSafelyWriteToFolder(java.io.File folder,
                                                    boolean deleteFolderContent)

      Check whether we can write to a folder. If `deleteFolderContent` is `true` the folder or file with the same name will be emptied or deleted.
      Parameters:
        `folder` - to check
        `deleteFolderContent` - flag whether the folder should be emptied or a file with the same name deleted
      Throws:
        `java.lang.IllegalArgumentException` - when folder can't be written to Throw IllegalArgumentException if folder cannot be written

    - #### canSafelyWriteToFolder

          public static void canSafelyWriteToFolder(java.io.File folder,
                                                    boolean deleteFolderIfExists,
                                                    java.io.FileFilter exporterFileFilter)

      Check whether we can write to a folder.
      Parameters:
        `folder` - which should be checked for writability and emptiness
        `deleteFolderIfExists` - flag whether the folder should be emptied or a file with the same name deleted
        `exporterFileFilter` - used for filtering listing of the folder
      Throws:
        `java.lang.IllegalArgumentException` - when folder can't be written to. That could have the following reasons:
        - it exists but is not a folder
        - it exists but is not empty
        - it does not exist but cannot be created

    - #### replaceAllWithRegex

          public static java.lang.Object[] replaceAllWithRegex(java.lang.String source,
                                                               java.lang.String regex,
                                                               java.lang.String replacement,
                                                               boolean caseSensitive)

      Replace in source all matches of regex by replacement taking into account case if caseSensitive is true
      Parameters:
        `source` - Source text
        `regex` - Regular expression
        `replacement` - Replacement text to which function applies a quoting
        `caseSensitive` - is case taken into account
      Returns:
        array of Object where first row is the replaced text, second row is the number of replacement that occurred

    - #### replaceValue

          public static int replaceValue(java.lang.String regex,
                                         java.lang.String replaceBy,
                                         boolean caseSensitive,
                                         java.lang.String value,
                                         java.util.function.Consumer<java.lang.String> setter)

      Replace all occurrences of `regex` in `value` by `replaceBy` if `value` is not blank. The replaced text is fed into the `setter`.
      Parameters:
        `regex` - Regular expression that is used for the search
        `replaceBy` - value that is used for replacement
        `caseSensitive` - flag whether the regex should be applied case sensitive
        `value` - in which the replacement takes place
        `setter` - that gets called with the replaced value
      Returns:
        number of matches that were replaced

    - #### unsplit

          public static java.lang.String unsplit(java.lang.Object[] splittee,
                                                 java.lang.Object splitChar)

      Takes an array of strings and a tokenizer character, and returns a string of all the strings concatenated with the tokenizer string in between each one.
      Parameters:
        `splittee` - Array of Objects to be concatenated.
        `splitChar` - Object to unsplit the strings with.
      Returns:
        Array of all the tokens.

Copyright © 1998-2019 Apache Software Foundation. All Rights Reserved.