Class ByteUtils
org.bitcoinj.core.Utils
.-
Field Summary
Modifier and TypeFieldDescriptionstatic final com.google.common.io.BaseEncoding
Deprecated.static final long
Maximum unsigned value that can be expressed by 32 bits.static final int
Maximum unsigned value that can be expressed by 16 bits. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Comparator<byte[]>
Provides a byte array comparator.static byte[]
bigIntegerToBytes
(BigInteger b, int numBytes) The built-inBigInteger.toByteArray()
includes the sign bit of the number and may result in an extra byte in cases of unsigned data.static BigInteger
bytesToBigInteger
(byte[] bytes) Converts an array of bytes into a positive BigInteger.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 byte[]
concat
(byte[] b1, byte[] b2) Concatenate two byte arraysstatic 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 BigInteger
decodeMPI
(byte[] mpi, boolean hasLength) MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.static long
encodeCompactBits
(BigInteger value) static byte[]
encodeMPI
(BigInteger value, boolean includeLength) MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.static String
formatHex
(byte[] bytes) static byte[]
static int
readInt32
(ByteBuffer buf) Read 4 bytes from the byte array (starting at the offset) as signed 32-bit integer in little endian format.static long
readInt64
(byte[] bytes, int offset) Read 8 bytes from the byte array (starting at the offset) as signed 64-bit integer in little endian format.static long
readInt64
(ByteBuffer buf) Read 8 bytes from the buffer as signed 64-bit integer in little endian format.static int
readUint16
(byte[] bytes, int offset) Read 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in little endian format.static int
Read 2 bytes from the stream as unsigned 16-bit integer in little endian format.static int
readUint16
(ByteBuffer buf) Read 2 bytes from the buffer as unsigned 16-bit integer in little endian format.static int
readUint16BE
(byte[] bytes, int offset) Read 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in big endian format.static int
readUint16BE
(ByteBuffer buf) Read 2 bytes from the buffer as unsigned 16-bit integer in big endian format.static long
readUint32
(byte[] bytes, int offset) Read 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format.static long
Read 4 bytes from the stream as unsigned 32-bit integer in little endian format.static long
readUint32
(ByteBuffer buf) Read 4 bytes from the buffer as unsigned 32-bit integer in little endian format.static long
readUint32BE
(byte[] bytes, int offset) Read 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in big endian format.static long
readUint32BE
(ByteBuffer buf) Read 4 bytes from the buffer as unsigned 32-bit integer in big endian format.static byte[]
reverseBytes
(byte[] bytes) Returns a copy of the given byte array in reverse order.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
writeInt16BE
(int val, OutputStream stream) Write a 16-bit integer to a given output stream in big-endian format.static ByteBuffer
writeInt16BE
(int val, ByteBuffer buf) Write a 16-bit integer to a given buffer in big-endian format.static void
writeInt16LE
(int val, OutputStream stream) Write a 16-bit integer to a given output stream in little-endian format.static ByteBuffer
writeInt16LE
(int val, ByteBuffer buf) Write a 16-bit integer to a given buffer in little-endian format.static void
writeInt32BE
(int val, byte[] out, int offset) Write a 32-bit integer to a given byte array in big-endian format, starting at a given offset.static void
writeInt32BE
(int val, OutputStream stream) Write a 32-bit integer to a given output stream in big-endian format.static ByteBuffer
writeInt32BE
(int val, ByteBuffer buf) Write a 32-bit integer to a given buffer in big-endian format.static void
writeInt32LE
(int val, OutputStream stream) Write a 32-bit integer to a given output stream in little-endian format.static ByteBuffer
writeInt32LE
(int val, ByteBuffer buf) Write a 32-bit integer to a given buffer in little-endian format.static void
writeInt32LE
(long val, byte[] out, int offset) Write a 32-bit integer to a given byte array in little-endian format, starting at a given offset.static void
writeInt32LE
(long val, OutputStream stream) Write a 32-bit integer to a given output stream in little-endian format.static ByteBuffer
writeInt32LE
(long val, ByteBuffer buf) Write a 32-bit integer to a given buffer in little-endian format.static void
writeInt64LE
(long val, byte[] out, int offset) Write a 64-bit integer to a given byte array in little-endian format, starting at a given offset.static void
writeInt64LE
(long val, OutputStream stream) Write a 64-bit integer to a given output stream in little-endian format.static ByteBuffer
writeInt64LE
(long val, ByteBuffer buf) Write a 64-bit integer to a given buffer in little-endian format.static void
writeInt64LE
(BigInteger val, OutputStream stream) Write a 64-bit integer to a given output stream in little-endian format.
-
Field Details
-
MAX_UNSIGNED_SHORT
public static final int MAX_UNSIGNED_SHORTMaximum unsigned value that can be expressed by 16 bits. -
MAX_UNSIGNED_INTEGER
public static final long MAX_UNSIGNED_INTEGERMaximum unsigned value that can be expressed by 32 bits. -
HEX
Deprecated.UsehexFormat
orparseHex(String)
or other available options.Hex encoding used throughout the framework. Use with ByteUtils.formatHex(byte[]) or ByteUtils.parseHex(CharSequence).
-
-
Constructor Details
-
ByteUtils
public ByteUtils()
-
-
Method Details
-
formatHex
-
parseHex
-
bigIntegerToBytes
The built-in
BigInteger.toByteArray()
includes the sign bit of the number and may result in an extra byte in cases of unsigned data. This method removes this extra byte.Assuming only positive numbers, it's possible to tell if an extra byte was added by checking if the first element of the array is 0 (0000_0000). Due to the guarantee of a minimal representation provided by BigInteger, we know that the sign bit will be the least significant bit 0000_0000 of a zero-value first byte. Otherwise the representation would not be minimal.
This is the inverse ofbytesToBigInteger(byte[])
.- Parameters:
b
- the non-negative integer to format into a byte arraynumBytes
- the maximum allowed size of the resulting byte array- Returns:
- byte array of max length
numBytes
-
bytesToBigInteger
Converts an array of bytes into a positive BigInteger. This is the inverse ofbigIntegerToBytes(BigInteger, int)
.- Parameters:
bytes
- to convert into a BigInteger- Returns:
- the converted BigInteger
-
writeInt16LE
Write a 16-bit integer to a given buffer in little-endian format.The value is expected as an unsigned
int
as per the Java Unsigned Integer API.- Parameters:
val
- value to be writtenbuf
- buffer to be written into- Returns:
- the buffer
- Throws:
BufferOverflowException
- if the value doesn't fit the remaining buffer
-
writeInt16BE
Write a 16-bit integer to a given buffer in big-endian format.The value is expected as an unsigned
int
as per the Java Unsigned Integer API.- Parameters:
val
- value to be writtenbuf
- buffer to be written into- Returns:
- the buffer
- Throws:
BufferOverflowException
- if the value doesn't fit the remaining buffer
-
writeInt32LE
Write a 32-bit integer to a given buffer in little-endian format.The value is expected as a signed or unsigned
int
. If you've got an unsignedlong
as per the Java Unsigned Integer API, usewriteInt32LE(long, ByteBuffer)
.- Parameters:
val
- value to be writtenbuf
- buffer to be written into- Returns:
- the buffer
- Throws:
BufferOverflowException
- if the value doesn't fit the remaining buffer
-
writeInt32LE
Write a 32-bit integer to a given buffer in little-endian format.The value is expected as an unsigned
long
as per the Java Unsigned Integer API.- Parameters:
val
- value to be writtenbuf
- buffer to be written into- Returns:
- the buffer
- Throws:
BufferOverflowException
- if the value doesn't fit the remaining buffer
-
writeInt32LE
public static void writeInt32LE(long val, byte[] out, int offset) throws ArrayIndexOutOfBoundsException Write a 32-bit integer to a given byte array in little-endian format, starting at a given offset.The value is expected as an unsigned
long
as per the Java Unsigned Integer API.- Parameters:
val
- value to be writtenout
- buffer to be written intooffset
- offset into the buffer- Throws:
ArrayIndexOutOfBoundsException
- if offset points outside of the buffer, or if the value doesn't fit the remaining buffer
-
writeInt32BE
Write a 32-bit integer to a given buffer in big-endian format.The value is expected as a signed or unsigned
int
.- Parameters:
val
- value to be writtenbuf
- buffer to be written into- Returns:
- the buffer
- Throws:
BufferOverflowException
- if the value doesn't fit the remaining buffer
-
writeInt32BE
public static void writeInt32BE(int val, byte[] out, int offset) throws ArrayIndexOutOfBoundsException Write a 32-bit integer to a given byte array in big-endian format, starting at a given offset.The value is expected as a signed or unsigned
int
.- Parameters:
val
- value to be writtenout
- buffer to be written intooffset
- offset into the buffer- Throws:
ArrayIndexOutOfBoundsException
- if offset points outside of the buffer, or if the value doesn't fit the remaining buffer
-
writeInt64LE
Write a 64-bit integer to a given buffer in little-endian format.The value is expected as a signed or unsigned
long
.- Parameters:
val
- value to be writtenbuf
- buffer to be written into- Returns:
- the buffer
- Throws:
BufferOverflowException
- if the value doesn't fit the remaining buffer
-
writeInt64LE
public static void writeInt64LE(long val, byte[] out, int offset) throws ArrayIndexOutOfBoundsException Write a 64-bit integer to a given byte array in little-endian format, starting at a given offset.The value is expected as a signed or unsigned
long
.- Parameters:
val
- value to be writtenout
- buffer to be written intooffset
- offset into the buffer- Throws:
ArrayIndexOutOfBoundsException
- if offset points outside of the buffer, or if the value doesn't fit the remaining buffer
-
writeInt16LE
Write a 16-bit integer to a given output stream in little-endian format.The value is expected as an unsigned
int
as per the Java Unsigned Integer API.- Parameters:
val
- value to be writtenstream
- stream to be written into- Throws:
IOException
- if an I/O error occurs
-
writeInt16BE
Write a 16-bit integer to a given output stream in big-endian format.The value is expected as an unsigned
int
as per the Java Unsigned Integer API.- Parameters:
val
- value to be writtenstream
- stream to be written into- Throws:
IOException
- if an I/O error occurs
-
writeInt32LE
Write a 32-bit integer to a given output stream in little-endian format.The value is expected as a signed or unsigned
int
. If you've got an unsignedlong
as per the Java Unsigned Integer API, usewriteInt32LE(long, OutputStream)
.- Parameters:
val
- value to be writtenstream
- stream to be written into- Throws:
IOException
- if an I/O error occurs
-
writeInt32LE
Write a 32-bit integer to a given output stream in little-endian format.The value is expected as an unsigned
long
as per the Java Unsigned Integer API.- Parameters:
val
- value to be writtenstream
- stream to be written into- Throws:
IOException
- if an I/O error occurs
-
writeInt32BE
Write a 32-bit integer to a given output stream in big-endian format.The value is expected as a signed or unsigned
int
.- Parameters:
val
- value to be writtenstream
- stream to be written into- Throws:
IOException
- if an I/O error occurs
-
writeInt64LE
Write a 64-bit integer to a given output stream in little-endian format.The value is expected as a signed or unsigned
long
.- Parameters:
val
- value to be writtenstream
- stream to be written into- Throws:
IOException
- if an I/O error occurs
-
writeInt64LE
Write a 64-bit integer to a given output stream in little-endian format.The value is expected as an unsigned
BigInteger
.- Parameters:
val
- value to be writtenstream
- stream to be written into- Throws:
IOException
- if an I/O error occurs
-
readUint16
Read 2 bytes from the buffer as unsigned 16-bit integer in little endian format.- Parameters:
buf
- buffer to be read from- Throws:
BufferUnderflowException
- if the read value extends beyond the remaining bytes of the buffer
-
readUint16
Read 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in little endian format.- Parameters:
bytes
- buffer to be read fromoffset
- offset into the buffer- Throws:
ArrayIndexOutOfBoundsException
- if offset points outside of the buffer, or if the read value extends beyond the remaining bytes of the buffer
-
readUint16BE
Read 2 bytes from the buffer as unsigned 16-bit integer in big endian format.- Parameters:
buf
- buffer to be read from- Throws:
BufferUnderflowException
- if the read value extends beyond the remaining bytes of the buffer
-
readUint16BE
Read 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in big endian format.- Parameters:
bytes
- buffer to be read fromoffset
- offset into the buffer- Throws:
ArrayIndexOutOfBoundsException
- if offset points outside of the buffer, or if the read value extends beyond the remaining bytes of the buffer
-
readUint32
Read 4 bytes from the buffer as unsigned 32-bit integer in little endian format.- Parameters:
buf
- buffer to be read from- Throws:
BufferUnderflowException
- if the read value extends beyond the remaining bytes of the buffer
-
readInt32
Read 4 bytes from the byte array (starting at the offset) as signed 32-bit integer in little endian format.- Parameters:
buf
- buffer to be read from- Returns:
- read integer
- Throws:
BufferUnderflowException
- if the read value extends beyond the remaining bytes of the buffer
-
readUint32
Read 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format.- Parameters:
bytes
- buffer to be read fromoffset
- offset into the buffer- Throws:
ArrayIndexOutOfBoundsException
- if offset points outside of the buffer, or if the read value extends beyond the remaining bytes of the buffer
-
readUint32BE
Read 4 bytes from the buffer as unsigned 32-bit integer in big endian format.- Parameters:
buf
- buffer to be read from- Throws:
BufferUnderflowException
- if the read value extends beyond the remaining bytes of the buffer
-
readUint32BE
Read 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in big endian format.- Parameters:
bytes
- buffer to be read fromoffset
- offset into the buffer- Throws:
ArrayIndexOutOfBoundsException
- if offset points outside of the buffer, or if the read value extends beyond the remaining bytes of the buffer
-
readInt64
Read 8 bytes from the buffer as signed 64-bit integer in little endian format.- Parameters:
buf
- buffer to be read from- Throws:
BufferUnderflowException
- if the read value extends beyond the remaining bytes of the buffer
-
readInt64
Read 8 bytes from the byte array (starting at the offset) as signed 64-bit integer in little endian format.- Parameters:
bytes
- buffer to be read fromoffset
- offset into the buffer- Throws:
ArrayIndexOutOfBoundsException
- if offset points outside of the buffer, or if the read value extends beyond the remaining bytes of the buffer
-
readUint16
Read 2 bytes from the stream as unsigned 16-bit integer in little endian format.- Parameters:
is
- stream to be read from
-
readUint32
Read 4 bytes from the stream as unsigned 32-bit integer in little endian format.- Parameters:
is
- stream to be read from
-
reverseBytes
public static byte[] reverseBytes(byte[] bytes) Returns a copy of the given byte array in reverse order. -
decodeMPI
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
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
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
- See Also:
-
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) -
arrayUnsignedComparator
Provides a byte array comparator.- Returns:
- A comparator for byte[]
-
concat
public static byte[] concat(byte[] b1, byte[] b2) Concatenate two byte arrays- Parameters:
b1
- first byte arrayb2
- second byte array- Returns:
- new concatenated byte array
-
hexFormat
orparseHex(String)
or other available options.