Package org.mycore.common
Class MCRUtils
java.lang.Object
org.mycore.common.MCRUtils
This class represent a general set of external methods to support the programming API.
- Version:
- $Revision$ $Date$
- Author:
- Jens Kupferschmidt, Frank Lützenkirchen, Thomas Scheffler (yagee)
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
asCryptString
(String salt, String text) static String
asMD5String
(int iterations, byte[] salt, String text) static String
asSHA1String
(int iterations, byte[] salt, String text) static String
asSHA256String
(int iterations, byte[] salt, String text) static <T> int
compareParts
(T first, T other, Function<T, Comparable>... part) Helps to implementComparable.compareTo(Object)
For everypart
a check is performed in the specified order.filterTrimmedNotEmpty
(String value) Checks is trimmedvalue
is not empty.getAllDirectoryNames
(File basedir) The method return a list of all directory names under the given directory and subdirectories of itself.getAllDirectoryNames
(File basedir, String path) The method return a list of all directory names under the given directory and subdirectories of itself.getAllFileNames
(File basedir) The method return a list of all file names under the given directory and subdirectories of itself.getAllFileNames
(File basedir, String path) The method return a list of all file names under the given directory and subdirectories of itself.static String
getMD5Sum
(InputStream inputStream) Calculates md5 sum of InputStream.static String
getSizeFormatted
(long bytes) Takes a file size in bytes and formats it as a string for output.static String
static <T extends Throwable>
voidmeasure
(TimeUnit unit, Consumer<Long> timeHandler, MCRThrowableTask<T> task) Measures the time of a method call.measure
(MCRThrowableTask<T> task) Measures and logs the time of a method callstatic String
static int
readBlocking
(InputStream in, byte[] b, int off, int len) Reads exactlylen
bytes from the input stream into the byte array.static int
readBlocking
(Reader in, char[] c, int off, int len) Reads exactlylen
bytes from the input stream into the byte array.static Path
safeResolve
(Path basePath, String... resolve) static Path
safeResolve
(Path basePath, Path resolve) static String
toHexString
(byte[] data) static void
Extracts files in a tar archive.static Exception
unwrapExCeption
(Exception e, Class<? extends Exception>... classes) static Path
writeTextToFile
(String textToWrite, String fileName, Charset cs) Writes plain text to a file.
-
Constructor Details
-
MCRUtils
public MCRUtils()
-
-
Method Details
-
readBlocking
Reads exactlylen
bytes from the input stream into the byte array. This method reads repeatedly from the underlying stream until all the bytes are read. InputStream.read is often documented to block like this, but in actuality it does not always do so, and returns early with just a few bytes. readBlockiyng blocks until all the bytes are read, the end of the stream is detected, or an exception is thrown. You will always get as many bytes as you asked for unless you get an eof or other exception. Unlike readFully, you find out how many bytes you did get.- Parameters:
b
- the buffer into which the data is read.off
- the start offset of the data.len
- the number of bytes to read.- Returns:
- number of bytes actually read.
- Throws:
IOException
- if an I/O error occurs.
-
readBlocking
Reads exactlylen
bytes from the input stream into the byte array. This method reads repeatedly from the underlying stream until all the bytes are read. Reader.read is often documented to block like this, but in actuality it does not always do so, and returns early with just a few bytes. readBlockiyng blocks until all the bytes are read, the end of the stream is detected, or an exception is thrown. You will always get as many bytes as you asked for unless you get an eof or other exception. Unlike readFully, you find out how many bytes you did get.- Parameters:
c
- the buffer into which the data is read.off
- the start offset of the data.len
- the number of bytes to read.- Returns:
- number of bytes actually read.
- Throws:
IOException
- if an I/O error occurs.
-
writeTextToFile
public static Path writeTextToFile(String textToWrite, String fileName, Charset cs) throws IOException Writes plain text to a file.- Parameters:
textToWrite
- the text to write into the filefileName
- the name of the file to write to, given as absolute path- Returns:
- a handle to the written file
- Throws:
IOException
-
getAllFileNames
The method return a list of all file names under the given directory and subdirectories of itself.- Parameters:
basedir
- the File instance of the basic directory- Returns:
- an ArrayList with file names as pathes
-
getAllFileNames
The method return a list of all file names under the given directory and subdirectories of itself.- Parameters:
basedir
- the File instance of the basic directorypath
- the part of directory path- Returns:
- an ArrayList with file names as pathes
-
getAllDirectoryNames
The method return a list of all directory names under the given directory and subdirectories of itself.- Parameters:
basedir
- the File instance of the basic directory- Returns:
- an ArrayList with directory names as pathes
-
getAllDirectoryNames
The method return a list of all directory names under the given directory and subdirectories of itself.- Parameters:
basedir
- the File instance of the basic directorypath
- the part of directory path- Returns:
- an ArrayList with directory names as pathes
-
parseDocumentType
-
asSHA1String
public static String asSHA1String(int iterations, byte[] salt, String text) throws NoSuchAlgorithmException - Throws:
NoSuchAlgorithmException
-
asSHA256String
public static String asSHA256String(int iterations, byte[] salt, String text) throws NoSuchAlgorithmException - Throws:
NoSuchAlgorithmException
-
asMD5String
public static String asMD5String(int iterations, byte[] salt, String text) throws NoSuchAlgorithmException - Throws:
NoSuchAlgorithmException
-
asCryptString
-
toHexString
-
getMD5Sum
Calculates md5 sum of InputStream. InputStream is consumed after calling this method and automatically closed.- Throws:
IOException
-
untar
Extracts files in a tar archive. Currently works only on uncompressed tar files.- Parameters:
source
- the uncompressed tar to extractexpandToDirectory
- the directory to extract the tar file to- Throws:
IOException
- if the source file does not exists
-
unwrapExCeption
@SafeVarargs public static Exception unwrapExCeption(Exception e, Class<? extends Exception>... classes) -
getSizeFormatted
Takes a file size in bytes and formats it as a string for output. For values < 5 KB the output format is for example "320 Byte". For values > 5 KB the output format is for example "6,8 KB". For values > 1 MB the output format is for example "3,45 MB". -
compareParts
Helps to implementComparable.compareTo(Object)
For everypart
a check is performed in the specified order. The first check that does not return0
the result is returned by this method. So when this method returns0
first
andother
should be the same.- Type Parameters:
T
- object that wants to implement compareTo()- Parameters:
first
- first Object that should be comparedother
- Object that first should be compared against, e.g.first.compareTo(other)
part
- differentcompareTo()
steps- Returns:
- a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
- Throws:
NullPointerException
- if eitherfirst
orother
is null
-
getStackTraceAsString
- Parameters:
t
- contains the printStackTrace- Returns:
- the stacktrace as string
-
filterTrimmedNotEmpty
Checks is trimmedvalue
is not empty.- Parameters:
value
- String to test- Returns:
- empty if value is
null
or empty after trimming.
-
measure
public static <T extends Throwable> void measure(TimeUnit unit, Consumer<Long> timeHandler, MCRThrowableTask<T> task) throws T Measures the time of a method call. timeHandler is guaranteed to be called even if exception is thrown.- Parameters:
unit
- time unit for timeHandlertimeHandler
- gets the duration inunit
task
- method reference- Throws:
T
- if task.run() throws Exception
-
measure
Measures and logs the time of a method call- Parameters:
task
- method reference- Throws:
T
- if task.run() throws Exception
-
safeResolve
-
safeResolve
-