Class Conversion

java.lang.Object
com.saperion.common.lang.conversion.Conversion

public final class Conversion extends Object
Utility class for conversion.

Supports the following conversions typically in both directions:

  • hex-char <-> byte
  • char[] of hex-char <-> byte[] (with optional header and spacing)
  • base64-char <-> byte
  • char[] of base64-char <-> byte[]
  • InputStream -> byte[] (only one way; to implement the inverse direction simply construct a ByteArrayInputStream)
  • Serializable <-> byte[]
  • Collection<String> <-> String (with separator and correct escaping)

All these methods are optimized for speed! Do not change them without controlling their performance behavior!

Many methods return a char[] instead of a String as some might expect. This is done out of performance considerations too. If one really needs a String it can be constructed by String.valueOf(char[]). But in many cases the converted result will not be used directly, but instead will be copied into a StringBuilder/StringBuffer or some kind of Writer. In such cases one should use the char[] directly and avoid an intermediate String.

Author:
agz
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte
    convertBase64CharacterToByte(char base64Character)
    Converts the specified a base64 character to a byte value.
    static byte[]
    convertBase64ToByteArray(char[] arBase64Chars)
    Parses the specified char[] assuming that it consists of base64 encoded quadruples of characters and converts it to a byte[].
    static byte[]
    convertBase64ToByteArray(char[] arBase64Chars, int beginIndex, int endIndex)
    Parses the specified char[] section from the specified begin index to the specified end index - 1 assuming that it consists of base64 encoded quadruples of characters and converts it to a byte[].
    static byte[]
    Parses the specified String assuming that it consists of base64 encoded quadruples of characters and converts it to a byte[].
    static byte[]
    convertBase64ToByteArray(String base64String, int beginIndex, int endIndex)
    Parses the specified String section from the specified begin index to the specified end index - 1 assuming that it consists of base64 encoded quadruples of characters and converts it to a byte[].
    static char[]
    convertByteArrayToBase64(byte[] arBytes)
    Returns a char[] containing a base64 encoding that represents the bytes of the specified byte[].
    static char[]
    convertByteArrayToBase64(byte[] arBytes, int beginIndex, int endIndex)
    Returns a char[] containing a base64 encoding that represents the bytes of the specified byte[] section from the specified begin index to the specified end index - 1.
    static char[]
    Returns a char[] containing pairwise hexadecimal digits that represent the bytes of the specified byte[].
    static char[]
    convertByteArrayToHexDigits(byte[] arBytes, char blockSeparator, int blockSize)
    Returns a char[] containing pairwise hexadecimal digits that represent the bytes of the specified byte[] optionally separated at the specified interval of bytes by the specified separator character.
    static char[]
    convertByteArrayToHexDigits(byte[] arBytes, int beginIndex, int endIndex)
    Returns a char[] containing pairwise hexadecimal digits that represent the bytes of the specified byte[] section from the specified begin index to the specified end index - 1.
    static char[]
    convertByteArrayToHexDigits(byte[] arBytes, int beginIndex, int endIndex, char blockSeparator, int blockSize)
    Returns a char[] containing pairwise hexadecimal digits that represent the bytes of the specified byte[] section from the specified begin index to the specified end index - 1 optionally separated at the specified interval of bytes by the specified separator character.
    static char[]
    convertByteArrayToHexDigits(byte[] arBytes, int beginIndex, int endIndex, String header)
    Returns a char[] containing pairwise hexadecimal digits that represent the bytes of the specified byte[] section from the specified begin index to the specified end index - 1 optionally preceded by the specified header string.
    static char[]
    convertByteArrayToHexDigits(byte[] arBytes, int beginIndex, int endIndex, String header, char blockSeparator, int blockSize)
    Returns a char[] containing pairwise hexadecimal digits that represent the bytes of the specified byte[] section from the specified begin index to the specified end index - 1 optionally separated at the specified interval of bytes by the specified separator character and optionally preceded by the specified header string.
    static char[]
    convertByteArrayToHexDigits(byte[] arBytes, String header)
    Returns a char[] containing pairwise hexadecimal digits that represent the bytes of the specified byte[] optionally preceded by the specified header string.
    static char[]
    convertByteArrayToHexDigits(byte[] arBytes, String header, char blockSeparator, int blockSize)
    Returns a char[] containing pairwise hexadecimal digits that represent the bytes of the specified byte[] optionally separated at the specified interval of bytes by the specified separator character and optionally preceded by the specified header string.
    Converts the specified byte[] into a Serializable using standard deserialization.
    static byte[]
    convertHexDigitsToByteArray(char[] arHexChars)
    Parses the specified char[] assuming that it consists of pairs of hexadecimal digits optionally separated by other characters.
    static byte[]
    convertHexDigitsToByteArray(char[] arHexChars, int beginIndex)
    Parses the specified char[] section from the specified begin index to its end assuming that it consists of pairs of hexadecimal digits optionally separated by other characters.
    static byte[]
    convertHexDigitsToByteArray(char[] arHexChars, int beginIndex, int endIndex)
    Parses the specified char[] section from the specified begin index to the specified end index - 1 assuming that it consists of pairs of hexadecimal digits optionally separated by other characters.
    static byte[]
    Parses the specified String assuming that it consists of pairs of hexadecimal digits optionally separated by other characters.
    static byte[]
    convertHexDigitsToByteArray(String hexString, int beginIndex)
    Parses the specified String section from the specified begin index to its end assuming that it consists of pairs of hexadecimal digits optionally separated by other characters.
    static byte[]
    convertHexDigitsToByteArray(String hexString, int beginIndex, int endIndex)
    Parses the specified String section from the specified begin index to the specified end index - 1 assuming that it consists of pairs of hexadecimal digits optionally separated by other characters.
    static byte
    Converts the specified a hexadecimal digit character to a byte value.
    static byte[]
    static char
    Converts the specified integer value to a base64 character.
    static char
    Converts the specified integer value to a hexadecimal digit character.
    static byte[]
    convertSerializableToByteArray(Serializable serializable, int sizeGuess)
    Converts the specified Serializable into a byte[] using standard serialization.
    static String
    convertStringCollectionToString(Collection<String> colStrings, char separator, char escape, String nullString)
    Converts the specified collection of strings to a string using the specified separator, escape character and string for null-elements.
    static List<String>
    convertStringToStringList(String string, char separator, char escape, String nullString)
    Converts the specified string to a list of strings using the specified separator, escape character and string for null-elements.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait