1 package org.openslx.imagemaster.util;
 
    4 import java.math.BigInteger;
 
    5 import java.security.Key;
 
    6 import java.security.interfaces.RSAPrivateKey;
 
    7 import java.security.interfaces.RSAPublicKey;
 
    8 import java.util.Arrays;
 
    9 import java.util.Random;
 
   10 import java.util.UUID;
 
   12 import org.apache.logging.log4j.LogManager;
 
   13 import org.apache.logging.log4j.Logger;
 
   21    private static Logger 
LOGGER = LogManager.getLogger( 
Util.class );
 
   36    public static void notNullFatal( Object something, String message )
 
   38       if ( something == null ) {
 
   39          if ( message != null )
 
   40             LOGGER.fatal( 
"[NOTNULL] " + message, 
new NullPointerException() );
 
   61          if ( message != null )
 
   62             LOGGER.fatal( 
"[NOTNULL] " + message, 
new NullPointerException() );
 
   78       if ( something == null || something.isEmpty() ) {
 
   79          if ( message != null )
 
   80             LOGGER.fatal( 
"[NOTNULL] " + message, 
new NullPointerException() );
 
   88    private static final Random 
random = 
new Random();
 
  100       return random.nextInt( n );
 
  110       File[] files = folder.listFiles();
 
  111       if ( files != null ) {
 
  112          for ( File f : files ) {
 
  113             if ( f.isDirectory() ) {
 
  125       return str == null || str.isEmpty();
 
  128    public static boolean isEmpty( String str, String message, Logger logger )
 
  130       if ( str != null && !str.isEmpty() )
 
  132       logger.debug( message );
 
  157          return Integer.parseInt( s );
 
  158       } 
catch ( NumberFormatException e ) {
 
  172          return new BigInteger( s );
 
  173       } 
catch ( NumberFormatException e ) {
 
  180       int blocks = (int) ( fileSize / blockSize );
 
  181       if ( fileSize % blockSize != 0 )
 
  188       fileName = fileName.replaceAll( 
"[^a-zA-Z0-9_\\-]+", 
"_" );
 
  189       if ( fileName.length() > 40 )
 
  190          fileName = fileName.substring( 0, 40 );
 
  204       if ( k1 instanceof RSAPublicKey && k2 instanceof RSAPublicKey )
 
  206          RSAPublicKey rsa1 = (RSAPublicKey)k1;
 
  207          RSAPublicKey rsa2 = (RSAPublicKey)k2;
 
  208          return rsa1.getModulus().equals( rsa2.getModulus() )
 
  209                && rsa1.getPublicExponent().equals( rsa2.getPublicExponent() );
 
  212       if ( k1 instanceof RSAPrivateKey && k2 instanceof RSAPrivateKey )
 
  214          RSAPrivateKey rsa1 = (RSAPrivateKey)k1;
 
  215          RSAPrivateKey rsa2 = (RSAPrivateKey)k2;
 
  216          return rsa1.getModulus().equals( rsa2.getModulus() )
 
  217                && rsa1.getPrivateExponent().equals( rsa2.getPrivateExponent() );
 
  219       return Arrays.equals( k1.getEncoded(), k2.getEncoded() );
 
  227          file = absolutePath.getCanonicalPath();
 
  228          dir = parentDir.getCanonicalPath() + File.separator;
 
  229       } 
catch ( Exception e ) {
 
  230          LOGGER.error( 
"Could not get relative path for " + absolutePath.toString(), e );
 
  233       if ( !file.startsWith( dir ) )
 
  235       return file.substring( dir.length() );
 
  238    public static boolean isUUID( String 
id )
 
  241          UUID.fromString( 
id );
 
  242       } 
catch ( Exception e ) {
 
static int tryToParseInt(String s, int defaultValue)
Tries to parse an int. 
static String getRelativePath(File absolutePath, File parentDir)
static void deleteFolder(File folder)
Remove a folder and all contents. 
static BigInteger tryToParseBigInt(String s)
Tries to parse a bigint. 
static int getNumberOfBlocks(long fileSize, int blockSize)
static boolean isUUID(String id)
static void notNullFatal(int number, String message)
Check if the given object is null, abort program if true. 
static int randomInt(int n)
Return a random integer in the range of 0 (inclusive) and n (exclusive). 
static final Random random
Static Random instance. 
Some utilities to make our lives easier. 
static void notNullFatal(Object something, String message)
Check if the given object is null, abort program if true. 
static String sanitizeFileName(String fileName)
static int tryToParseInt(String s)
Tries to parse an int. 
static boolean isEmpty(String str)
static boolean isEmpty(String str, String message, Logger logger)
static boolean keysEqual(Key k1, Key k2)
Checks whether the two given keys are equal. 
static void notNullOrEmptyFatal(String something, String message)
Check if String is null or empty, abort program if so.