From b2221070447f2ab6557ebd43815fde4e8740e744 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Thu, 30 May 2019 14:30:16 +0100 Subject: [PATCH] Add static method for comparing byte arrays. --- .../org/briarproject/bramble/api/Bytes.java | 24 +++++++------------ .../client/ContactGroupFactoryImpl.java | 3 +-- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/Bytes.java b/bramble-api/src/main/java/org/briarproject/bramble/api/Bytes.java index 6da75fdff..d8a727b71 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/Bytes.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/Bytes.java @@ -4,7 +4,6 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.util.StringUtils; import java.util.Arrays; -import java.util.Comparator; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; @@ -16,8 +15,6 @@ import javax.annotation.concurrent.ThreadSafe; @NotNullByDefault public class Bytes implements Comparable { - public static final BytesComparator COMPARATOR = new BytesComparator(); - private final byte[] bytes; private int hashCode = -1; @@ -45,14 +42,7 @@ public class Bytes implements Comparable { @Override public int compareTo(Bytes other) { - byte[] aBytes = bytes, bBytes = other.bytes; - int length = Math.min(aBytes.length, bBytes.length); - for (int i = 0; i < length; i++) { - int aUnsigned = aBytes[i] & 0xFF, bUnsigned = bBytes[i] & 0xFF; - if (aUnsigned < bUnsigned) return -1; - if (aUnsigned > bUnsigned) return 1; - } - return aBytes.length - bBytes.length; + return compare(bytes, other.bytes); } @Override @@ -61,11 +51,13 @@ public class Bytes implements Comparable { "(" + StringUtils.toHexString(getBytes()) + ")"; } - public static class BytesComparator implements Comparator { - - @Override - public int compare(Bytes a, Bytes b) { - return a.compareTo(b); + public static int compare(byte[] a, byte[] b) { + int length = Math.min(a.length, b.length); + for (int i = 0; i < length; i++) { + int aUnsigned = a[i] & 0xFF, bUnsigned = b[i] & 0xFF; + if (aUnsigned < bUnsigned) return -1; + if (aUnsigned > bUnsigned) return 1; } + return a.length - b.length; } } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/client/ContactGroupFactoryImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/client/ContactGroupFactoryImpl.java index 6b35c9281..091bc95f7 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/client/ContactGroupFactoryImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/client/ContactGroupFactoryImpl.java @@ -1,6 +1,5 @@ package org.briarproject.bramble.client; -import org.briarproject.bramble.api.Bytes; import org.briarproject.bramble.api.FormatException; import org.briarproject.bramble.api.client.ClientHelper; import org.briarproject.bramble.api.client.ContactGroupFactory; @@ -55,7 +54,7 @@ class ContactGroupFactoryImpl implements ContactGroupFactory { private byte[] createGroupDescriptor(AuthorId local, AuthorId remote) { try { - if (Bytes.COMPARATOR.compare(local, remote) < 0) + if (local.compareTo(remote) < 0) return clientHelper.toByteArray(BdfList.of(local, remote)); else return clientHelper.toByteArray(BdfList.of(remote, local)); } catch (FormatException e) {