public class Misc extends Object
| Modifier and Type | Field and Description |
|---|---|
static int |
BUFFERSIZE |
static String |
DEFAULT_INPUT_STREAM_ENCODING
Deprecated.
|
static String |
LINE_SEPARATOR |
| Constructor and Description |
|---|
Misc() |
| Modifier and Type | Method and Description |
|---|---|
static void |
addItemsToList(Collection<String> collection,
String list,
String collectionDescription,
boolean lowercase)
Adds items on a string, added by comma separator (ex: "1,2,3"), into a list.
|
static <T> void |
addToSortedListNonUnique(List<T> list,
T item) |
static <T> void |
addToSortedListUnique(List<T> list,
T item) |
static String |
asHex(byte[] buf) |
static byte[] |
compress(byte[] input)
Compresses the input string using the Deflater class of Java.
|
static byte[] |
compress(String input)
Compresses the input string using the default stream encoding.
|
static String |
concat(String separator,
String... parts) |
static String |
concatStrings(String part1,
String separator,
String part2)
Concatenates two strings, if specified, uses the separator in between two strings.
|
static int |
countRegex(String string,
String regex)
Counts the number of characters that the specified reges will affect in the specified string.
|
static String |
createNumericUUID() |
static String |
createRandomUUID() |
static String |
createRandomUUID(boolean removeDashes)
Creates a Universally Unique Identifier, via the java.util.UUID class (36 characters or 32 characters without dashes).
|
static String |
createSimpleUUID()
Creates a Universally Unique Identifier, via the java.rmi.server.UID class.
|
static String |
createUUID() |
static byte[] |
decompress(byte[] input)
Decompresses the compressed byte array.
|
static void |
fileToStream(String filename,
OutputStream output)
Copies the content of the specified file to an output stream.
|
static String |
fileToString(String fileName)
Please consider using resourceToString() instead of relying on files.
|
static String |
fileToString(String fileName,
String endOfLineString)
Please consider using resourceToString() instead of relying on files.
|
static void |
fileToWriter(String filename,
Writer writer)
Copies the content of the specified file to a writer.
|
static String |
getBuildOutputDirectory() |
static long |
getCurrentTimeMillis() |
static Properties |
getEnvironmentVariables() |
static String |
getHostname() |
static String |
getProjectBaseDir() |
static byte[] |
gunzip(byte[] input)
Unzips a zipped byte array to a string by create an expandable byte array to hold the decompressed data.
|
static byte[] |
gzip(byte[] input)
Creates an expandable byte array to hold the compressed data.
|
static byte[] |
gzip(String input)
Zips the input string with the default input stream encoding.
|
static boolean |
isEmpty(String string) |
static boolean |
isNotEmpty(String string) |
static String |
listToString(List list)
Converts the list to a string.
|
static long |
parseAge(String value,
long defaultValue) |
static String |
readerToString(Reader reader,
String endOfLineString)
Copies the content of a reader into a string, adds specified string to the end of the line, if specified.
|
static void |
readerToWriter(Reader reader,
Writer writer)
Copies the content of a reader to the buffer of a writer.
|
static String |
replace(String source,
String from,
String to)
String replacer.
|
static byte[] |
streamToBytes(InputStream inputStream)
Writes the content of an input stream to a byte array.
|
static void |
streamToFile(InputStream inputStream,
File file)
Writes the content of an input stream to a specified file.
|
static void |
streamToStream(InputStream input,
OutputStream output) |
static void |
streamToStream(InputStream input,
OutputStream output,
byte[] eof)
Writes the content of an input stream to an output stream by copying the buffer of input stream to the buffer of the output stream.
|
static String |
streamToString(InputStream stream) |
static String |
streamToString(InputStream stream,
String streamEncoding) |
static String |
streamToString(InputStream stream,
String endOfLineString,
String streamEncoding) |
static void |
stringToFile(String string,
String fileName)
Writes the string to a file.
|
static long |
toFileSize(String value,
long defaultValue)
Converts the file size to bytes.
|
static int |
unsignedByteToInt(byte b)
Converts an unsigned byte to its integer representation.
|
static String |
urlDecode(String input) |
public static final int BUFFERSIZE
@Deprecated public static final String DEFAULT_INPUT_STREAM_ENCODING
public static final String LINE_SEPARATOR
public static String createSimpleUUID()
public static String createUUID()
public static String createRandomUUID(boolean removeDashes)
public static String createRandomUUID()
public static String asHex(byte[] buf)
public static String createNumericUUID()
public static int unsignedByteToInt(byte b)
Misc.unsignedByteToInt(new Btye(12)) returns 12 Misc.unsignedByteToInt(new Byte(-12)) returns 244
b - byte to be converted.public static long getCurrentTimeMillis()
public static void fileToWriter(String filename, Writer writer) throws IOException
Example:
Writer writer = new StringWriter();
Misc.fileToWriter(someFileName, writer);
System.out.println(writer.toString) // prints the content of the writer
// that's copied from the file.
IOException - exception to be thrown exception to be thrown if an I/O exception occurspublic static void fileToStream(String filename, OutputStream output) throws IOException
Example:
OutputStream os = new ByteArrayOutputStream
Misc.fileToStream(someFileName, os);
System.out.println(os.toString) // prints the content of the output stream
// that's copied from the file.
IOException - exception to be thrown if an I/O exception occurspublic static void streamToStream(InputStream input, OutputStream output) throws IOException
IOExceptionpublic static void streamToStream(InputStream input, OutputStream output, byte[] eof) throws IOException
Example:
String test = "test";
ByteArrayInputStream bais = new ByteArrayInputStream(test.getBytes());
OutputStream baos = new ByteArrayOutputStream();
Misc.streamToStream(bais, baos);
System.out.println(baos.toString()); // prints "test"
IOException - exception to be thrown if an I/O exception occurspublic static void streamToFile(InputStream inputStream, File file) throws IOException
Example:
String test = "test";
ByteArrayInputStream bais = new ByteArrayInputStream(test.getBytes());
Misc.streamToFile(bais, file); // "test" copied inside the file.
IOException - exception to be thrown if an I/O exception occurspublic static byte[] streamToBytes(InputStream inputStream) throws IOException
Example:
String test = "test";
ByteArrayInputStream bais = new ByteArrayInputStream(test.getBytes());
byte[] arr = Misc.streamToBytes(bais);
System.out.println(new String(arr, StandardCharsets.UTF_8)); // prints "test"
IOExceptionpublic static void readerToWriter(Reader reader, Writer writer) throws IOException
Example:
Reader reader = new StringReader("test");
Writer writer = new StringWriter();
Misc.readerToWriter(reader, writer, true);
System.out.println(writer.toString)); // prints "test"
IOExceptionpublic static String fileToString(String fileName) throws IOException
IOExceptionpublic static String fileToString(String fileName, String endOfLineString) throws IOException
IOExceptionpublic static String readerToString(Reader reader, String endOfLineString) throws IOException
Example:
Reader r = new StringReader(" WeAreFrank' \n";
String s = Misc.readerToString(r, "!!", true);
System.out.println(s);
// prints "<root> WeAreFrank!!</root>"
IOExceptionpublic static String streamToString(InputStream stream) throws IOException
IOExceptionpublic static String streamToString(InputStream stream, String streamEncoding) throws IOException
IOExceptionpublic static String streamToString(InputStream stream, String endOfLineString, String streamEncoding) throws IOException
IOExceptionpublic static void stringToFile(String string, String fileName) throws IOException
IOExceptionpublic static String replace(String source, String from, String to)
Example:
String a = "WeAreFrank";
String res = Misc.replace(a, "WeAre", "IAm");
System.out.println(res); // prints "IAmFrank"
source - is the original stringfrom - is the string to be replacedto - is the string which will used to replacepublic static String concatStrings(String part1, String separator, String part2)
Example:
String a = "We";
String b = "Frank";
String separator = "Are";
String res = Misc.concatStrings(a, separator, b);
System.out.println(res); // prints "WeAreFrank"
part1 - First stringseparator - Specified separatorpart2 - Second stringpublic static byte[] gzip(String input) throws IOException
IOExceptiongzip(byte[])public static byte[] gzip(byte[] input)
throws IOException
Example:
String s = "test";
byte[] arr = s.getBytes();
byte[] zipped = Misc.gzip(arr);
System.out.println(Misc.gunzipToString(zipped)); // prints "test"
IOExceptionpublic static byte[] gunzip(byte[] input)
throws DataFormatException,
IOException
Example:
String s = "test";
byte[] arr = s.getBytes();
byte[] zipped = Misc.gzip(arr);
System.out.println(Misc.gunzipToString(zipped)); // prints "test"
DataFormatExceptionIOExceptionpublic static byte[] compress(String input) throws IOException
IOExceptioncompress(byte[])public static byte[] compress(byte[] input)
throws IOException
Example:
String s = "#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$#!@#$";
byte[] compressedSymbols = Misc.compress(s);
assertTrue(compressedSymbols.length < s1.length()); // will assertTrue as compressed one's length should be less than the normal string. However, this may not be the case for shorter strings.
IOExceptionpublic static byte[] decompress(byte[] input)
throws DataFormatException,
IOException
Example:
String s = "test";
byte[] compressed = Misc.compress(s);
String decompressed = Misc.decompressToString(compressed);
System.out.print(decompressed); // prints "test"
DataFormatExceptionIOExceptionpublic static Properties getEnvironmentVariables() throws IOException
IOExceptionpublic static String getHostname()
public static long toFileSize(String value, long defaultValue)
Misc.toFileSize("14GB", 20); // gives out 15032385536public static String listToString(List list)
List list = new ArrayList();
list.add("We Are");
list.add(" Frank");
String res = Misc.listToString(list); // res gives out "We Are Frank"
public static void addItemsToList(Collection<String> collection, String list, String collectionDescription, boolean lowercase)
collectionDescription - description of the listpublic static long parseAge(String value, long defaultValue)
public static String getBuildOutputDirectory()
public static String getProjectBaseDir()
public static int countRegex(String string, String regex)
String s = "12ab34";
String regex = "\\d";
int regexCount = Misc.countRegex(s, regex); // regexCount gives out 4
public static <T> void addToSortedListUnique(List<T> list, T item)
public static <T> void addToSortedListNonUnique(List<T> list, T item)
public static boolean isEmpty(String string)
public static boolean isNotEmpty(String string)
Copyright © 2023 Frank!Framework. All rights reserved.