public class Utils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static com.google.common.io.BaseEncoding |
HEX
Hex encoding used throughout the framework.
|
static int |
MAX_INITIAL_ARRAY_LENGTH
Max initial size of variable length arrays and ArrayLists that could be attacked.
|
static java.util.Date |
mockTime
If non-null, overrides the return value of now().
|
static com.google.common.base.Joiner |
SPACE_JOINER
Joiner for concatenating words with a space inbetween.
|
static com.google.common.base.Splitter |
WHITESPACE_SPLITTER
Splitter for splitting words on whitespaces.
|
Constructor and Description |
---|
Utils() |
Modifier and Type | Method and Description |
---|---|
static byte[] |
bigIntegerToBytes(java.math.BigInteger b,
int numBytes)
The regular
BigInteger.toByteArray() includes the sign bit of the number and
might result in an extra byte addition. |
static boolean |
checkBitLE(byte[] data,
int index)
Checks if the given bit is set in data, using little endian (not the same as Java native big endian)
|
static long |
currentTimeMillis()
Returns the current time in milliseconds since the epoch, or a mocked out equivalent.
|
static long |
currentTimeSeconds() |
static java.lang.String |
dateTimeFormat(java.util.Date dateTime)
Formats a given date+time value to an ISO 8601 string.
|
static java.lang.String |
dateTimeFormat(long dateTime)
Formats a given date+time value to an ISO 8601 string.
|
static java.math.BigInteger |
decodeCompactBits(long compact)
The "compact" format is a representation of a whole number N using an unsigned 32 bit number similar to a
floating point format.
|
static java.math.BigInteger |
decodeMPI(byte[] mpi,
boolean hasLength)
MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.
|
static long |
encodeCompactBits(java.math.BigInteger value) |
static byte[] |
encodeMPI(java.math.BigInteger value,
boolean includeLength)
MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.
|
static void |
finishMockSleep()
Let the sleeping thread pass the synchronization point any number of times.
|
static void |
int64ToByteArrayLE(long val,
byte[] out,
int offset)
Write 8 bytes to the byte array (starting at the offset) as signed 64-bit integer in little endian format.
|
static void |
int64ToByteStreamLE(long val,
java.io.OutputStream stream)
Write 8 bytes to the output stream as signed 64-bit integer in little endian format.
|
static boolean |
isAndroidRuntime() |
static boolean |
isLinux() |
static boolean |
isMac() |
static boolean |
isOpenJDKRuntime() |
static boolean |
isOracleJavaRuntime() |
static boolean |
isWindows() |
static int |
maxOfMostFreq(int... items) |
static int |
maxOfMostFreq(java.util.List<java.lang.Integer> items) |
static java.util.Date |
now()
Returns the current time, or a mocked out equivalent.
|
static void |
passMockSleep()
Let sleeping thread pass the synchronization point.
|
static long |
readInt64(byte[] bytes,
int offset)
Parse 8 bytes from the byte array (starting at the offset) as signed 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 |
readUint16FromStream(java.io.InputStream is)
Parse 2 bytes from the stream 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 |
readUint32FromStream(java.io.InputStream is)
Parse 4 bytes from the stream as unsigned 32-bit integer in little endian format.
|
static byte[] |
reverseBytes(byte[] bytes)
Returns a copy of the given byte array in reverse order.
|
static java.util.Date |
rollMockClock(int seconds)
Advances (or rewinds) the mock clock by the given number of seconds.
|
static java.util.Date |
rollMockClockMillis(long millis)
Advances (or rewinds) the mock clock by the given number of milliseconds.
|
static void |
setBitLE(byte[] data,
int index)
Sets the given bit in data to one, using little endian (not the same as Java native big endian)
|
static void |
setMockClock()
Sets the mock clock to the current time.
|
static void |
setMockClock(long mockClockSeconds)
Sets the mock clock to the given time (in seconds).
|
static void |
setMockSleep(boolean isEnable)
Enable or disable mock sleep.
|
static byte[] |
sha256hash160(byte[] input)
Calculates RIPEMD160(SHA256(input)).
|
static void |
sleep(long millis)
Sleep for a span of time, or mock sleep if enabled
|
static java.lang.String |
toString(java.util.List<byte[]> stack) |
static void |
uint16ToByteArrayLE(int val,
byte[] out,
int offset)
Write 2 bytes to the byte array (starting at the offset) as unsigned 16-bit integer in little endian format.
|
static void |
uint16ToByteStreamBE(int val,
java.io.OutputStream stream)
Write 2 bytes to the output stream as unsigned 16-bit integer in big endian format.
|
static void |
uint16ToByteStreamLE(int val,
java.io.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)
Write 4 bytes to the byte array (starting at the offset) as unsigned 32-bit integer in big endian format.
|
static void |
uint32ToByteArrayLE(long val,
byte[] out,
int offset)
Write 4 bytes to the byte array (starting at the offset) as unsigned 32-bit integer in little endian format.
|
static void |
uint32ToByteStreamBE(long val,
java.io.OutputStream stream)
Write 4 bytes to the output stream as unsigned 32-bit integer in big endian format.
|
static void |
uint32ToByteStreamLE(long val,
java.io.OutputStream stream)
Write 4 bytes to the output stream as unsigned 32-bit integer in little endian format.
|
static void |
uint64ToByteStreamLE(java.math.BigInteger val,
java.io.OutputStream stream)
Write 8 bytes to the output stream as unsigned 64-bit integer in little endian format.
|
public static final com.google.common.base.Joiner SPACE_JOINER
public static final com.google.common.base.Splitter WHITESPACE_SPLITTER
public static final com.google.common.io.BaseEncoding HEX
public static final int MAX_INITIAL_ARRAY_LENGTH
public static volatile java.util.Date mockTime
public static byte[] bigIntegerToBytes(java.math.BigInteger b, int numBytes)
The regular BigInteger.toByteArray()
includes the sign bit of the number and
might result in an extra byte addition. This method removes this extra byte.
Assuming only positive numbers, it's possible to discriminate if an extra byte is added by checking if the first element of the array is 0 (0000_0000). Due to the minimal representation provided by BigInteger, it means that the bit sign is the least significant bit 0000_0000 . Otherwise the representation is not minimal. For example, if the sign bit is 0000_0000, then the representation is not minimal due to the rightmost zero.
b
- the integer to format into a byte arraynumBytes
- the desired size of the resulting byte arraypublic static void uint16ToByteArrayLE(int val, byte[] out, int offset)
public static void uint32ToByteArrayLE(long val, byte[] out, int offset)
public static void uint32ToByteArrayBE(long val, byte[] out, int offset)
public static void int64ToByteArrayLE(long val, byte[] out, int offset)
public static void uint16ToByteStreamLE(int val, java.io.OutputStream stream) throws java.io.IOException
java.io.IOException
public static void uint16ToByteStreamBE(int val, java.io.OutputStream stream) throws java.io.IOException
java.io.IOException
public static void uint32ToByteStreamLE(long val, java.io.OutputStream stream) throws java.io.IOException
java.io.IOException
public static void uint32ToByteStreamBE(long val, java.io.OutputStream stream) throws java.io.IOException
java.io.IOException
public static void int64ToByteStreamLE(long val, java.io.OutputStream stream) throws java.io.IOException
java.io.IOException
public static void uint64ToByteStreamLE(java.math.BigInteger val, java.io.OutputStream stream) throws java.io.IOException
java.io.IOException
public static int readUint16(byte[] bytes, int offset)
public static long readUint32(byte[] bytes, int offset)
public static long readInt64(byte[] bytes, int offset)
public static long readUint32BE(byte[] bytes, int offset)
public static int readUint16BE(byte[] bytes, int offset)
public static int readUint16FromStream(java.io.InputStream is)
public static long readUint32FromStream(java.io.InputStream is)
public static byte[] reverseBytes(byte[] bytes)
public static byte[] sha256hash160(byte[] input)
public static java.math.BigInteger decodeMPI(byte[] mpi, boolean hasLength)
hasLength
- can be set to false if the given array is missing the 4 byte length fieldpublic static byte[] encodeMPI(java.math.BigInteger value, boolean includeLength)
includeLength
- indicates whether the 4 byte length field should be includedpublic static java.math.BigInteger decodeCompactBits(long compact)
The "compact" format is a representation of a whole number N using an unsigned 32 bit number similar to a floating point format. The most significant 8 bits are the unsigned exponent of base 256. This exponent can be thought of as "number of bytes of N". The lower 23 bits are the mantissa. Bit number 24 (0x800000) represents the sign of N. Therefore, N = (-1^sign) * mantissa * 256^(exponent-3).
Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn(). MPI uses the most significant bit of the first byte as sign. Thus 0x1234560000 is compact 0x05123456 and 0xc0de000000 is compact 0x0600c0de. Compact 0x05c0de00 would be -0x40de000000.
Bitcoin only uses this "compact" format for encoding difficulty targets, which are unsigned 256bit quantities. Thus, all the complexities of the sign bit and using base 256 are probably an implementation accident.
public static long encodeCompactBits(java.math.BigInteger value)
decodeCompactBits(long)
public static java.util.Date rollMockClock(int seconds)
public static java.util.Date rollMockClockMillis(long millis)
public static void setMockClock()
public static void setMockClock(long mockClockSeconds)
public static java.util.Date now()
public static long currentTimeMillis()
public static long currentTimeSeconds()
public static java.lang.String dateTimeFormat(java.util.Date dateTime)
dateTime
- value to format, as a Datepublic static java.lang.String dateTimeFormat(long dateTime)
dateTime
- value to format, unix time (ms)public static boolean checkBitLE(byte[] data, int index)
public static void setBitLE(byte[] data, int index)
public static void sleep(long millis)
public static void setMockSleep(boolean isEnable)
public static void passMockSleep()
public static void finishMockSleep()
public static int maxOfMostFreq(int... items)
public static int maxOfMostFreq(java.util.List<java.lang.Integer> items)
public static boolean isAndroidRuntime()
public static boolean isOpenJDKRuntime()
public static boolean isOracleJavaRuntime()
public static boolean isLinux()
public static boolean isWindows()
public static boolean isMac()
public static java.lang.String toString(java.util.List<byte[]> stack)