Class Encoder


  • public class Encoder
    extends Object
    • Field Detail

      • UINT64_LENGTH

        public static final int UINT64_LENGTH
        The length of an encoded uint64, in bytes.
        See Also:
        Constant Field Values
      • MAX_UINT64

        public static final BigInteger MAX_UINT64
        The maximum value that a uint64 can contain.
    • Constructor Detail

      • Encoder

        public Encoder()
    • Method Detail

      • encodeToMsgPack

        public static byte[] encodeToMsgPack​(Object o)
                                      throws com.fasterxml.jackson.core.JsonProcessingException
        Convenience method for serializing arbitrary objects.
        Returns:
        serialized object
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - if serialization failed
      • decodeFromMsgPack

        public static <T> T decodeFromMsgPack​(String input,
                                              Class<T> tClass)
                                       throws IOException
        Convenience method for deserializing arbitrary objects encoded with canonical msg-pack
        Type Parameters:
        T - object type
        Parameters:
        input - base64 encoded byte array representing canonical msg-pack encoding
        tClass - class of type of object to deserialize as
        Returns:
        deserialized object
        Throws:
        IOException - if decoding failed
      • decodeFromMsgPack

        public static <T> T decodeFromMsgPack​(byte[] input,
                                              Class<T> tClass)
                                       throws IOException
        Convenience method for deserializing arbitrary objects encoded with canonical msg-pack
        Type Parameters:
        T - object type
        Parameters:
        input - byte array representing canonical msg-pack encoding
        tClass - class of type of object to deserialize as
        Returns:
        deserialized object
        Throws:
        IOException - if decoding failed
      • encodeToJson

        public static String encodeToJson​(Object o)
                                   throws com.fasterxml.jackson.core.JsonProcessingException
        Encode an object as json.
        Parameters:
        o - object to encode
        Returns:
        json string
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - error
      • decodeFromJson

        public static <T> T decodeFromJson​(String input,
                                           Class<T> tClass)
                                    throws IOException
        Encode an object as json.
        Parameters:
        input - json string to decode
        tClass - class to decode the json string into
        Returns:
        object specified by tClass
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - error
        IOException
      • encodeToHexStr

        public static String encodeToHexStr​(byte[] bytes)
        Convenience method for writing bytes as hex.
        Parameters:
        bytes - input to encodeToMsgPack as hex string
        Returns:
        encoded hex string
      • decodeFromHexStr

        public static byte[] decodeFromHexStr​(String hexStr)
                                       throws org.apache.commons.codec.DecoderException
        Convenience method for decoding bytes from hex.
        Parameters:
        hexStr - hex string to decode
        Returns:
        byte array
        Throws:
        org.apache.commons.codec.DecoderException
      • encodeToBase32StripPad

        public static String encodeToBase32StripPad​(byte[] bytes)
        Convenience method for writing bytes as base32
        Parameters:
        bytes - input
        Returns:
        base32 string with stripped whitespace
      • decodeFromBase32StripPad

        public static byte[] decodeFromBase32StripPad​(String base32)
        Convenience method for reading base32 back into bytes
        Parameters:
        base32 - input string with optional padding.
        Returns:
        bytes for base32 data
      • encodeToBase64

        public static String encodeToBase64​(byte[] bytes)
        Encode to base64 string. Does not strip padding.
        Parameters:
        bytes - input
        Returns:
        base64 string with appropriate padding
      • decodeFromBase64

        public static byte[] decodeFromBase64​(String str)
        Decode from base64 string.
        Parameters:
        str - input
        Returns:
        decoded bytes
      • encodeUint64

        public static byte[] encodeUint64​(BigInteger value)
        Encode an non-negative integer as a big-endian uint64 value.
        Parameters:
        value - The value to encode.
        Returns:
        A byte array containing the big-endian encoding of the value. Its length will be Encoder.UINT64_LENGTH.
        Throws:
        IllegalArgumentException - if value is negative.
      • encodeUint64

        public static byte[] encodeUint64​(long value)
        Encode an non-negative integer as a big-endian uint64 value.
        Parameters:
        value - The value to encode.
        Returns:
        A byte array containing the big-endian encoding of the value. Its length will be Encoder.UINT64_LENGTH.
        Throws:
        IllegalArgumentException - if value is negative.
      • decodeUint64

        public static BigInteger decodeUint64​(byte[] encoded)
        Decode an encoded big-endian uint64 value.
        Parameters:
        encoded - The encoded uint64 value. Its length must be Encoder.UINT64_LENGTH.
        Returns:
        The decoded value.
        Throws:
        IllegalArgumentException - if encoded is the wrong length.
      • encodeUintToBytes

        public static byte[] encodeUintToBytes​(BigInteger value,
                                               int byteNum)
        Encode an non-negative integer as a big-endian general uint value.
        Parameters:
        value - The value to encode.
        byteNum - The size of output bytes.
        Returns:
        A byte array containing the big-endian encoding of the value. Its length will be byteNum.
        Throws:
        IllegalArgumentException - if value cannot be represented by the byte array of length byteNum.
      • decodeBytesToUint

        public static BigInteger decodeBytesToUint​(byte[] encoded)
        Decode an encoded big-endian uint value.
        Parameters:
        encoded - The encoded bytes for the value.
        Returns:
        A byte array containing the big-endian encoding of the value. Its length will be Encoder.UINT64_LENGTH.