Class MCRUtils

java.lang.Object
org.mycore.common.MCRUtils

public class MCRUtils extends Object
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 Details

    • MCRUtils

      public MCRUtils()
  • Method Details

    • readBlocking

      public static int readBlocking(InputStream in, byte[] b, int off, int len) throws IOException
      Reads exactly len 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

      public static int readBlocking(Reader in, char[] c, int off, int len) throws IOException
      Reads exactly len 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 file
      fileName - the name of the file to write to, given as absolute path
      Returns:
      a handle to the written file
      Throws:
      IOException
    • getAllFileNames

      public static ArrayList<String> getAllFileNames(File basedir)
      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

      public static ArrayList<String> getAllFileNames(File basedir, String path)
      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
      path - the part of directory path
      Returns:
      an ArrayList with file names as pathes
    • getAllDirectoryNames

      public static ArrayList<String> getAllDirectoryNames(File basedir)
      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

      public static ArrayList<String> getAllDirectoryNames(File basedir, String path)
      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
      path - the part of directory path
      Returns:
      an ArrayList with directory names as pathes
    • parseDocumentType

      public static String parseDocumentType(InputStream in)
    • 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

      public static String asCryptString(String salt, String text)
    • toHexString

      public static String toHexString(byte[] data)
    • getMD5Sum

      public static String getMD5Sum(InputStream inputStream) throws IOException
      Calculates md5 sum of InputStream. InputStream is consumed after calling this method and automatically closed.
      Throws:
      IOException
    • untar

      public static void untar(Path source, Path expandToDirectory) throws IOException
      Extracts files in a tar archive. Currently works only on uncompressed tar files.
      Parameters:
      source - the uncompressed tar to extract
      expandToDirectory - 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

      public static String getSizeFormatted(long bytes)
      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

      @SafeVarargs public static <T> int compareParts(T first, T other, Function<T,Comparable>... part)
      Helps to implement Comparable.compareTo(Object) For every part a check is performed in the specified order. The first check that does not return 0 the result is returned by this method. So when this method returns 0 first and other should be the same.
      Type Parameters:
      T - object that wants to implement compareTo()
      Parameters:
      first - first Object that should be compared
      other - Object that first should be compared against, e.g. first.compareTo(other)
      part - different compareTo() 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 either first or other is null
    • getStackTraceAsString

      public static String getStackTraceAsString(Throwable t)
      Parameters:
      t - contains the printStackTrace
      Returns:
      the stacktrace as string
    • filterTrimmedNotEmpty

      public static Optional<String> filterTrimmedNotEmpty(String value)
      Checks is trimmed value 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 timeHandler
      timeHandler - gets the duration in unit
      task - method reference
      Throws:
      T - if task.run() throws Exception
    • measure

      public static <T extends Throwable> Duration measure(MCRThrowableTask<T> task) throws T
      Measures and logs the time of a method call
      Parameters:
      task - method reference
      Throws:
      T - if task.run() throws Exception
    • safeResolve

      public static Path safeResolve(Path basePath, Path resolve)
    • safeResolve

      public static Path safeResolve(Path basePath, String... resolve)