Class JOrphanUtils
-
- All Implemented Interfaces:
public final class JOrphanUtilsThis class contains frequently-used static utility methods.
-
-
Method Summary
Modifier and Type Method Description static Array<String>split(String splittee, String splitChar, boolean truncate)This is almost equivalent to the split method in JDK 1.4. static Array<String>split(String splittee, String splitChar)static Array<String>split(String splittee, String delims, 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 StringBuilderrightAlign(StringBuilder in, int len)Right aligns some text in a StringBuilder N.B. static StringBuilderleftAlign(StringBuilder in, int len)Left aligns some text in a StringBuilder N.B. static StringbooleanToSTRING(boolean value)Convert a boolean to its upper case string representation. static StringreplaceFirst(String source, String search, String replace)Simple-minded String.replace() for JDK1.3 Should probably be recoded... static StringreplaceAllChars(String source, char search, String replace)Version of String.replaceAll() for JDK1. static Stringsubstitute(String input, String pattern, String sub)Replace all patterns in a String static Stringtrim(String input, String delims)Trim a string by the tokens provided. static Array<byte>getByteArraySlice(Array<byte> array, int begin, int end)Returns a slice of a byte array. static voidcloseQuietly(Closeable cl)Close a Closeable with no error thrown static voidcloseQuietly(Socket sock)close a Socket with no error thrown static voidcloseQuietly(ServerSocket sock)close a Socket with no error thrown static booleanstartsWith(Array<byte> target, Array<byte> search, int offset)Check if a byte array starts with the given byte array. static booleanisXML(Array<byte> target)Detects if some content starts with the standard XML prefix. static StringbaToHexString(Array<byte> ba)Convert binary byte array to hex string. static StringbaToHexString(Array<byte> ba, char separator)Convert binary byte array to hex string. static Array<byte>baToHexBytes(Array<byte> ba)Convert binary byte array to hex string. static intread(InputStream is, Array<byte> buffer, int offset, int length)Read as much as possible into buffer. static voiddisplayThreads(boolean includeDaemons)Display currently running threads on system.out This may be expensive to run. static StringnullifyIfEmptyTrimmed(String input)Returns nullif input is empty,nullor contains spaces onlystatic booleanisBlank(String value)Check that value is empty (""), nullor whitespace only.static voidwrite(Array<byte> data, OutputStream output)Write data to an output stream in chunks with a maximum size of 4K. static StringformatDuration(long elapsedSec)Returns duration formatted with format HH:mm:ss. static voidcanSafelyWriteToFolder(File folder)Check whether we can write to a folder. static voidcanSafelyWriteToFolder(File folder, FileFilter fileFilter)Check whether we can write to a folder. static voidcanSafelyWriteToFolder(File folder, boolean deleteFolderContent)Check whether we can write to a folder. static voidcanSafelyWriteToFolder(File folder, boolean deleteFolderIfExists, FileFilter exporterFileFilter)Check whether we can write to a folder. static Array<Object>replaceAllWithRegex(String source, String regex, String replacement, boolean caseSensitive)Replace in source all matches of regex by replacement taking into account case if caseSensitive is true static intreplaceValue(String regex, String replaceBy, boolean caseSensitive, String value, Consumer<in String> setter)Replace all occurrences of regexinvaluebyreplaceByifvalueis not blank.static Stringunsplit(Array<Object> splittee, 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 StringgenerateRandomAlphanumericPassword(int length)Random alphanumeric password of a given length. -
-
Method Detail
-
split
static Array<String> split(String splittee, String splitChar, boolean truncate)
This is almost equivalent to the split 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 splitsplitChar- Character(s) to split the string on, these are treated as a single unittruncate- Should adjacent and leading/trailing splitChars be removed?- Returns:
Array of all the tokens; empty if the input string is
nullor the splitChar isnull
-
split
static Array<String> split(String splittee, String delims, 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 ondef- Default value to place between two split chars that have nothing between them.- Returns:
Array of all the tokens.
-
rightAlign
static StringBuilder rightAlign(StringBuilder in, int len)
Right aligns some text in a StringBuilder N.B. modifies the input builder
- Parameters:
in- StringBuilder containing some textlen- output length desired- Returns:
input StringBuilder, with leading spaces
-
leftAlign
static StringBuilder leftAlign(StringBuilder in, int len)
Left aligns some text in a StringBuilder N.B. modifies the input builder
- Parameters:
in- StringBuilder containing some textlen- output length desired- Returns:
input StringBuilder, with trailing spaces
-
booleanToSTRING
static 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
static String replaceFirst(String source, String search, String replace)
Simple-minded String.replace() for JDK1.3 Should probably be recoded...
- Parameters:
source- input stringsearch- string to look for (no regular expressions)replace- string to replace the search string- Returns:
the output string
-
replaceAllChars
static String replaceAllChars(String source, char search, 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 stringsearch- char to look for (no regular expressions)replace- string to replace the search string- Returns:
the output string
-
substitute
static String substitute(String input, String pattern, String sub)
Replace all patterns in a String
- Parameters:
input- - string to be transformedpattern- - pattern to replacesub- - replacement- Returns:
the updated string
-
trim
static String trim(String input, String delims)
Trim a string by the tokens provided.
- Parameters:
input- string to trimdelims- list of delimiters- Returns:
input trimmed at the first delimiter
-
getByteArraySlice
static Array<byte> getByteArraySlice(Array<byte> array, int begin, int end)
Returns a slice of a byte array.
TODO - add bounds checking?
- Parameters:
array- input arraybegin- start of sliceend- end of slice- Returns:
slice from the input array
-
closeQuietly
static void closeQuietly(Closeable cl)
Close a Closeable with no error thrown
- Parameters:
cl- - Closeable (may be null)
-
closeQuietly
static void closeQuietly(Socket sock)
close a Socket with no error thrown
- Parameters:
sock- - Socket (may be null)
-
closeQuietly
static void closeQuietly(ServerSocket sock)
close a Socket with no error thrown
- Parameters:
sock- - ServerSocket (may be null)
-
startsWith
static boolean startsWith(Array<byte> target, Array<byte> search, int offset)
Check if a byte array starts with the given byte array.
- Parameters:
target- array to scansearch- array to search foroffset- starting offset (≥0)- Returns:
true if the search array matches the target at the current offset
-
isXML
static boolean isXML(Array<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
static String baToHexString(Array<byte> ba)
Convert binary byte array to hex string.
- Parameters:
ba- input binary byte array- Returns:
hex representation of binary input
-
baToHexString
static String baToHexString(Array<byte> ba, char separator)
Convert binary byte array to hex string.
- Parameters:
ba- input binary byte arrayseparator- the separator to be added between pairs of hex digits- Returns:
hex representation of binary input
-
baToHexBytes
static Array<byte> baToHexBytes(Array<byte> ba)
Convert binary byte array to hex string.
- Parameters:
ba- input binary byte array- Returns:
hex representation of binary input
-
read
static int read(InputStream is, Array<byte> buffer, int offset, int length)
Read as much as possible into buffer.
- Parameters:
is- the stream to read frombuffer- output bufferoffset- offset into bufferlength- number of bytes to read- Returns:
the number of bytes actually read
-
displayThreads
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
static String nullifyIfEmptyTrimmed(String input)
Returns
nullif input is empty,nullor contains spaces only- Parameters:
input- String- Returns:
trimmed input or
null
-
isBlank
static boolean isBlank(String value)
Check that value is empty (""),
nullor whitespace only.- Parameters:
value- Value- Returns:
trueif the String is not empty (""), notnulland not whitespace only.
-
write
static void write(Array<byte> data, OutputStream output)
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 writtenoutput- the output stream to use
-
formatDuration
static String formatDuration(long elapsedSec)
Returns duration formatted with format HH:mm:ss.
- Parameters:
elapsedSec- long elapsed time in seconds- Returns:
String formatted with format HH:mm:ss
-
canSafelyWriteToFolder
static void canSafelyWriteToFolder(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
-
canSafelyWriteToFolder
static void canSafelyWriteToFolder(File folder, 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 checkfileFilter- used to filter listing of folder
-
canSafelyWriteToFolder
static void canSafelyWriteToFolder(File folder, boolean deleteFolderContent)
Check whether we can write to a folder. If
deleteFolderContentistruethe folder or file with the same name will be emptied or deleted.- Parameters:
folder- to checkdeleteFolderContent- flag whether the folder should be emptied or a file with the same name deleted
-
canSafelyWriteToFolder
static void canSafelyWriteToFolder(File folder, boolean deleteFolderIfExists, FileFilter exporterFileFilter)
Check whether we can write to a folder.
- Parameters:
folder- which should be checked for writability and emptinessdeleteFolderIfExists- flag whether the folder should be emptied or a file with the same name deletedexporterFileFilter- used for filtering listing of the folder
-
replaceAllWithRegex
static Array<Object> replaceAllWithRegex(String source, String regex, String replacement, boolean caseSensitive)
Replace in source all matches of regex by replacement taking into account case if caseSensitive is true
- Parameters:
source- Source textregex- Regular expressionreplacement- Replacement text to which function applies a quotingcaseSensitive- 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
static int replaceValue(String regex, String replaceBy, boolean caseSensitive, String value, Consumer<in String> setter)
Replace all occurrences of
regexinvaluebyreplaceByifvalueis not blank. The replaced text is fed into thesetter.- Parameters:
regex- Regular expression that is used for the searchreplaceBy- value that is used for replacementcaseSensitive- flag whether the regex should be applied case sensitivevalue- in which the replacement takes placesetter- that gets called with the replaced value- Returns:
number of matches that were replaced
-
unsplit
static String unsplit(Array<Object> splittee, 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.
-
generateRandomAlphanumericPassword
static String generateRandomAlphanumericPassword(int length)
Random alphanumeric password of a given length.
- Parameters:
length- Max length of password- Returns:
String random password
-
-
-
-