summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/util/Arrays.java
diff options
context:
space:
mode:
Diffstat (limited to 'bcprov/src/main/java/org/bouncycastle/util/Arrays.java')
-rw-r--r--bcprov/src/main/java/org/bouncycastle/util/Arrays.java140
1 files changed, 130 insertions, 10 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/util/Arrays.java b/bcprov/src/main/java/org/bouncycastle/util/Arrays.java
index 457320e..3f7677c 100644
--- a/bcprov/src/main/java/org/bouncycastle/util/Arrays.java
+++ b/bcprov/src/main/java/org/bouncycastle/util/Arrays.java
@@ -11,7 +11,7 @@ public final class Arrays
{
// static class, hide constructor
}
-
+
public static boolean areEqual(
boolean[] a,
boolean[] b)
@@ -199,36 +199,62 @@ public final class Arrays
return true;
}
- public static boolean areEqual(
- BigInteger[] a,
- BigInteger[] b)
+ public static boolean areEqual(Object[] a, Object[] b)
{
if (a == b)
{
return true;
}
-
if (a == null || b == null)
{
return false;
}
-
if (a.length != b.length)
{
return false;
}
-
for (int i = 0; i != a.length; i++)
{
- if (!a[i].equals(b[i]))
+ Object objA = a[i], objB = b[i];
+ if (objA == null)
+ {
+ if (objB != null)
+ {
+ return false;
+ }
+ }
+ else if (!objA.equals(objB))
{
return false;
}
}
-
return true;
}
+ public static boolean contains(short[] a, short n)
+ {
+ for (int i = 0; i < a.length; ++i)
+ {
+ if (a[i] == n)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static boolean contains(int[] a, int n)
+ {
+ for (int i = 0; i < a.length; ++i)
+ {
+ if (a[i] == n)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
public static void fill(
byte[] array,
byte value)
@@ -391,7 +417,7 @@ public final class Arrays
return hc;
}
- public static int hashCode(BigInteger[] data)
+ public static int hashCode(Object[] data)
{
if (data == null)
{
@@ -423,6 +449,20 @@ public final class Arrays
return copy;
}
+ public static byte[] clone(byte[] data, byte[] existing)
+ {
+ if (data == null)
+ {
+ return null;
+ }
+ if ((existing == null) || (existing.length != data.length))
+ {
+ return clone(data);
+ }
+ System.arraycopy(data, 0, existing, 0, existing.length);
+ return existing;
+ }
+
public static byte[][] clone(byte[][] data)
{
if (data == null)
@@ -470,6 +510,33 @@ public final class Arrays
return copy;
}
+ public static long[] clone(long[] data)
+ {
+ if (data == null)
+ {
+ return null;
+ }
+ long[] copy = new long[data.length];
+
+ System.arraycopy(data, 0, copy, 0, data.length);
+
+ return copy;
+ }
+
+ public static long[] clone(long[] data, long[] existing)
+ {
+ if (data == null)
+ {
+ return null;
+ }
+ if ((existing == null) || (existing.length != data.length))
+ {
+ return clone(data);
+ }
+ System.arraycopy(data, 0, existing, 0, existing.length);
+ return existing;
+ }
+
public static short[] clone(short[] data)
{
if (data == null)
@@ -576,6 +643,17 @@ public final class Arrays
return tmp;
}
+ /**
+ * Make a copy of a range of bytes from the passed in data array. The range can
+ * extend beyond the end of the input array, in which case the return array will
+ * be padded with zeroes.
+ *
+ * @param data the array from which the data is to be copied.
+ * @param from the start index at which the copying should take place.
+ * @param to the final index of the range (exclusive).
+ *
+ * @return a new byte array containing the range given.
+ */
public static byte[] copyOfRange(byte[] data, int from, int to)
{
int newLength = getLength(from, to);
@@ -660,6 +738,34 @@ public final class Arrays
return newLength;
}
+ public static byte[] append(byte[] a, byte b)
+ {
+ if (a == null)
+ {
+ return new byte[]{ b };
+ }
+
+ int length = a.length;
+ byte[] result = new byte[length + 1];
+ System.arraycopy(a, 0, result, 0, length);
+ result[length] = b;
+ return result;
+ }
+
+ public static int[] append(int[] a, int b)
+ {
+ if (a == null)
+ {
+ return new int[]{ b };
+ }
+
+ int length = a.length;
+ int[] result = new int[length + 1];
+ System.arraycopy(a, 0, result, 0, length);
+ result[length] = b;
+ return result;
+ }
+
public static byte[] concatenate(byte[] a, byte[] b)
{
if (a != null && b != null)
@@ -733,4 +839,18 @@ public final class Arrays
return concatenate(b, c, d);
}
}
+
+ public static byte[] prepend(byte[] a, byte b)
+ {
+ if (a == null)
+ {
+ return new byte[]{ b };
+ }
+
+ int length = a.length;
+ byte[] result = new byte[length + 1];
+ System.arraycopy(a, 0, result, 1, length);
+ result[0] = b;
+ return result;
+ }
}