ByteUtils: improve JavaDoc comments for bigIntegerToBytes(), bytesToBigInteger()

This commit is contained in:
Sean Gilligan 2024-06-23 18:14:13 -07:00 committed by Andreas Schildbach
parent fae543bb6d
commit 0aa17cd0a2

View file

@ -64,21 +64,20 @@ public class ByteUtils {
/** /**
* <p> * <p>
* The regular {@link BigInteger#toByteArray()} includes the sign bit of the number and * The built-in {@link BigInteger#toByteArray()} includes the sign bit of the number and
* might result in an extra byte addition. This method removes this extra byte. * may result in an extra byte in cases of unsigned data. This method removes this extra byte.
* </p> * </p>
* <p> * <p>
* Assuming only positive numbers, it's possible to discriminate if an extra byte * Assuming only positive numbers, it's possible to tell if an extra byte
* is added by checking if the first element of the array is 0 (0000_0000). * was 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 * Due to the guarantee of a minimal representation provided by BigInteger, we know that the sign bit
* is the least significant bit 0000_000<b>0</b> . * will be the least significant bit 0000_000<b>0</b> of a zero-value first byte.
* Otherwise the representation is not minimal. * Otherwise the representation would not be minimal.
* For example, if the sign bit is 0000_00<b>0</b>0, then the representation is not minimal due to the rightmost zero.
* </p> * </p>
* This is the antagonist to {@link #bytesToBigInteger(byte[])}. * This is the inverse of {@link #bytesToBigInteger(byte[])}.
* @param b the integer to format into a byte array * @param b the non-negative integer to format into a byte array
* @param numBytes the desired size of the resulting byte array * @param numBytes the maximum allowed size of the resulting byte array
* @return numBytes byte long array. * @return byte array of max length {@code numBytes}
*/ */
public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) { public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) {
checkArgument(b.signum() >= 0, () -> "b must be positive or zero: " + b); checkArgument(b.signum() >= 0, () -> "b must be positive or zero: " + b);
@ -95,7 +94,7 @@ public class ByteUtils {
} }
/** /**
* Converts an array of bytes into a positive BigInteger. This is the antagonist to * Converts an array of bytes into a positive BigInteger. This is the inverse of
* {@link #bigIntegerToBytes(BigInteger, int)}. * {@link #bigIntegerToBytes(BigInteger, int)}.
* *
* @param bytes to convert into a BigInteger * @param bytes to convert into a BigInteger