1818package org .apache .avro .io ;
1919
2020import java .io .IOException ;
21+ import java .util .Arrays ;
2122
2223import org .apache .avro .Schema ;
2324import 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 ) {
0 commit comments