类 SerializeUtils


  • public class SerializeUtils
    extends Object
    作者:
    tag
    • 方法概要

      所有方法 静态方法 具体方法 
      修饰符和类型 方法 说明
      static byte[] bigDecimal2Bytes​(BigDecimal value)  
      static byte[] bigInteger2Bytes​(BigInteger value)
      将大整数转为字节数组,结果是小端序字节数组 Converting large integers to byte arrays results in small endian byte arrays
      static BigInteger bigIntegerFromBytes​(byte[] array)
      根据字节数组生成对应的大整数 Generate corresponding large integers from byte arrays
      static byte[] bigIntegerToBytes​(BigInteger b, int numBytes)
      The regular BigInteger.toByteArray() method isn't quite what we often need: it appends a leading zero to indicate that the number is positive and may need padding.
      static BigDecimal bytes2BigDecimal​(byte[] bytes)  
      static BigInteger decodeMPI​(byte[] mpi, boolean hasLength)
      MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.
      static void doubleToByteStream​(double val, OutputStream stream)
      将Double类型数据写入输出流
      static byte[] encodeMPI​(BigInteger value, boolean includeLength)
      MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.
      static byte[] formatMessageForSigning​(String message)
      Given a textual message, returns a byte buffer formatted as follows:
      static void int16ToByteArrayLE​(short val, byte[] out, int offset)
      将一个short型数据以小端格式存储到指定字节数组指定的偏移量的连续2个字节中
      static byte[] int16ToBytes​(int x)
      int转byte[] (只转int数据四个字节中的两个字节)
      static void int16ToByteStreamLE​(short val, OutputStream stream)
      Write 2 bytes to the output stream as unsigned 16-bit short in little endian format.
      static void int32ToByteArrayLE​(int val, byte[] out, int offset)
      将一个int型数据以小端格式存储到指定字节数组指定的偏移量的连续4个字节中
      static byte[] int32ToBytes​(int x)
      int转byte[]
      static void int64ToByteStreamLE​(long val, OutputStream stream)
      Write 8 bytes to the output stream as unsigned 64-bit long in little endian format.
      static int readInt32LE​(byte[] bytes, int offset)
      Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format.
      static long readInt64LE​(byte[] bytes, int offset)
      Parse 8 bytes from the byte array (starting at the offset) as unsigned 64-bit integer in little endian format.
      static int readUint16​(byte[] bytes, int offset)
      Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in little endian format.
      static int readUint16BE​(byte[] bytes, int offset)
      Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in big endian format.
      static int readUint16LE​(byte[] bytes, int offset)
      Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in little endian format.
      static long readUint32​(byte[] bytes, int offset)
      Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format.
      static long readUint32BE​(byte[] bytes, int offset)
      Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in big endian format.
      static long readUint32LE​(byte[] bytes, int offset)
      Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format.
      static long readUint48​(byte[] bytes, int offset)
      Parse 6 bytes from the byte array (starting at the offset) as unsigned 48-bit integer in little endian format.
      static long readUint64​(byte[] bytes, int offset)
      Parse 8 bytes from the byte array (starting at the offset) as unsigned 64-bit integer in little endian format.
      static short readUint8LE​(byte[] bytes, int offset)  
      static byte[] sha256hash160​(byte[] input)
      Calculates RIPEMD160(SHA256(input)).
      static int sizeOfBigInteger()
      获取BigInteger数据占的字节数
      static int sizeOfBoolean()
      获取Boolean数据占的字节数
      static int sizeOfBytes​(byte[] val)
      获取byte[]占用的最小字节数(byte[]长度占用的最小字节数+byte[]长度)
      static int sizeOfDouble​(Double val)  
      static int sizeOfInt16()
      获取Int16数据占的字节数
      static int sizeOfInt32()
      获取Int32数据占的字节数
      static int sizeOfInt64()
      获取Int64数据占的字节数
      static int sizeOfNonce()
      获取nonce数据占的字节数
      static int sizeOfNulsData​(NulsData val)
      获取NulsData对象所占字节长度
      static int sizeOfString​(String val)
      字符串转为byte[]的长度
      static int sizeOfUint16()
      获取Uint16数据占的字节数
      static int sizeOfUint32()
      获取Uint32数据占的字节数
      static int sizeOfUint48()
      获取Unit48数据占的字节数
      static int sizeOfUint8()
      获取Uint8数据占的字节数
      static int sizeOfVarInt​(Integer val)
      获取指定Integer占用的最小字节数
      static int sizeOfVarInt​(Long val)
      计算Long型数据占几个字节
      static void uint16ToByteStreamLE​(int val, OutputStream stream)
      Write 2 bytes to the output stream as unsigned 16-bit integer in little endian format.
      static void uint32ToByteArrayBE​(long val, byte[] out, int offset)
      将一个Long型数据以大端格式存储到指定字节数组指定的偏移量的连续4个字节中
      static void uint32ToByteArrayLE​(long val, byte[] out, int offset)
      将一个Long型数据以小端格式存储到指定字节数组指定的偏移量的连续4个字节中
      static void uint32ToByteStreamLE​(long val, OutputStream stream)
      Write 4 bytes to the output stream as unsigned 32-bit long in little endian format.
      static byte[] uint48ToBytes​(long val)
      long数据转byte[](只转前6个字节)
      static byte[] uint64ToByteArray​(long val)
      long数据转byte[]
      static void uint64ToByteArrayLE​(long val, byte[] out, int offset)
      将一个Long型数据以小端格式存储到指定字节数组指定的偏移量的连续8个字节中
      static void uint64ToByteStreamLE​(BigInteger val, OutputStream stream)
      Write 8 bytes to the output stream as unsigned 64-bit BigInteger in little endian format.
      static void uint8ToByteStreamLE​(short val, OutputStream stream)  
    • 字段详细资料

      • CHARSET

        public static final Charset CHARSET
      • SIGNED_MESSAGE_HEADER

        public static final String SIGNED_MESSAGE_HEADER
        The string that prefixes all text messages signed using Bitcoin keys.
        另请参阅:
        常量字段值
      • SIGNED_MESSAGE_HEADER_BYTES

        public static final byte[] SIGNED_MESSAGE_HEADER_BYTES
    • 构造器详细资料

      • SerializeUtils

        public SerializeUtils()
    • 方法详细资料

      • decodeMPI

        public static BigInteger decodeMPI​(byte[] mpi,
                                           boolean hasLength)
        MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function. They consist of a 4 byte big endian length field, followed by the stated number of bytes representing the number in big endian format (with a sign bit).
        参数:
        hasLength - can be set to false if the given array is missing the 4 byte length field
      • encodeMPI

        public static byte[] encodeMPI​(BigInteger value,
                                       boolean includeLength)
        MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function. They consist of a 4 byte big endian length field, followed by the stated number of bytes representing the number in big endian format (with a sign bit).
        参数:
        includeLength - indicates whether the 4 byte length field should be included
      • formatMessageForSigning

        public static byte[] formatMessageForSigning​(String message)
        Given a textual message, returns a byte buffer formatted as follows:

        [24] "Bitcoin Signed Message:\n" [message.length as a varint] message

      • readUint8LE

        public static short readUint8LE​(byte[] bytes,
                                        int offset)
      • readUint16LE

        public static int readUint16LE​(byte[] bytes,
                                       int offset)
        Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in little endian format./从字节数组(以偏移量开始)解析2字节,以小端格式的无符号16位整数
        参数:
        bytes - 字节数组
        offset - 偏移量(数组下标)
      • readUint16BE

        public static int readUint16BE​(byte[] bytes,
                                       int offset)
        Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in big endian format./从字节数组(以偏移量开始)解析2字节,以大端格式的无符号32位整数
        参数:
        bytes - 字节数组
        offset - 偏移量(数组下标)
      • readUint16

        public static int readUint16​(byte[] bytes,
                                     int offset)
        Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in little endian format. 从字节数组(以偏移量开始)解析2字节,以端格式的无符号16位整数
        参数:
        bytes - byte[]
        offset - int偏移量
        返回:
        int
      • readUint32LE

        public static long readUint32LE​(byte[] bytes,
                                        int offset)
        Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format./从字节数组(以偏移量开始)解析4字节,以小端格式的无符号32位整数
        参数:
        bytes - 字节数组
        offset - 偏移量(数组下标)
      • readInt32LE

        public static int readInt32LE​(byte[] bytes,
                                      int offset)
        Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format./从字节数组(以偏移量开始)解析4字节,以小端格式的无符号32位整数
        参数:
        bytes - 字节数组
        offset - 偏移量(数组下标)
      • readUint32BE

        public static long readUint32BE​(byte[] bytes,
                                        int offset)
        Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in big endian format./从字节数组(以偏移量开始)解析4字节,以大端格式的无符号32位整数
        参数:
        bytes - 字节数组
        offset - 偏移量(数组下标)
      • readUint32

        public static long readUint32​(byte[] bytes,
                                      int offset)
        Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format. 从字节数组(以偏移量开始)解析4字节,以端格式的无符号32位整数
        参数:
        bytes - byte[]
        offset - int偏移量
        返回:
        int
      • readUint48

        public static long readUint48​(byte[] bytes,
                                      int offset)
        Parse 6 bytes from the byte array (starting at the offset) as unsigned 48-bit integer in little endian format. 从字节数组(以偏移量开始)解析6字节,以端格式的无符号48位整数
        参数:
        bytes - byte[]
        offset - int偏移量
        返回:
        int
      • readInt64LE

        public static long readInt64LE​(byte[] bytes,
                                       int offset)
        Parse 8 bytes from the byte array (starting at the offset) as unsigned 64-bit integer in little endian format./从字节数组(以偏移量开始)解析8字节,以小端格式的无符号64位整数
        参数:
        bytes - 字节数组
        offset - 偏移量(数组下标)
      • readUint64

        public static long readUint64​(byte[] bytes,
                                      int offset)
        Parse 8 bytes from the byte array (starting at the offset) as unsigned 64-bit integer in little endian format. 从字节数组(以偏移量开始)解析8字节,以端格式的无符号64位整数
        参数:
        bytes - byte[]
        offset - int偏移量
        返回:
        int
      • int16ToBytes

        public static byte[] int16ToBytes​(int x)
        int转byte[] (只转int数据四个字节中的两个字节)
        参数:
        x - int
        返回:
        byte[]
      • int32ToBytes

        public static byte[] int32ToBytes​(int x)
        int转byte[]
        参数:
        x - int
        返回:
        byte[]
      • uint48ToBytes

        public static byte[] uint48ToBytes​(long val)
        long数据转byte[](只转前6个字节)
        参数:
        val - long
        返回:
        byte[]
      • uint64ToByteArray

        public static byte[] uint64ToByteArray​(long val)
        long数据转byte[]
        参数:
        val - long
        返回:
        byte[]
      • sha256hash160

        public static byte[] sha256hash160​(byte[] input)
        Calculates RIPEMD160(SHA256(input)). This is used in Address calculations.
        参数:
        input - 字节数组
      • bigIntegerToBytes

        public static byte[] bigIntegerToBytes​(BigInteger b,
                                               int numBytes)
        The regular BigInteger.toByteArray() method isn't quite what we often need: it appends a leading zero to indicate that the number is positive and may need padding.
        参数:
        b - the integer to format into a byte array
        numBytes - the desired size of the resulting byte array
        返回:
        numBytes byte long array.
      • int16ToByteArrayLE

        public static void int16ToByteArrayLE​(short val,
                                              byte[] out,
                                              int offset)
        将一个short型数据以小端格式存储到指定字节数组指定的偏移量的连续2个字节中
        参数:
        val - short数据
        out - 存放short型数据的字节数组
        offset - 偏移量
      • uint32ToByteArrayBE

        public static void uint32ToByteArrayBE​(long val,
                                               byte[] out,
                                               int offset)
        将一个Long型数据以大端格式存储到指定字节数组指定的偏移量的连续4个字节中
        参数:
        val - long数据
        out - 存放Long型数据的字节数组
        offset - 偏移量
      • uint32ToByteArrayLE

        public static void uint32ToByteArrayLE​(long val,
                                               byte[] out,
                                               int offset)
        将一个Long型数据以小端格式存储到指定字节数组指定的偏移量的连续4个字节中
        参数:
        val - long数据
        out - 存放Long型数据的字节数组
        offset - 偏移量
      • int32ToByteArrayLE

        public static void int32ToByteArrayLE​(int val,
                                              byte[] out,
                                              int offset)
        将一个int型数据以小端格式存储到指定字节数组指定的偏移量的连续4个字节中
        参数:
        val - int数据
        out - 存放int型数据的字节数组
        offset - 偏移量
      • uint64ToByteArrayLE

        public static void uint64ToByteArrayLE​(long val,
                                               byte[] out,
                                               int offset)
        将一个Long型数据以小端格式存储到指定字节数组指定的偏移量的连续8个字节中
        参数:
        val - long数据
        out - 存放Long型数据的字节数组
        offset - 偏移量
      • int16ToByteStreamLE

        public static void int16ToByteStreamLE​(short val,
                                               OutputStream stream)
                                        throws IOException
        Write 2 bytes to the output stream as unsigned 16-bit short in little endian format./将2字节写入输出流作为无符号16位short型数据,以小端格式
        参数:
        val - short数据
        stream - 输出流
        抛出:
        IOException
      • uint16ToByteStreamLE

        public static void uint16ToByteStreamLE​(int val,
                                                OutputStream stream)
                                         throws IOException
        Write 2 bytes to the output stream as unsigned 16-bit integer in little endian format./将2字节写入输出流作为无符号16位整数,以小端格式
        参数:
        val - int数据
        stream - 输出流
        抛出:
        IOException
      • uint32ToByteStreamLE

        public static void uint32ToByteStreamLE​(long val,
                                                OutputStream stream)
                                         throws IOException
        Write 4 bytes to the output stream as unsigned 32-bit long in little endian format./将4字节写入输出流作为无符号32位long型数据,以小端格式
        参数:
        val - long数据
        stream - 输出流
        抛出:
        IOException
      • int64ToByteStreamLE

        public static void int64ToByteStreamLE​(long val,
                                               OutputStream stream)
                                        throws IOException
        Write 8 bytes to the output stream as unsigned 64-bit long in little endian format./将8字节写入输出流作为无符号64位long型数据,以小端格式
        参数:
        val - long数据
        stream - 输出流
        抛出:
        IOException
      • uint64ToByteStreamLE

        public static void uint64ToByteStreamLE​(BigInteger val,
                                                OutputStream stream)
                                         throws IOException
        Write 8 bytes to the output stream as unsigned 64-bit BigInteger in little endian format./将8字节写入输出流作为无符号64位BigInteger型数据,以小端格式
        参数:
        val - BigInteger数据
        stream - 输出流
        抛出:
        IOException
      • doubleToByteStream

        public static void doubleToByteStream​(double val,
                                              OutputStream stream)
                                       throws IOException
        将Double类型数据写入输出流
        参数:
        val - BigInteger数据
        stream - 输出流
        抛出:
        IOException
      • sizeOfDouble

        public static int sizeOfDouble​(Double val)
      • sizeOfString

        public static int sizeOfString​(String val)
        字符串转为byte[]的长度
        参数:
        val - String
        返回:
        int
      • sizeOfVarInt

        public static int sizeOfVarInt​(Long val)
        计算Long型数据占几个字节
        参数:
        val - Long
        返回:
        int
      • sizeOfInt16

        public static int sizeOfInt16()
        获取Int16数据占的字节数
        返回:
        int
      • sizeOfBigInteger

        public static int sizeOfBigInteger()
        获取BigInteger数据占的字节数
        返回:
        int
      • sizeOfUint16

        public static int sizeOfUint16()
        获取Uint16数据占的字节数
        返回:
        int
      • sizeOfUint8

        public static int sizeOfUint8()
        获取Uint8数据占的字节数
        返回:
        int
      • sizeOfInt32

        public static int sizeOfInt32()
        获取Int32数据占的字节数
        返回:
        int
      • sizeOfUint32

        public static int sizeOfUint32()
        获取Uint32数据占的字节数
        返回:
        int
      • sizeOfUint48

        public static int sizeOfUint48()
        获取Unit48数据占的字节数
        返回:
        int
      • sizeOfInt64

        public static int sizeOfInt64()
        获取Int64数据占的字节数
        返回:
        int
      • sizeOfVarInt

        public static int sizeOfVarInt​(Integer val)
        获取指定Integer占用的最小字节数
        参数:
        val - Integer
        返回:
        int
      • sizeOfBoolean

        public static int sizeOfBoolean()
        获取Boolean数据占的字节数
        返回:
        int
      • sizeOfNonce

        public static int sizeOfNonce()
        获取nonce数据占的字节数
        返回:
        int
      • sizeOfBytes

        public static int sizeOfBytes​(byte[] val)
        获取byte[]占用的最小字节数(byte[]长度占用的最小字节数+byte[]长度)
      • sizeOfNulsData

        public static int sizeOfNulsData​(NulsData val)
        获取NulsData对象所占字节长度
        参数:
        val - NulsData
        返回:
        int
      • bigIntegerFromBytes

        public static BigInteger bigIntegerFromBytes​(byte[] array)
        根据字节数组生成对应的大整数 Generate corresponding large integers from byte arrays
        参数:
        array - 小端序的字节数组/Small-endian byte array
        返回:
        大整数
      • bigInteger2Bytes

        public static byte[] bigInteger2Bytes​(BigInteger value)
        将大整数转为字节数组,结果是小端序字节数组 Converting large integers to byte arrays results in small endian byte arrays
        参数:
        value - 大整数、BigInteger
        返回:
        字节数组