Class Utils
- java.lang.Object
 - 
- org.bitcoinj.core.Utils
 
 
- 
public class Utils extends java.lang.ObjectA collection of various utility methods that are helpful for working with the Bitcoin protocol. To enable debug logging from the library, run with -Dbitcoinj.logging=true on your command line. 
- 
- 
Field Summary
Fields Modifier and Type Field Description static com.google.common.io.BaseEncodingHEXHex encoding used throughout the framework.static intMAX_INITIAL_ARRAY_LENGTHMax initial size of variable length arrays and ArrayLists that could be attacked.static com.google.common.base.JoinerSPACE_JOINERJoiner for concatenating words with a space inbetween.static com.google.common.base.SplitterWHITESPACE_SPLITTERSplitter for splitting words on whitespaces. 
- 
Constructor Summary
Constructors Constructor Description Utils() 
- 
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]bigIntegerToBytes(java.math.BigInteger b, int numBytes)The regularBigInteger.toByteArray()includes the sign bit of the number and might result in an extra byte addition.static booleancheckBitLE(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 longcurrentTimeMillis()Returns the current time in milliseconds since the epoch, or a mocked out equivalent.static longcurrentTimeSeconds()Returns the current time in seconds since the epoch, or a mocked out equivalent.static java.lang.StringdateTimeFormat(long dateTime)Formats a given date+time value to an ISO 8601 string.static java.lang.StringdateTimeFormat(java.util.Date dateTime)Formats a given date+time value to an ISO 8601 string.static java.math.BigIntegerdecodeCompactBits(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.BigIntegerdecodeMPI(byte[] mpi, boolean hasLength)MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.static longencodeCompactBits(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 voidint64ToByteArrayLE(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 voidint64ToByteStreamLE(long val, java.io.OutputStream stream)Write 8 bytes to the output stream as signed 64-bit integer in little endian format.static booleanisAndroidRuntime()static booleanisLinux()static booleanisMac()static booleanisOpenJDKRuntime()static booleanisOracleJavaRuntime()static booleanisWindows()static java.util.Datenow()Returns the current time, or a mocked out equivalent.static longreadInt64(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 intreadUint16(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 intreadUint16BE(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 intreadUint16FromStream(java.io.InputStream is)Parse 2 bytes from the stream as unsigned 16-bit integer in little endian format.static longreadUint32(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 longreadUint32BE(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 longreadUint32FromStream(java.io.InputStream is)Parse 4 bytes from the stream as unsigned 32-bit integer in little endian format.static voidresetMocking()Clears the mock clock and sleepstatic byte[]reverseBytes(byte[] bytes)Returns a copy of the given byte array in reverse order.static java.util.DaterollMockClock(int seconds)Advances (or rewinds) the mock clock by the given number of seconds.static java.util.DaterollMockClockMillis(long millis)Advances (or rewinds) the mock clock by the given number of milliseconds.static voidsetBitLE(byte[] data, int index)Sets the given bit in data to one, using little endian (not the same as Java native big endian)static voidsetMockClock()Sets the mock clock to the current time.static voidsetMockClock(long mockClockSeconds)Sets the mock clock to the given time (in seconds).static byte[]sha256hash160(byte[] input)Calculates RIPEMD160(SHA256(input)).static java.lang.StringtoString(java.util.List<byte[]> stack)static voiduint16ToByteArrayLE(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 voiduint16ToByteStreamBE(int val, java.io.OutputStream stream)Write 2 bytes to the output stream as unsigned 16-bit integer in big endian format.static voiduint16ToByteStreamLE(int val, java.io.OutputStream stream)Write 2 bytes to the output stream as unsigned 16-bit integer in little endian format.static voiduint32ToByteArrayBE(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 voiduint32ToByteArrayLE(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 voiduint32ToByteStreamBE(long val, java.io.OutputStream stream)Write 4 bytes to the output stream as unsigned 32-bit integer in big endian format.static voiduint32ToByteStreamLE(long val, java.io.OutputStream stream)Write 4 bytes to the output stream as unsigned 32-bit integer in little endian format.static voiduint64ToByteStreamLE(java.math.BigInteger val, java.io.OutputStream stream)Write 8 bytes to the output stream as unsigned 64-bit integer in little endian format. 
 - 
 
- 
- 
Field Detail
- 
SPACE_JOINER
public static final com.google.common.base.Joiner SPACE_JOINER
Joiner for concatenating words with a space inbetween. 
- 
WHITESPACE_SPLITTER
public static final com.google.common.base.Splitter WHITESPACE_SPLITTER
Splitter for splitting words on whitespaces. 
- 
HEX
public static final com.google.common.io.BaseEncoding HEX
Hex encoding used throughout the framework. Use with HEX.encode(byte[]) or HEX.decode(CharSequence). 
- 
MAX_INITIAL_ARRAY_LENGTH
public static final int MAX_INITIAL_ARRAY_LENGTH
Max initial size of variable length arrays and ArrayLists that could be attacked. Avoids this attack: Attacker sends a msg indicating it will contain a huge number (eg 2 billion) elements (eg transaction inputs) and forces bitcoinj to try to allocate a huge piece of the memory resulting in OutOfMemoryError.- See Also:
 - Constant Field Values
 
 
 - 
 
- 
Method Detail
- 
bigIntegerToBytes
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.
- Parameters:
 b- the integer to format into a byte arraynumBytes- the desired size of the resulting byte array- Returns:
 - numBytes byte long array.
 
 
- 
uint16ToByteArrayLE
public 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. 
- 
uint32ToByteArrayLE
public 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. 
- 
uint32ToByteArrayBE
public 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. 
- 
int64ToByteArrayLE
public 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. 
- 
uint16ToByteStreamLE
public static void uint16ToByteStreamLE(int val, java.io.OutputStream stream) throws java.io.IOExceptionWrite 2 bytes to the output stream as unsigned 16-bit integer in little endian format.- Throws:
 java.io.IOException
 
- 
uint16ToByteStreamBE
public static void uint16ToByteStreamBE(int val, java.io.OutputStream stream) throws java.io.IOExceptionWrite 2 bytes to the output stream as unsigned 16-bit integer in big endian format.- Throws:
 java.io.IOException
 
- 
uint32ToByteStreamLE
public static void uint32ToByteStreamLE(long val, java.io.OutputStream stream) throws java.io.IOExceptionWrite 4 bytes to the output stream as unsigned 32-bit integer in little endian format.- Throws:
 java.io.IOException
 
- 
uint32ToByteStreamBE
public static void uint32ToByteStreamBE(long val, java.io.OutputStream stream) throws java.io.IOExceptionWrite 4 bytes to the output stream as unsigned 32-bit integer in big endian format.- Throws:
 java.io.IOException
 
- 
int64ToByteStreamLE
public static void int64ToByteStreamLE(long val, java.io.OutputStream stream) throws java.io.IOExceptionWrite 8 bytes to the output stream as signed 64-bit integer in little endian format.- Throws:
 java.io.IOException
 
- 
uint64ToByteStreamLE
public static void uint64ToByteStreamLE(java.math.BigInteger val, java.io.OutputStream stream) throws java.io.IOExceptionWrite 8 bytes to the output stream as unsigned 64-bit integer in little endian format.- Throws:
 java.io.IOException
 
- 
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. 
- 
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. 
- 
readInt64
public 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. 
- 
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. 
- 
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. 
- 
readUint16FromStream
public static int readUint16FromStream(java.io.InputStream is)
Parse 2 bytes from the stream as unsigned 16-bit integer in little endian format. 
- 
readUint32FromStream
public static long readUint32FromStream(java.io.InputStream is)
Parse 4 bytes from the stream as unsigned 32-bit integer in little endian format. 
- 
reverseBytes
public static byte[] reverseBytes(byte[] bytes)
Returns a copy of the given byte array in reverse order. 
- 
sha256hash160
public static byte[] sha256hash160(byte[] input)
Calculates RIPEMD160(SHA256(input)). This is used in Address calculations. 
- 
decodeMPI
public static java.math.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).- Parameters:
 hasLength- can be set to false if the given array is missing the 4 byte length field
 
- 
encodeMPI
public static byte[] encodeMPI(java.math.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).- Parameters:
 includeLength- indicates whether the 4 byte length field should be included
 
- 
decodeCompactBits
public 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.
 
- 
encodeCompactBits
public static long encodeCompactBits(java.math.BigInteger value)
- See Also:
 decodeCompactBits(long)
 
- 
rollMockClock
public static java.util.Date rollMockClock(int seconds)
Advances (or rewinds) the mock clock by the given number of seconds. 
- 
rollMockClockMillis
public static java.util.Date rollMockClockMillis(long millis)
Advances (or rewinds) the mock clock by the given number of milliseconds. 
- 
setMockClock
public static void setMockClock()
Sets the mock clock to the current time. 
- 
setMockClock
public static void setMockClock(long mockClockSeconds)
Sets the mock clock to the given time (in seconds). 
- 
resetMocking
public static void resetMocking()
Clears the mock clock and sleep 
- 
now
public static java.util.Date now()
Returns the current time, or a mocked out equivalent. 
- 
currentTimeMillis
public static long currentTimeMillis()
Returns the current time in milliseconds since the epoch, or a mocked out equivalent. 
- 
currentTimeSeconds
public static long currentTimeSeconds()
Returns the current time in seconds since the epoch, or a mocked out equivalent. 
- 
dateTimeFormat
public static java.lang.String dateTimeFormat(java.util.Date dateTime)
Formats a given date+time value to an ISO 8601 string.- Parameters:
 dateTime- value to format, as a Date
 
- 
dateTimeFormat
public static java.lang.String dateTimeFormat(long dateTime)
Formats a given date+time value to an ISO 8601 string.- Parameters:
 dateTime- value to format, unix time (ms)
 
- 
checkBitLE
public 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) 
- 
setBitLE
public 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) 
- 
isAndroidRuntime
public static boolean isAndroidRuntime()
 
- 
isOpenJDKRuntime
public static boolean isOpenJDKRuntime()
 
- 
isOracleJavaRuntime
public static boolean isOracleJavaRuntime()
 
- 
isLinux
public static boolean isLinux()
 
- 
isWindows
public static boolean isWindows()
 
- 
isMac
public static boolean isMac()
 
- 
toString
public static java.lang.String toString(java.util.List<byte[]> stack)
 
 - 
 
 -