Package com.algorand.algosdk.util
Class Encoder
- java.lang.Object
-
- com.algorand.algosdk.util.Encoder
-
public class Encoder extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static BigInteger
MAX_UINT64
The maximum value that a uint64 can contain.static int
UINT64_LENGTH
The length of an encoded uint64, in bytes.
-
Constructor Summary
Constructors Constructor Description Encoder()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static BigInteger
decodeBytesToUint(byte[] encoded)
Decode an encoded big-endian uint value.static byte[]
decodeFromBase32StripPad(String base32)
Convenience method for reading base32 back into bytesstatic byte[]
decodeFromBase64(String str)
Decode from base64 string.static byte[]
decodeFromHexStr(String hexStr)
Convenience method for decoding bytes from hex.static <T> T
decodeFromJson(String input, Class<T> tClass)
Encode an object as json.static <T> T
decodeFromMsgPack(byte[] input, Class<T> tClass)
Convenience method for deserializing arbitrary objects encoded with canonical msg-packstatic <T> T
decodeFromMsgPack(String input, Class<T> tClass)
Convenience method for deserializing arbitrary objects encoded with canonical msg-packstatic BigInteger
decodeUint64(byte[] encoded)
Decode an encoded big-endian uint64 value.static String
encodeToBase32StripPad(byte[] bytes)
Convenience method for writing bytes as base32static String
encodeToBase64(byte[] bytes)
Encode to base64 string.static String
encodeToHexStr(byte[] bytes)
Convenience method for writing bytes as hex.static String
encodeToJson(Object o)
Encode an object as json.static byte[]
encodeToMsgPack(Object o)
Convenience method for serializing arbitrary objects.static byte[]
encodeUint64(long value)
Encode an non-negative integer as a big-endian uint64 value.static byte[]
encodeUint64(BigInteger value)
Encode an non-negative integer as a big-endian uint64 value.static byte[]
encodeUintToBytes(BigInteger value, int byteNum)
Encode an non-negative integer as a big-endian general uint value.
-
-
-
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.
-
-
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 encodingtClass
- 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 encodingtClass
- 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 decodetClass
- class to decode the json string into- Returns:
- object specified by tClass
- Throws:
com.fasterxml.jackson.core.JsonProcessingException
- errorIOException
-
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.
-
-