public final class JOrphanUtils extends Object
Modifier and Type | Method and Description |
---|---|
static byte[] |
baToHexBytes(byte[] ba)
Convert binary byte array to hex string.
|
static String |
baToHexString(byte[] ba)
Convert binary byte array to hex string.
|
static String |
baToHexString(byte[] ba,
char separator)
Convert binary byte array to hex string.
|
static String |
booleanToSTRING(boolean value)
Convert a boolean to its upper case string representation.
|
static void |
canSafelyWriteToFolder(File folder)
Check whether we can write to a folder.
|
static void |
canSafelyWriteToFolder(File folder,
boolean deleteFolderContent)
Check whether we can write to a folder.
|
static void |
canSafelyWriteToFolder(File folder,
boolean deleteFolderIfExists,
FileFilter exporterFileFilter)
Check whether we can write to a folder.
|
static void |
canSafelyWriteToFolder(File folder,
FileFilter fileFilter)
Check whether we can write to a folder.
|
static void |
closeQuietly(Closeable cl)
Close a Closeable with no error thrown
|
static void |
closeQuietly(ServerSocket sock)
close a Socket with no error thrown
|
static void |
closeQuietly(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 String |
formatDuration(long elapsedSec) |
static String |
generateRandomAlphanumericPassword(int length) |
static byte[] |
getByteArraySlice(byte[] array,
int begin,
int end)
Returns a slice of a byte array.
|
static boolean |
isBlank(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 StringBuilder |
leftAlign(StringBuilder in,
int len)
Left aligns some text in a StringBuilder N.B.
|
static String |
nullifyIfEmptyTrimmed(String input)
Returns
null if input is empty, null or contains spaces only |
static int |
read(InputStream is,
byte[] buffer,
int offset,
int length)
Read as much as possible into buffer.
|
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
|
static 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 String |
replaceFirst(String source,
String search,
String replace)
Simple-minded String.replace() for JDK1.3 Should probably be recoded...
|
static int |
replaceValue(String regex,
String replaceBy,
boolean caseSensitive,
String value,
Consumer<String> setter)
Replace all occurrences of
regex in value by replaceBy if value is not blank. |
static StringBuilder |
rightAlign(StringBuilder in,
int len)
Right aligns some text in a StringBuilder N.B.
|
static String[] |
split(String splittee,
String splitChar) |
static String[] |
split(String splittee,
String splitChar,
boolean truncate)
This is almost equivalent to the
String.split(String) method in JDK 1.4. |
static 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 boolean |
startsWith(byte[] target,
byte[] search,
int offset)
Check if a byte array starts with the given byte array.
|
static String |
substitute(String input,
String pattern,
String sub)
Replace all patterns in a String
|
static String |
trim(String input,
String delims)
Trim a string by the tokens provided.
|
static String |
unsplit(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 void |
write(byte[] data,
OutputStream output)
Write data to an output stream in chunks with a maximum size of 4K.
|
public static String[] split(String splittee, String splitChar, boolean truncate)
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.
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?null
or the splitChar is null
split(String, String, String)
public static String[] split(String splittee, String delims, String def)
Trailing delimiters are significant (unless the default = null)
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. If null, then ignore omitted elements.NullPointerException
- if splittee or delims are null
split(String, String, boolean)
,
This is a rewritten version of JMeterUtils.split()
public static StringBuilder rightAlign(StringBuilder in, int len)
in
- StringBuilder containing some textlen
- output length desiredpublic static StringBuilder leftAlign(StringBuilder in, int len)
in
- StringBuilder containing some textlen
- output length desiredpublic static String booleanToSTRING(boolean value)
value
- boolean to convertpublic static String replaceFirst(String source, String search, String replace)
source
- input stringsearch
- string to look for (no regular expressions)replace
- string to replace the search stringpublic static String replaceAllChars(String source, char search, String replace)
source
- input stringsearch
- char to look for (no regular expressions)replace
- string to replace the search stringpublic static String substitute(String input, String pattern, String sub)
input
- - string to be transformedpattern
- - pattern to replacesub
- - replacement- JDK1.4 only
public static String trim(String input, String delims)
input
- string to trimdelims
- list of delimiterspublic static byte[] getByteArraySlice(byte[] array, int begin, int end)
TODO - add bounds checking?
array
- input arraybegin
- start of sliceend
- end of slicepublic static void closeQuietly(Closeable cl)
cl
- - Closeable (may be null)public static void closeQuietly(Socket sock)
sock
- - Socket (may be null)public static void closeQuietly(ServerSocket sock)
sock
- - ServerSocket (may be null)public static boolean startsWith(byte[] target, byte[] search, int offset)
target
- array to scansearch
- array to search foroffset
- starting offset (≥0)String.startsWith(String, int)
public static boolean isXML(byte[] target)
target
- the content to checkpublic static String baToHexString(byte[] ba)
ba
- input binary byte arraypublic static String baToHexString(byte[] ba, char separator)
ba
- input binary byte arrayseparator
- the separator to be added between pairs of hex digitspublic static byte[] baToHexBytes(byte[] ba)
ba
- input binary byte arraypublic static int read(InputStream is, byte[] buffer, int offset, int length) throws IOException
is
- the stream to read frombuffer
- output bufferoffset
- offset into bufferlength
- number of bytes to readIOException
- if some I/O errors occurpublic static void displayThreads(boolean includeDaemons)
includeDaemons
- whether to include daemon threads or not.public static String nullifyIfEmptyTrimmed(String input)
null
if input is empty, null
or contains spaces onlyinput
- Stringnull
public static boolean isBlank(String value)
null
or whitespace only.value
- Valuetrue
if the String is not empty (""), not null
and not whitespace only.public static void write(byte[] data, OutputStream output) throws IOException
data
- the buffer to be writtenoutput
- the output stream to useIOException
- if there is a problem writing the datapublic static String formatDuration(long elapsedSec)
elapsedSec
- long elapsed time in secondspublic static void canSafelyWriteToFolder(File folder)
IllegalArgumentException
if folder cannot be written to either:
folder
- to checkIllegalArgumentException
- when folder can't be written topublic static void canSafelyWriteToFolder(File folder, FileFilter fileFilter)
IllegalArgumentException
if folder cannot be written to either:
folder
- to checkfileFilter
- used to filter listing of folderIllegalArgumentException
- when folder can't be written topublic static void canSafelyWriteToFolder(File folder, boolean deleteFolderContent)
deleteFolderContent
is true
the folder or file with
the same name will be emptied or deleted.folder
- to checkdeleteFolderContent
- flag whether the folder should be emptied or a file with the same name deletedIllegalArgumentException
- when folder can't be written to
Throw IllegalArgumentException if folder cannot be writtenpublic static void canSafelyWriteToFolder(File folder, boolean deleteFolderIfExists, FileFilter exporterFileFilter)
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 folderIllegalArgumentException
- when folder can't be written to. That could have the following reasons:
public static Object[] replaceAllWithRegex(String source, String regex, String replacement, boolean caseSensitive)
source
- Source textregex
- Regular expressionreplacement
- Replacement text to which function applies a quotingcaseSensitive
- is case taken into accountpublic static int replaceValue(String regex, String replaceBy, boolean caseSensitive, String value, Consumer<String> setter)
regex
in value
by replaceBy
if value
is not blank.
The replaced text is fed into the setter
.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 valuepublic static String unsplit(Object[] splittee, Object splitChar)
splittee
- Array of Objects to be concatenated.splitChar
- Object to unsplit the strings with.public static String generateRandomAlphanumericPassword(int length)
length
- Max length of passwordCopyright © 1998-2019 Apache Software Foundation. All Rights Reserved.