Utils

public class Utils

Utility functions.

Fields

EMAIL_PATTERN

public static final Pattern EMAIL_PATTERN

Regular expression pattern that checks a mail for RFC822 compliance.

Taken from: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

This belongs to the perl module “Mail::RFC822::Address”. It is copyright 2001-2002 by Paul Warren. The license can be found here: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address/Mail-RFC822-Address.html#copyright%20and%20license

Methods

InputStream2String

public static String InputStream2String(InputStream is)

Helper function calling InputStream2String for charset UTF-8.

InputStream2String

public static String InputStream2String(InputStream is, String charset)

Return a string from the input stream is up to the next LF (line feed, \A) character.

Parameters:
  • is – The input stream

Returns:

The detected string or an empty string if the stream has become empty.

String2InputStream

public static InputStream String2InputStream(String s)

Helper function to convert a String into an UTF-8 encoded ByteArrayInputStream.

URLDecodeWithUTF8

public static String URLDecodeWithUTF8(String string)

Exception-free decoding of URI-encoded Strings with UTF-8. Any UnsupportedEncodingException will terminate the server since this would by no means be an acceptable run-time environment if UTF-8 is not available.

Parameters:
  • string

Returns:

decoded string.

See also: {@linkUtils.URLEncodeWithUTF8(String)}

URLEncodeWithUTF8

public static String URLEncodeWithUTF8(String string)

Exception-free URI-encoding of strings using UTF-8. Any UnsupportedEncodingException will terminate the server since this would by no means be an acceptable run-time environment if UTF-8 is not available.

Parameters:
  • string

Returns:

encoded string.

See also: {@linkUtils.URLDecodeWithUTF8(String)}

checkPasswordStrength

public static void checkPasswordStrength(String password)

Determines the strength of a password. Currently this just throws an error when the String password does not at least contain - An uppercase char - A lowercase char - A number - A punctuation char and if the length is not at least 8 characters.

correcthorsebatterystaple is also rejected.

Parameters:
  • password – The password to be checked.

document2String

public static String document2String(Document document)

Converts a Document into a String. For debugging purposes.

Parameters:
  • document

Returns:

String representation of the document.

element2String

public static String element2String(Element e)

Converts an Element into a String. For debugging purposes.

Parameters:
  • e – The Element

Returns:

String representation of the element.

getReadableByteSize

public static String getReadableByteSize(Long fssize)

Converts file sizes to human readably sizes.

For example, getReadableByteSize(2048) == “2KiB”.

getSecureFilename

public static String getSecureFilename(int byteSize)

Generate a secure filename (base32 letters and numbers).

Very similar to getUID, but uses cryptographic random number instead, also nicely formats the resulting string.

Parameters:
  • byteSize – How many bytes of random bits shall be generated.

Returns:

The filename as a String.

getUID

public static String getUID()

Generate a UID (Hexadecimal).

Currently this generates two random long numbers and concatenates their hex representations. This function is thread-safe.

Returns:

The UID as a String.

hash

public static String hash(MessageDigest md, String string, String salt, int it)
Parameters:
  • alg – The hashing algorithm implementation.

  • string – The string to be hashed.

  • salt – Cryptographic salt.

  • it – Number of iterations. Zero and negative integers don’t make any sense. Therefore, if it<=1 the hashing is done once.

Returns:

Hashed string, or null if string is null

isRFC822Compliant

public static boolean isRFC822Compliant(String email)

Check if email is RFC822 compliant.

Parameters:
  • email

Returns:

true when the email given by the String email is RFC822 compliant.

log

public static double log(int base, Long x)

Intended to compute the maximum exponent for the required conversion.

sha512

public static String sha512(String string, String salt, int it)

This function returns the SHA512 hash of a given string, salt and number of iterations. This function terminates the application completely with an error code when this hashing algorithm is not available.

Parameters:
  • string – The string to be hashed.

  • salt – Cryptographic salt.

  • it – Number of iterations. Zero and negative integers don’t make any sense. Therefore, if it<=1 the hashing is done once.

Returns:

Hashed string, or null if string is null