Skip to content

Commit ff3d409

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

5 files changed

Lines changed: 15 additions & 24 deletions

File tree

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: 11 additions & 17 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,8 @@ 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(), d.d1.getPos() + size, d.d2.getBuf(), d.d2.getPos(),
160+
d.d2.getPos() + size);
159161
d.d1.skipFixed(size);
160162
d.d2.skipFixed(size);
161163
return c;
@@ -164,7 +166,8 @@ private static int compare(Decoders d, Schema schema) throws IOException {
164166
case BYTES: {
165167
int l1 = d1.readInt();
166168
int l2 = d2.readInt();
167-
int c = compareBytes(d.d1.getBuf(), d.d1.getPos(), l1, d.d2.getBuf(), d.d2.getPos(), l2);
169+
int c = Arrays.compare(d.d1.getBuf(), d.d1.getPos(), d.d1.getPos() + l1, d.d2.getBuf(), d.d2.getPos(),
170+
d.d2.getPos() + l2);
168171
d.d1.skipFixed(l1);
169172
d.d2.skipFixed(l2);
170173
return c;
@@ -181,16 +184,7 @@ private static int compare(Decoders d, Schema schema) throws IOException {
181184
* return a positive value, if less than return a negative value.
182185
*/
183186
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;
187+
return Arrays.compare(b1, s1, s1 + l1, b2, s2, s2 + l2);
194188
}
195189

196190
private static class HashData {
@@ -298,7 +292,7 @@ public static int skipLong(final byte[] bytes, int start) {
298292
/**
299293
* Encode a boolean to the byte array at the given position. Will throw
300294
* IndexOutOfBounds if the position is not valid.
301-
*
295+
*
302296
* @return The number of bytes written to the buffer, 1.
303297
*/
304298
public static int encodeBoolean(boolean b, byte[] buf, int pos) {
@@ -310,7 +304,7 @@ public static int encodeBoolean(boolean b, byte[] buf, int pos) {
310304
* Encode an integer to the byte array at the given position. Will throw
311305
* IndexOutOfBounds if it overflows. Users should ensure that there are at least
312306
* 5 bytes left in the buffer before calling this method.
313-
*
307+
*
314308
* @return The number of bytes written to the buffer, between 1 and 5.
315309
*/
316310
public static int encodeInt(int n, byte[] buf, int pos) {
@@ -341,7 +335,7 @@ public static int encodeInt(int n, byte[] buf, int pos) {
341335
* Encode a long to the byte array at the given position. Will throw
342336
* IndexOutOfBounds if it overflows. Users should ensure that there are at least
343337
* 10 bytes left in the buffer before calling this method.
344-
*
338+
*
345339
* @return The number of bytes written to the buffer, between 1 and 10.
346340
*/
347341
public static int encodeLong(long n, byte[] buf, int pos) {
@@ -392,7 +386,7 @@ public static int encodeLong(long n, byte[] buf, int pos) {
392386
* Encode a float to the byte array at the given position. Will throw
393387
* IndexOutOfBounds if it overflows. Users should ensure that there are at least
394388
* 4 bytes left in the buffer before calling this method.
395-
*
389+
*
396390
* @return Returns the number of bytes written to the buffer, 4.
397391
*/
398392
public static int encodeFloat(float f, byte[] buf, int pos) {
@@ -408,7 +402,7 @@ public static int encodeFloat(float f, byte[] buf, int pos) {
408402
* Encode a double to the byte array at the given position. Will throw
409403
* IndexOutOfBounds if it overflows. Users should ensure that there are at least
410404
* 8 bytes left in the buffer before calling this method.
411-
*
405+
*
412406
* @return Returns the number of bytes written to the buffer, 8.
413407
*/
414408
public static int encodeDouble(double d, byte[] buf, int pos) {

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)