001: package org.bouncycastle.util;
002:
003: /**
004: * General array utilities.
005: */
006: public final class Arrays {
007: private Arrays() {
008: // static class, hide constructor
009: }
010:
011: public static boolean areEqual(byte[] a, byte[] b) {
012: if (a == b) {
013: return true;
014: }
015:
016: if (a == null || b == null) {
017: return false;
018: }
019:
020: if (a.length != b.length) {
021: return false;
022: }
023:
024: for (int i = 0; i != a.length; i++) {
025: if (a[i] != b[i]) {
026: return false;
027: }
028: }
029:
030: return true;
031: }
032:
033: public static boolean areEqual(int[] a, int[] b) {
034: if (a == b) {
035: return true;
036: }
037:
038: if (a == null || b == null) {
039: return false;
040: }
041:
042: if (a.length != b.length) {
043: return false;
044: }
045:
046: for (int i = 0; i != a.length; i++) {
047: if (a[i] != b[i]) {
048: return false;
049: }
050: }
051:
052: return true;
053: }
054:
055: public static void fill(byte[] array, byte value) {
056: for (int i = 0; i < array.length; i++) {
057: array[i] = value;
058: }
059: }
060:
061: public static void fill(long[] array, long value) {
062: for (int i = 0; i < array.length; i++) {
063: array[i] = value;
064: }
065: }
066:
067: public static void fill(short[] array, short value) {
068: for (int i = 0; i < array.length; i++) {
069: array[i] = value;
070: }
071: }
072:
073: public static int hashCode(byte[] data) {
074: int value = 0;
075:
076: if (data != null) {
077: for (int i = 0; i != data.length; i++) {
078: value ^= (data[i] & 0xff) << (i % 4);
079: }
080: }
081:
082: return value;
083: }
084:
085: public static byte[] clone(byte[] data) {
086: byte[] copy = new byte[data.length];
087:
088: System.arraycopy(data, 0, copy, 0, data.length);
089:
090: return copy;
091: }
092:
093: public static int[] clone(int[] data) {
094: int[] copy = new int[data.length];
095:
096: System.arraycopy(data, 0, copy, 0, data.length);
097:
098: return copy;
099: }
100: }
|