Skip to content
Merged
Prev Previous commit
Next Next commit
Avoid unnecessary object creations
  • Loading branch information
ibauersachs committed Dec 25, 2021
commit 9119b5d2ecff0d0a61762a3b1b7fa2c41f893946
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Copyright (c) 1998-2019, Brian Wellington
Copyright (c) 2005 VeriSign. All rights reserved.
Copyright (c) 2019-2021, dnsjava authors

All rights reserved.
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/xbill/DNS/dnssec/ByteArrayComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
// Copyright (c) 2013-2021 Ingo Bauersachs
package org.xbill.DNS.dnssec;

import java.util.Comparator;
import lombok.experimental.UtilityClass;

/**
* This class implements a basic comparator for byte arrays. It is primarily useful for comparing
* RDATA portions of DNS records in doing DNSSEC canonical ordering.
*
* @since 3.5
*/
final class ByteArrayComparator implements Comparator<Object> {
@UtilityClass
final class ByteArrayComparator {
private static final int MAX_BYTE = 0xFF;

/** {@inheritDoc} */
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/xbill/DNS/dnssec/NSEC3ValUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,16 @@ private boolean nsec3Covers(NSEC3Record nsec3, Name zonename, byte[] hash) {
byte[] next = nsec3.getNext();

// This is the "normal case: owner < next and owner < hash < next
ByteArrayComparator bac = new ByteArrayComparator();
if (bac.compare(owner, hash) < 0 && bac.compare(hash, next) < 0) {
if (ByteArrayComparator.compare(owner, hash) < 0
&& ByteArrayComparator.compare(hash, next) < 0) {
return true;
}

// this is the end of zone case: next <= owner AND (hash > owner OR hash < next)
// Otherwise, the NSEC3 does not cover the hash.
return bac.compare(next, owner) <= 0
&& (bac.compare(hash, owner) > 0 || bac.compare(hash, next) < 0);
return ByteArrayComparator.compare(next, owner) <= 0
&& (ByteArrayComparator.compare(hash, owner) > 0
|| ByteArrayComparator.compare(hash, next) < 0);
}

/**
Expand Down Expand Up @@ -385,7 +386,6 @@ private boolean validIterations(SRRset nsec, KeyCache keyCache) {
*/
public boolean allNSEC3sIgnoreable(List<SRRset> nsec3s, KeyCache dnskeyRrset) {
Map<Name, NSEC3Record> foundNsecs = new HashMap<>();
ByteArrayComparator comp = new ByteArrayComparator();
for (SRRset set : nsec3s) {
for (Record r : set.rrs()) {
NSEC3Record current = (NSEC3Record) r;
Expand All @@ -405,7 +405,7 @@ public boolean allNSEC3sIgnoreable(List<SRRset> nsec3s, KeyCache dnskeyRrset) {
}

if (current.getSalt() != null
&& comp.compare(current.getSalt(), previous.getSalt()) != 0) {
&& ByteArrayComparator.compare(current.getSalt(), previous.getSalt()) != 0) {
return true;
}
} else {
Expand Down
11 changes: 5 additions & 6 deletions src/test/java/org/xbill/DNS/dnssec/TestByteArrayComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,25 @@
import org.junit.jupiter.api.Test;

class TestByteArrayComparator {
private final ByteArrayComparator c = new ByteArrayComparator();
private final byte[] b1 = new byte[] {0};
private final byte[] b2 = new byte[] {0};
private final byte[] b3 = new byte[] {1};
private final byte[] b4 = new byte[] {1, 0};

@Test
void testEquals() {
assertEquals(0, c.compare(b1, b2));
assertEquals(0, ByteArrayComparator.compare(b1, b2));
}

@Test
void testLessThan() {
assertEquals(-1, c.compare(b2, b3));
assertEquals(-1, c.compare(b1, b4));
assertEquals(-1, ByteArrayComparator.compare(b2, b3));
assertEquals(-1, ByteArrayComparator.compare(b1, b4));
}

@Test
void testGreaterThan() {
assertEquals(1, c.compare(b3, b2));
assertEquals(1, c.compare(b4, b1));
assertEquals(1, ByteArrayComparator.compare(b3, b2));
assertEquals(1, ByteArrayComparator.compare(b4, b1));
}
}