Skip to content

Commit 4b05b82

Browse files
committed
AVRO-4045: Use JDK compare for byte arrays
1 parent c7b79bf commit 4b05b82

File tree

5 files changed

+8
-19
lines changed

5 files changed

+8
-19
lines changed

lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ public String toString() {
545545

546546
@Override
547547
public int compareTo(Fixed that) {
548-
return BinaryData.compareBytes(this.bytes, 0, this.bytes.length, that.bytes, 0, that.bytes.length);
548+
return Arrays.compare(this.bytes, 0, this.bytes.length, that.bytes, 0, that.bytes.length);
549549
}
550550
}
551551

lang/java/avro/src/main/java/org/apache/avro/io/BinaryData.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.avro.io;
1919

2020
import java.io.IOException;
21+
import java.util.Arrays;
2122

2223
import org.apache.avro.Schema;
2324
import org.apache.avro.Schema.Field;
@@ -155,7 +156,7 @@ private static int compare(Decoders d, Schema schema) throws IOException {
155156
}
156157
case FIXED: {
157158
int size = schema.getFixedSize();
158-
int c = compareBytes(d.d1.getBuf(), d.d1.getPos(), size, d.d2.getBuf(), d.d2.getPos(), size);
159+
int c = Arrays.compare(d.d1.getBuf(), d.d1.getPos(), size, d.d2.getBuf(), d.d2.getPos(), size);
159160
d.d1.skipFixed(size);
160161
d.d2.skipFixed(size);
161162
return c;
@@ -164,7 +165,7 @@ private static int compare(Decoders d, Schema schema) throws IOException {
164165
case BYTES: {
165166
int l1 = d1.readInt();
166167
int l2 = d2.readInt();
167-
int c = compareBytes(d.d1.getBuf(), d.d1.getPos(), l1, d.d2.getBuf(), d.d2.getPos(), l2);
168+
int c = Arrays.compare(d.d1.getBuf(), d.d1.getPos(), l1, d.d2.getBuf(), d.d2.getPos(), l2);
168169
d.d1.skipFixed(l1);
169170
d.d2.skipFixed(l2);
170171
return c;
@@ -181,16 +182,7 @@ private static int compare(Decoders d, Schema schema) throws IOException {
181182
* return a positive value, if less than return a negative value.
182183
*/
183184
public static int compareBytes(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
184-
int end1 = s1 + l1;
185-
int end2 = s2 + l2;
186-
for (int i = s1, j = s2; i < end1 && j < end2; i++, j++) {
187-
int a = (b1[i] & 0xff);
188-
int b = (b2[j] & 0xff);
189-
if (a != b) {
190-
return a - b;
191-
}
192-
}
193-
return l1 - l2;
185+
return Arrays.compare(b1, s1, l1, b2, s2, l2);
194186
}
195187

196188
private static class HashData {

lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.apache.avro.generic.GenericData;
3131
import org.apache.avro.generic.GenericFixed;
3232
import org.apache.avro.generic.IndexedRecord;
33-
import org.apache.avro.io.BinaryData;
3433
import org.apache.avro.io.DatumReader;
3534
import org.apache.avro.io.DatumWriter;
3635
import org.apache.avro.specific.FixedSize;
@@ -987,7 +986,7 @@ protected int compare(Object o1, Object o2, Schema s, boolean equals) {
987986
break;
988987
byte[] b1 = (byte[]) o1;
989988
byte[] b2 = (byte[]) o2;
990-
return BinaryData.compareBytes(b1, 0, b1.length, b2, 0, b2.length);
989+
return Arrays.compare(b1, 0, b1.length, b2, 0, b2.length);
991990
}
992991
return super.compare(o1, o2, s, equals);
993992
}

lang/java/avro/src/main/java/org/apache/avro/specific/SpecificFixed.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Arrays;
2525
import org.apache.avro.Schema;
2626
import org.apache.avro.generic.GenericFixed;
27-
import org.apache.avro.io.BinaryData;
2827

2928
/** Base class for generated fixed-sized data classes. */
3029
public abstract class SpecificFixed implements GenericFixed, Comparable<SpecificFixed>, Externalizable {
@@ -70,7 +69,7 @@ public String toString() {
7069

7170
@Override
7271
public int compareTo(SpecificFixed that) {
73-
return BinaryData.compareBytes(this.bytes, 0, this.bytes.length, that.bytes, 0, that.bytes.length);
72+
return Arrays.compare(this.bytes, 0, this.bytes.length, that.bytes, 0, that.bytes.length);
7473
}
7574

7675
@Override

lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.Arrays;
2626

2727
import org.apache.avro.SystemLimitException;
28-
import org.apache.avro.io.BinaryData;
2928

3029
/**
3130
* A Utf8 string. Unlike {@link String}, instances are mutable. This is more
@@ -181,7 +180,7 @@ public int hashCode() {
181180

182181
@Override
183182
public int compareTo(Utf8 that) {
184-
return BinaryData.compareBytes(this.bytes, 0, this.length, that.bytes, 0, that.length);
183+
return Arrays.compare(this.bytes, 0, this.length, that.bytes, 0, that.length);
185184
}
186185

187186
// CharSequence implementation

0 commit comments

Comments
 (0)