aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/common/util/ByteUtil.java
diff options
context:
space:
mode:
authorJ08nY2018-03-03 19:13:04 +0100
committerJ08nY2018-03-03 19:13:04 +0100
commit30210ec562a49d1b397a19d8c64ed0e0fd80cc0e (patch)
tree9d9d5be4c7ff2dd7023d6933039eafa2402adfbf /src/cz/crcs/ectester/common/util/ByteUtil.java
parent6e93b823e5a3aaab738af143d9c1ed07d8c56fcd (diff)
downloadECTester-30210ec562a49d1b397a19d8c64ed0e0fd80cc0e.tar.gz
ECTester-30210ec562a49d1b397a19d8c64ed0e0fd80cc0e.tar.zst
ECTester-30210ec562a49d1b397a19d8c64ed0e0fd80cc0e.zip
Add some docs and fix ECTesterReader --bit-size option.
Diffstat (limited to 'src/cz/crcs/ectester/common/util/ByteUtil.java')
-rw-r--r--src/cz/crcs/ectester/common/util/ByteUtil.java30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/cz/crcs/ectester/common/util/ByteUtil.java b/src/cz/crcs/ectester/common/util/ByteUtil.java
index ad9f1bf..0de8dc7 100644
--- a/src/cz/crcs/ectester/common/util/ByteUtil.java
+++ b/src/cz/crcs/ectester/common/util/ByteUtil.java
@@ -7,15 +7,27 @@ package cz.crcs.ectester.common.util;
* @author Jan Jancar johny@neuromancer.sk
*/
public class ByteUtil {
+
+ /**
+ * Gen a short from a byte array at <code>offset</code>, big-endian.
+ * @return the short value
+ */
public static short getShort(byte[] array, int offset) {
return (short) (((array[offset] & 0xFF) << 8) | (array[offset + 1] & 0xFF));
}
+ /**
+ * Set a short in a byte array at <code>offset</code>, big-endian.
+ */
public static void setShort(byte[] array, int offset, short value) {
array[offset + 1] = (byte) (value & 0xFF);
array[offset] = (byte) ((value >> 8) & 0xFF);
}
+ /**
+ * Compare two byte arrays upto <code>length</code> and get first difference.
+ * @return the position of the first difference in the two byte arrays, or <code>length</code> if they are equal.
+ */
public static int diffBytes(byte[] one, int oneOffset, byte[] other, int otherOffset, int length) {
for (int i = 0; i < length; ++i) {
byte a = one[i + oneOffset];
@@ -27,10 +39,17 @@ public class ByteUtil {
return length;
}
+ /**
+ * Compare two byte arrays, upto <code>length</code>.
+ * @return whether the arrays are equal upto <code>length</code>
+ */
public static boolean compareBytes(byte[] one, int oneOffset, byte[] other, int otherOffset, int length) {
return diffBytes(one, oneOffset, other, otherOffset, length) == length;
}
+ /**
+ * Test if the byte array has all values equal to <code>value</code>.
+ */
public static boolean allValue(byte[] array, byte value) {
for (byte a : array) {
if (a != value)
@@ -56,10 +75,21 @@ public class ByteUtil {
return result;
}
+ /**
+ * Parse a hex string into a byte array, big-endian.
+ * @param hex The String to parse.
+ * @return the byte array from the hex string.
+ */
public static byte[] hexToBytes(String hex) {
return hexToBytes(hex, true);
}
+ /**
+ * Parse a hex string into a byte-array, specify endianity.
+ * @param hex The String to parse.
+ * @param bigEndian Whether to parse as big-endian.
+ * @return the byte array from the hex string.
+ */
public static byte[] hexToBytes(String hex, boolean bigEndian) {
hex = hex.replace(" ", "");
int len = hex.length();