1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.util;
19
20 import static com.google.common.base.Preconditions.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkPositionIndex;
23
24 import java.io.DataInput;
25 import java.io.DataOutput;
26 import java.io.IOException;
27 import java.lang.reflect.Field;
28 import java.math.BigDecimal;
29 import java.math.BigInteger;
30 import java.nio.ByteBuffer;
31 import java.nio.ByteOrder;
32 import java.nio.charset.Charset;
33 import java.security.AccessController;
34 import java.security.PrivilegedAction;
35 import java.security.SecureRandom;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.Comparator;
39 import java.util.Iterator;
40 import java.util.List;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.apache.hadoop.hbase.classification.InterfaceAudience;
45 import org.apache.hadoop.hbase.classification.InterfaceStability;
46 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
47 import org.apache.hadoop.io.RawComparator;
48 import org.apache.hadoop.io.WritableComparator;
49 import org.apache.hadoop.io.WritableUtils;
50
51 import sun.misc.Unsafe;
52
53 import com.google.common.annotations.VisibleForTesting;
54 import com.google.common.collect.Lists;
55 import org.apache.hadoop.hbase.util.Bytes.LexicographicalComparerHolder.UnsafeComparer;
56
57
58
59
60
61
62 @InterfaceAudience.Public
63 @InterfaceStability.Stable
64 public class Bytes {
65
66
67 private static final String UTF8_ENCODING = "UTF-8";
68
69
70
71 private static final Charset UTF8_CHARSET = Charset.forName(UTF8_ENCODING);
72
73
74 private static final byte [] EMPTY_BYTE_ARRAY = new byte [0];
75
76 private static final Log LOG = LogFactory.getLog(Bytes.class);
77
78
79
80
81 public static final int SIZEOF_BOOLEAN = Byte.SIZE / Byte.SIZE;
82
83
84
85
86 public static final int SIZEOF_BYTE = SIZEOF_BOOLEAN;
87
88
89
90
91 public static final int SIZEOF_CHAR = Character.SIZE / Byte.SIZE;
92
93
94
95
96 public static final int SIZEOF_DOUBLE = Double.SIZE / Byte.SIZE;
97
98
99
100
101 public static final int SIZEOF_FLOAT = Float.SIZE / Byte.SIZE;
102
103
104
105
106 public static final int SIZEOF_INT = Integer.SIZE / Byte.SIZE;
107
108
109
110
111 public static final int SIZEOF_LONG = Long.SIZE / Byte.SIZE;
112
113
114
115
116 public static final int SIZEOF_SHORT = Short.SIZE / Byte.SIZE;
117
118
119
120
121
122
123
124
125 public static final int ESTIMATED_HEAP_TAX = 16;
126
127
128
129
130
131
132
133
134 final public static int len(byte[] b) {
135 return b == null ? 0 : b.length;
136 }
137
138
139
140
141 @InterfaceAudience.Public
142 @InterfaceStability.Stable
143 public static class ByteArrayComparator implements RawComparator<byte []> {
144
145
146
147 public ByteArrayComparator() {
148 super();
149 }
150 @Override
151 public int compare(byte [] left, byte [] right) {
152 return compareTo(left, right);
153 }
154 @Override
155 public int compare(byte [] b1, int s1, int l1, byte [] b2, int s2, int l2) {
156 return LexicographicalComparerHolder.BEST_COMPARER.
157 compareTo(b1, s1, l1, b2, s2, l2);
158 }
159 }
160
161
162
163
164
165
166
167
168
169 @InterfaceAudience.Public
170 @InterfaceStability.Stable
171 public static class RowEndKeyComparator extends ByteArrayComparator {
172 @Override
173 public int compare(byte[] left, byte[] right) {
174 return compare(left, 0, left.length, right, 0, right.length);
175 }
176 @Override
177 public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
178 if (b1 == b2 && s1 == s2 && l1 == l2) {
179 return 0;
180 }
181 if (l1 == 0) {
182 return l2;
183 }
184 if (l2 == 0) {
185 return -1;
186 }
187 return super.compare(b1, s1, l1, b2, s2, l2);
188 }
189 }
190
191
192
193
194 public final static Comparator<byte []> BYTES_COMPARATOR = new ByteArrayComparator();
195
196
197
198
199 public final static RawComparator<byte []> BYTES_RAWCOMPARATOR = new ByteArrayComparator();
200
201
202
203
204
205
206
207 public static byte [] readByteArray(final DataInput in)
208 throws IOException {
209 int len = WritableUtils.readVInt(in);
210 if (len < 0) {
211 throw new NegativeArraySizeException(Integer.toString(len));
212 }
213 byte [] result = new byte[len];
214 in.readFully(result, 0, len);
215 return result;
216 }
217
218
219
220
221
222
223
224 public static byte [] readByteArrayThrowsRuntime(final DataInput in) {
225 try {
226 return readByteArray(in);
227 } catch (Exception e) {
228 throw new RuntimeException(e);
229 }
230 }
231
232
233
234
235
236
237
238 public static void writeByteArray(final DataOutput out, final byte [] b)
239 throws IOException {
240 if(b == null) {
241 WritableUtils.writeVInt(out, 0);
242 } else {
243 writeByteArray(out, b, 0, b.length);
244 }
245 }
246
247
248
249
250
251
252
253
254
255 public static void writeByteArray(final DataOutput out, final byte [] b,
256 final int offset, final int length)
257 throws IOException {
258 WritableUtils.writeVInt(out, length);
259 out.write(b, offset, length);
260 }
261
262
263
264
265
266
267
268
269
270
271 public static int writeByteArray(final byte [] tgt, final int tgtOffset,
272 final byte [] src, final int srcOffset, final int srcLength) {
273 byte [] vint = vintToBytes(srcLength);
274 System.arraycopy(vint, 0, tgt, tgtOffset, vint.length);
275 int offset = tgtOffset + vint.length;
276 System.arraycopy(src, srcOffset, tgt, offset, srcLength);
277 return offset + srcLength;
278 }
279
280
281
282
283
284
285
286
287
288
289 public static int putBytes(byte[] tgtBytes, int tgtOffset, byte[] srcBytes,
290 int srcOffset, int srcLength) {
291 System.arraycopy(srcBytes, srcOffset, tgtBytes, tgtOffset, srcLength);
292 return tgtOffset + srcLength;
293 }
294
295
296
297
298
299
300
301
302 public static int putByte(byte[] bytes, int offset, byte b) {
303 bytes[offset] = b;
304 return offset + 1;
305 }
306
307
308
309
310
311
312
313
314 public static int putByteBuffer(byte[] bytes, int offset, ByteBuffer buf) {
315 int len = buf.remaining();
316 buf.get(bytes, offset, len);
317 return offset + len;
318 }
319
320
321
322
323
324
325
326
327
328
329
330 public static byte[] toBytes(ByteBuffer buf) {
331 ByteBuffer dup = buf.duplicate();
332 dup.position(0);
333 return readBytes(dup);
334 }
335
336 private static byte[] readBytes(ByteBuffer buf) {
337 byte [] result = new byte[buf.remaining()];
338 buf.get(result);
339 return result;
340 }
341
342
343
344
345
346 public static String toString(final byte [] b) {
347 if (b == null) {
348 return null;
349 }
350 return toString(b, 0, b.length);
351 }
352
353
354
355
356
357
358
359 public static String toString(final byte [] b1,
360 String sep,
361 final byte [] b2) {
362 return toString(b1, 0, b1.length) + sep + toString(b2, 0, b2.length);
363 }
364
365
366
367
368
369
370
371
372
373
374 public static String toString(final byte [] b, int off, int len) {
375 if (b == null) {
376 return null;
377 }
378 if (len == 0) {
379 return "";
380 }
381 return new String(b, off, len, UTF8_CHARSET);
382 }
383
384
385
386
387
388
389
390
391 public static String toStringBinary(final byte [] b) {
392 if (b == null)
393 return "null";
394 return toStringBinary(b, 0, b.length);
395 }
396
397
398
399
400
401
402
403
404
405
406
407
408 public static String toStringBinary(ByteBuffer buf) {
409 if (buf == null)
410 return "null";
411 if (buf.hasArray()) {
412 return toStringBinary(buf.array(), buf.arrayOffset(), buf.limit());
413 }
414 return toStringBinary(toBytes(buf));
415 }
416
417
418
419
420
421
422
423
424
425
426
427 public static String toStringBinary(final byte [] b, int off, int len) {
428 StringBuilder result = new StringBuilder();
429
430 if (off >= b.length) return result.toString();
431 if (off + len > b.length) len = b.length - off;
432 for (int i = off; i < off + len ; ++i ) {
433 int ch = b[i] & 0xFF;
434 if ( (ch >= '0' && ch <= '9')
435 || (ch >= 'A' && ch <= 'Z')
436 || (ch >= 'a' && ch <= 'z')
437 || " `~!@#$%^&*()-_=+[]{}|;:'\",.<>/?".indexOf(ch) >= 0 ) {
438 result.append((char)ch);
439 } else {
440 result.append(String.format("\\x%02X", ch));
441 }
442 }
443 return result.toString();
444 }
445
446 private static boolean isHexDigit(char c) {
447 return
448 (c >= 'A' && c <= 'F') ||
449 (c >= '0' && c <= '9');
450 }
451
452
453
454
455
456
457
458 public static byte toBinaryFromHex(byte ch) {
459 if ( ch >= 'A' && ch <= 'F' )
460 return (byte) ((byte)10 + (byte) (ch - 'A'));
461
462 return (byte) (ch - '0');
463 }
464
465 public static byte [] toBytesBinary(String in) {
466
467 byte [] b = new byte[in.length()];
468 int size = 0;
469 for (int i = 0; i < in.length(); ++i) {
470 char ch = in.charAt(i);
471 if (ch == '\\' && in.length() > i+1 && in.charAt(i+1) == 'x') {
472
473 char hd1 = in.charAt(i+2);
474 char hd2 = in.charAt(i+3);
475
476
477 if (!isHexDigit(hd1) ||
478 !isHexDigit(hd2)) {
479
480 continue;
481 }
482
483 byte d = (byte) ((toBinaryFromHex((byte)hd1) << 4) + toBinaryFromHex((byte)hd2));
484
485 b[size++] = d;
486 i += 3;
487 } else {
488 b[size++] = (byte) ch;
489 }
490 }
491
492 byte [] b2 = new byte[size];
493 System.arraycopy(b, 0, b2, 0, size);
494 return b2;
495 }
496
497
498
499
500
501
502 public static byte[] toBytes(String s) {
503 return s.getBytes(UTF8_CHARSET);
504 }
505
506
507
508
509
510
511
512
513 public static byte [] toBytes(final boolean b) {
514 return new byte[] { b ? (byte) -1 : (byte) 0 };
515 }
516
517
518
519
520
521
522 public static boolean toBoolean(final byte [] b) {
523 if (b.length != 1) {
524 throw new IllegalArgumentException("Array has wrong size: " + b.length);
525 }
526 return b[0] != (byte) 0;
527 }
528
529
530
531
532
533
534
535 public static byte[] toBytes(long val) {
536 byte [] b = new byte[8];
537 for (int i = 7; i > 0; i--) {
538 b[i] = (byte) val;
539 val >>>= 8;
540 }
541 b[0] = (byte) val;
542 return b;
543 }
544
545
546
547
548
549
550
551 public static long toLong(byte[] bytes) {
552 return toLong(bytes, 0, SIZEOF_LONG);
553 }
554
555
556
557
558
559
560
561
562
563 public static long toLong(byte[] bytes, int offset) {
564 return toLong(bytes, offset, SIZEOF_LONG);
565 }
566
567
568
569
570
571
572
573
574
575
576
577 public static long toLong(byte[] bytes, int offset, final int length) {
578 if (length != SIZEOF_LONG || offset + length > bytes.length) {
579 throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_LONG);
580 }
581 if (UnsafeComparer.isAvailable()) {
582 return toLongUnsafe(bytes, offset);
583 } else {
584 long l = 0;
585 for(int i = offset; i < offset + length; i++) {
586 l <<= 8;
587 l ^= bytes[i] & 0xFF;
588 }
589 return l;
590 }
591 }
592
593 private static IllegalArgumentException
594 explainWrongLengthOrOffset(final byte[] bytes,
595 final int offset,
596 final int length,
597 final int expectedLength) {
598 String reason;
599 if (length != expectedLength) {
600 reason = "Wrong length: " + length + ", expected " + expectedLength;
601 } else {
602 reason = "offset (" + offset + ") + length (" + length + ") exceed the"
603 + " capacity of the array: " + bytes.length;
604 }
605 return new IllegalArgumentException(reason);
606 }
607
608
609
610
611
612
613
614
615
616
617 public static int putLong(byte[] bytes, int offset, long val) {
618 if (bytes.length - offset < SIZEOF_LONG) {
619 throw new IllegalArgumentException("Not enough room to put a long at"
620 + " offset " + offset + " in a " + bytes.length + " byte array");
621 }
622 if (UnsafeComparer.isAvailable()) {
623 return putLongUnsafe(bytes, offset, val);
624 } else {
625 for(int i = offset + 7; i > offset; i--) {
626 bytes[i] = (byte) val;
627 val >>>= 8;
628 }
629 bytes[offset] = (byte) val;
630 return offset + SIZEOF_LONG;
631 }
632 }
633
634
635
636
637
638
639
640
641 public static int putLongUnsafe(byte[] bytes, int offset, long val)
642 {
643 if (UnsafeComparer.littleEndian) {
644 val = Long.reverseBytes(val);
645 }
646 UnsafeComparer.theUnsafe.putLong(bytes, (long) offset +
647 UnsafeComparer.BYTE_ARRAY_BASE_OFFSET , val);
648 return offset + SIZEOF_LONG;
649 }
650
651
652
653
654
655
656 public static float toFloat(byte [] bytes) {
657 return toFloat(bytes, 0);
658 }
659
660
661
662
663
664
665
666 public static float toFloat(byte [] bytes, int offset) {
667 return Float.intBitsToFloat(toInt(bytes, offset, SIZEOF_INT));
668 }
669
670
671
672
673
674
675
676 public static int putFloat(byte [] bytes, int offset, float f) {
677 return putInt(bytes, offset, Float.floatToRawIntBits(f));
678 }
679
680
681
682
683
684 public static byte [] toBytes(final float f) {
685
686 return Bytes.toBytes(Float.floatToRawIntBits(f));
687 }
688
689
690
691
692
693 public static double toDouble(final byte [] bytes) {
694 return toDouble(bytes, 0);
695 }
696
697
698
699
700
701
702 public static double toDouble(final byte [] bytes, final int offset) {
703 return Double.longBitsToDouble(toLong(bytes, offset, SIZEOF_LONG));
704 }
705
706
707
708
709
710
711
712 public static int putDouble(byte [] bytes, int offset, double d) {
713 return putLong(bytes, offset, Double.doubleToLongBits(d));
714 }
715
716
717
718
719
720
721
722
723 public static byte [] toBytes(final double d) {
724
725 return Bytes.toBytes(Double.doubleToRawLongBits(d));
726 }
727
728
729
730
731
732
733
734
735 public static byte[] toBytes(int val) {
736 byte [] b = new byte[4];
737 for(int i = 3; i > 0; i--) {
738 b[i] = (byte) val;
739 val >>>= 8;
740 }
741 b[0] = (byte) val;
742 return b;
743 }
744
745
746
747
748
749
750 public static int toInt(byte[] bytes) {
751 return toInt(bytes, 0, SIZEOF_INT);
752 }
753
754
755
756
757
758
759
760 public static int toInt(byte[] bytes, int offset) {
761 return toInt(bytes, offset, SIZEOF_INT);
762 }
763
764
765
766
767
768
769
770
771
772
773 public static int toInt(byte[] bytes, int offset, final int length) {
774 if (length != SIZEOF_INT || offset + length > bytes.length) {
775 throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_INT);
776 }
777 if (UnsafeComparer.isAvailable()) {
778 return toIntUnsafe(bytes, offset);
779 } else {
780 int n = 0;
781 for(int i = offset; i < (offset + length); i++) {
782 n <<= 8;
783 n ^= bytes[i] & 0xFF;
784 }
785 return n;
786 }
787 }
788
789
790
791
792
793
794
795 public static int toIntUnsafe(byte[] bytes, int offset) {
796 if (UnsafeComparer.littleEndian) {
797 return Integer.reverseBytes(UnsafeComparer.theUnsafe.getInt(bytes,
798 (long) offset + UnsafeComparer.BYTE_ARRAY_BASE_OFFSET));
799 } else {
800 return UnsafeComparer.theUnsafe.getInt(bytes,
801 (long) offset + UnsafeComparer.BYTE_ARRAY_BASE_OFFSET);
802 }
803 }
804
805
806
807
808
809
810
811 public static short toShortUnsafe(byte[] bytes, int offset) {
812 if (UnsafeComparer.littleEndian) {
813 return Short.reverseBytes(UnsafeComparer.theUnsafe.getShort(bytes,
814 (long) offset + UnsafeComparer.BYTE_ARRAY_BASE_OFFSET));
815 } else {
816 return UnsafeComparer.theUnsafe.getShort(bytes,
817 (long) offset + UnsafeComparer.BYTE_ARRAY_BASE_OFFSET);
818 }
819 }
820
821
822
823
824
825
826
827 public static long toLongUnsafe(byte[] bytes, int offset) {
828 if (UnsafeComparer.littleEndian) {
829 return Long.reverseBytes(UnsafeComparer.theUnsafe.getLong(bytes,
830 (long) offset + UnsafeComparer.BYTE_ARRAY_BASE_OFFSET));
831 } else {
832 return UnsafeComparer.theUnsafe.getLong(bytes,
833 (long) offset + UnsafeComparer.BYTE_ARRAY_BASE_OFFSET);
834 }
835 }
836
837
838
839
840
841
842
843
844
845
846 public static int readAsInt(byte[] bytes, int offset, final int length) {
847 if (offset + length > bytes.length) {
848 throw new IllegalArgumentException("offset (" + offset + ") + length (" + length
849 + ") exceed the" + " capacity of the array: " + bytes.length);
850 }
851 int n = 0;
852 for(int i = offset; i < (offset + length); i++) {
853 n <<= 8;
854 n ^= bytes[i] & 0xFF;
855 }
856 return n;
857 }
858
859
860
861
862
863
864
865
866
867
868 public static int putInt(byte[] bytes, int offset, int val) {
869 if (bytes.length - offset < SIZEOF_INT) {
870 throw new IllegalArgumentException("Not enough room to put an int at"
871 + " offset " + offset + " in a " + bytes.length + " byte array");
872 }
873 if (UnsafeComparer.isAvailable()) {
874 return putIntUnsafe(bytes, offset, val);
875 } else {
876 for(int i= offset + 3; i > offset; i--) {
877 bytes[i] = (byte) val;
878 val >>>= 8;
879 }
880 bytes[offset] = (byte) val;
881 return offset + SIZEOF_INT;
882 }
883 }
884
885
886
887
888
889
890
891
892 public static int putIntUnsafe(byte[] bytes, int offset, int val)
893 {
894 if (UnsafeComparer.littleEndian) {
895 val = Integer.reverseBytes(val);
896 }
897 UnsafeComparer.theUnsafe.putInt(bytes, (long) offset +
898 UnsafeComparer.BYTE_ARRAY_BASE_OFFSET , val);
899 return offset + SIZEOF_INT;
900 }
901
902
903
904
905
906
907 public static byte[] toBytes(short val) {
908 byte[] b = new byte[SIZEOF_SHORT];
909 b[1] = (byte) val;
910 val >>= 8;
911 b[0] = (byte) val;
912 return b;
913 }
914
915
916
917
918
919
920 public static short toShort(byte[] bytes) {
921 return toShort(bytes, 0, SIZEOF_SHORT);
922 }
923
924
925
926
927
928
929
930 public static short toShort(byte[] bytes, int offset) {
931 return toShort(bytes, offset, SIZEOF_SHORT);
932 }
933
934
935
936
937
938
939
940
941
942
943 public static short toShort(byte[] bytes, int offset, final int length) {
944 if (length != SIZEOF_SHORT || offset + length > bytes.length) {
945 throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_SHORT);
946 }
947 if (UnsafeComparer.isAvailable()) {
948 return toShortUnsafe(bytes, offset);
949 } else {
950 short n = 0;
951 n ^= bytes[offset] & 0xFF;
952 n <<= 8;
953 n ^= bytes[offset+1] & 0xFF;
954 return n;
955 }
956 }
957
958
959
960
961
962
963
964
965
966
967 public static byte[] getBytes(ByteBuffer buf) {
968 return readBytes(buf.duplicate());
969 }
970
971
972
973
974
975
976
977
978
979
980 public static int putShort(byte[] bytes, int offset, short val) {
981 if (bytes.length - offset < SIZEOF_SHORT) {
982 throw new IllegalArgumentException("Not enough room to put a short at"
983 + " offset " + offset + " in a " + bytes.length + " byte array");
984 }
985 if (UnsafeComparer.isAvailable()) {
986 return putShortUnsafe(bytes, offset, val);
987 } else {
988 bytes[offset+1] = (byte) val;
989 val >>= 8;
990 bytes[offset] = (byte) val;
991 return offset + SIZEOF_SHORT;
992 }
993 }
994
995
996
997
998
999
1000
1001
1002 public static int putShortUnsafe(byte[] bytes, int offset, short val)
1003 {
1004 if (UnsafeComparer.littleEndian) {
1005 val = Short.reverseBytes(val);
1006 }
1007 UnsafeComparer.theUnsafe.putShort(bytes, (long) offset +
1008 UnsafeComparer.BYTE_ARRAY_BASE_OFFSET , val);
1009 return offset + SIZEOF_SHORT;
1010 }
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024 public static int putAsShort(byte[] bytes, int offset, int val) {
1025 if (bytes.length - offset < SIZEOF_SHORT) {
1026 throw new IllegalArgumentException("Not enough room to put a short at"
1027 + " offset " + offset + " in a " + bytes.length + " byte array");
1028 }
1029 bytes[offset+1] = (byte) val;
1030 val >>= 8;
1031 bytes[offset] = (byte) val;
1032 return offset + SIZEOF_SHORT;
1033 }
1034
1035
1036
1037
1038
1039
1040
1041 public static byte[] toBytes(BigDecimal val) {
1042 byte[] valueBytes = val.unscaledValue().toByteArray();
1043 byte[] result = new byte[valueBytes.length + SIZEOF_INT];
1044 int offset = putInt(result, 0, val.scale());
1045 putBytes(result, offset, valueBytes, 0, valueBytes.length);
1046 return result;
1047 }
1048
1049
1050
1051
1052
1053
1054
1055
1056 public static BigDecimal toBigDecimal(byte[] bytes) {
1057 return toBigDecimal(bytes, 0, bytes.length);
1058 }
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068 public static BigDecimal toBigDecimal(byte[] bytes, int offset, final int length) {
1069 if (bytes == null || length < SIZEOF_INT + 1 ||
1070 (offset + length > bytes.length)) {
1071 return null;
1072 }
1073
1074 int scale = toInt(bytes, offset);
1075 byte[] tcBytes = new byte[length - SIZEOF_INT];
1076 System.arraycopy(bytes, offset + SIZEOF_INT, tcBytes, 0, length - SIZEOF_INT);
1077 return new BigDecimal(new BigInteger(tcBytes), scale);
1078 }
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088 public static int putBigDecimal(byte[] bytes, int offset, BigDecimal val) {
1089 if (bytes == null) {
1090 return offset;
1091 }
1092
1093 byte[] valueBytes = val.unscaledValue().toByteArray();
1094 byte[] result = new byte[valueBytes.length + SIZEOF_INT];
1095 offset = putInt(result, offset, val.scale());
1096 return putBytes(result, offset, valueBytes, 0, valueBytes.length);
1097 }
1098
1099
1100
1101
1102
1103 public static byte [] vintToBytes(final long vint) {
1104 long i = vint;
1105 int size = WritableUtils.getVIntSize(i);
1106 byte [] result = new byte[size];
1107 int offset = 0;
1108 if (i >= -112 && i <= 127) {
1109 result[offset] = (byte) i;
1110 return result;
1111 }
1112
1113 int len = -112;
1114 if (i < 0) {
1115 i ^= -1L;
1116 len = -120;
1117 }
1118
1119 long tmp = i;
1120 while (tmp != 0) {
1121 tmp = tmp >> 8;
1122 len--;
1123 }
1124
1125 result[offset++] = (byte) len;
1126
1127 len = (len < -120) ? -(len + 120) : -(len + 112);
1128
1129 for (int idx = len; idx != 0; idx--) {
1130 int shiftbits = (idx - 1) * 8;
1131 long mask = 0xFFL << shiftbits;
1132 result[offset++] = (byte)((i & mask) >> shiftbits);
1133 }
1134 return result;
1135 }
1136
1137
1138
1139
1140
1141 public static long bytesToVint(final byte [] buffer) {
1142 int offset = 0;
1143 byte firstByte = buffer[offset++];
1144 int len = WritableUtils.decodeVIntSize(firstByte);
1145 if (len == 1) {
1146 return firstByte;
1147 }
1148 long i = 0;
1149 for (int idx = 0; idx < len-1; idx++) {
1150 byte b = buffer[offset++];
1151 i = i << 8;
1152 i = i | (b & 0xFF);
1153 }
1154 return (WritableUtils.isNegativeVInt(firstByte) ? ~i : i);
1155 }
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165 @Deprecated
1166 public static long readVLong(final byte [] buffer, final int offset)
1167 throws IOException {
1168 return readAsVLong(buffer, offset);
1169 }
1170
1171
1172
1173
1174
1175
1176
1177 public static long readAsVLong(final byte [] buffer, final int offset) {
1178 byte firstByte = buffer[offset];
1179 int len = WritableUtils.decodeVIntSize(firstByte);
1180 if (len == 1) {
1181 return firstByte;
1182 }
1183 long i = 0;
1184 for (int idx = 0; idx < len-1; idx++) {
1185 byte b = buffer[offset + 1 + idx];
1186 i = i << 8;
1187 i = i | (b & 0xFF);
1188 }
1189 return (WritableUtils.isNegativeVInt(firstByte) ? ~i : i);
1190 }
1191
1192
1193
1194
1195
1196
1197 public static int compareTo(final byte [] left, final byte [] right) {
1198 return LexicographicalComparerHolder.BEST_COMPARER.
1199 compareTo(left, 0, left.length, right, 0, right.length);
1200 }
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213 public static int compareTo(byte[] buffer1, int offset1, int length1,
1214 byte[] buffer2, int offset2, int length2) {
1215 return LexicographicalComparerHolder.BEST_COMPARER.
1216 compareTo(buffer1, offset1, length1, buffer2, offset2, length2);
1217 }
1218
1219 interface Comparer<T> {
1220 int compareTo(
1221 T buffer1, int offset1, int length1, T buffer2, int offset2, int length2
1222 );
1223 }
1224
1225 @VisibleForTesting
1226 static Comparer<byte[]> lexicographicalComparerJavaImpl() {
1227 return LexicographicalComparerHolder.PureJavaComparer.INSTANCE;
1228 }
1229
1230
1231
1232
1233
1234
1235
1236
1237 @VisibleForTesting
1238 static class LexicographicalComparerHolder {
1239 static final String UNSAFE_COMPARER_NAME =
1240 LexicographicalComparerHolder.class.getName() + "$UnsafeComparer";
1241
1242 static final Comparer<byte[]> BEST_COMPARER = getBestComparer();
1243
1244
1245
1246
1247 static Comparer<byte[]> getBestComparer() {
1248 try {
1249 Class<?> theClass = Class.forName(UNSAFE_COMPARER_NAME);
1250
1251
1252 @SuppressWarnings("unchecked")
1253 Comparer<byte[]> comparer =
1254 (Comparer<byte[]>) theClass.getEnumConstants()[0];
1255 return comparer;
1256 } catch (Throwable t) {
1257 return lexicographicalComparerJavaImpl();
1258 }
1259 }
1260
1261 enum PureJavaComparer implements Comparer<byte[]> {
1262 INSTANCE;
1263
1264 @Override
1265 public int compareTo(byte[] buffer1, int offset1, int length1,
1266 byte[] buffer2, int offset2, int length2) {
1267
1268 if (buffer1 == buffer2 &&
1269 offset1 == offset2 &&
1270 length1 == length2) {
1271 return 0;
1272 }
1273
1274 int end1 = offset1 + length1;
1275 int end2 = offset2 + length2;
1276 for (int i = offset1, j = offset2; i < end1 && j < end2; i++, j++) {
1277 int a = (buffer1[i] & 0xff);
1278 int b = (buffer2[j] & 0xff);
1279 if (a != b) {
1280 return a - b;
1281 }
1282 }
1283 return length1 - length2;
1284 }
1285 }
1286
1287 @VisibleForTesting
1288 enum UnsafeComparer implements Comparer<byte[]> {
1289 INSTANCE;
1290
1291 static final Unsafe theUnsafe;
1292
1293
1294 static final int BYTE_ARRAY_BASE_OFFSET;
1295
1296 static {
1297 theUnsafe = (Unsafe) AccessController.doPrivileged(
1298 new PrivilegedAction<Object>() {
1299 @Override
1300 public Object run() {
1301 try {
1302 Field f = Unsafe.class.getDeclaredField("theUnsafe");
1303 f.setAccessible(true);
1304 return f.get(null);
1305 } catch (NoSuchFieldException e) {
1306
1307
1308 throw new Error();
1309 } catch (IllegalAccessException e) {
1310 throw new Error();
1311 }
1312 }
1313 });
1314
1315 BYTE_ARRAY_BASE_OFFSET = theUnsafe.arrayBaseOffset(byte[].class);
1316
1317
1318 if (theUnsafe.arrayIndexScale(byte[].class) != 1) {
1319 throw new AssertionError();
1320 }
1321 }
1322
1323 static final boolean littleEndian =
1324 ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN);
1325
1326
1327
1328
1329
1330 static boolean lessThanUnsignedLong(long x1, long x2) {
1331 return (x1 + Long.MIN_VALUE) < (x2 + Long.MIN_VALUE);
1332 }
1333
1334
1335
1336
1337
1338 static boolean lessThanUnsignedInt(int x1, int x2) {
1339 return (x1 & 0xffffffffL) < (x2 & 0xffffffffL);
1340 }
1341
1342
1343
1344
1345
1346 static boolean lessThanUnsignedShort(short x1, short x2) {
1347 return (x1 & 0xffff) < (x2 & 0xffff);
1348 }
1349
1350
1351
1352
1353
1354 public static boolean isAvailable()
1355 {
1356 return theUnsafe != null;
1357 }
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370 @Override
1371 public int compareTo(byte[] buffer1, int offset1, int length1,
1372 byte[] buffer2, int offset2, int length2) {
1373
1374
1375 if (buffer1 == buffer2 &&
1376 offset1 == offset2 &&
1377 length1 == length2) {
1378 return 0;
1379 }
1380 final int minLength = Math.min(length1, length2);
1381 final int minWords = minLength / SIZEOF_LONG;
1382 final long offset1Adj = offset1 + BYTE_ARRAY_BASE_OFFSET;
1383 final long offset2Adj = offset2 + BYTE_ARRAY_BASE_OFFSET;
1384
1385
1386
1387
1388
1389
1390 for (int i = 0; i < minWords * SIZEOF_LONG; i += SIZEOF_LONG) {
1391 long lw = theUnsafe.getLong(buffer1, offset1Adj + (long) i);
1392 long rw = theUnsafe.getLong(buffer2, offset2Adj + (long) i);
1393 long diff = lw ^ rw;
1394 if(littleEndian){
1395 lw = Long.reverseBytes(lw);
1396 rw = Long.reverseBytes(rw);
1397 }
1398 if (diff != 0) {
1399 return lessThanUnsignedLong(lw, rw) ? -1 : 1;
1400 }
1401 }
1402 int offset = minWords * SIZEOF_LONG;
1403
1404 if (minLength - offset >= SIZEOF_INT) {
1405 int il = theUnsafe.getInt(buffer1, offset1Adj + offset);
1406 int ir = theUnsafe.getInt(buffer2, offset2Adj + offset);
1407 if(littleEndian){
1408 il = Integer.reverseBytes(il);
1409 ir = Integer.reverseBytes(ir);
1410 }
1411 if(il != ir){
1412 return lessThanUnsignedInt(il, ir) ? -1: 1;
1413 }
1414 offset += SIZEOF_INT;
1415 }
1416 if (minLength - offset >= SIZEOF_SHORT) {
1417 short sl = theUnsafe.getShort(buffer1, offset1Adj + offset);
1418 short sr = theUnsafe.getShort(buffer2, offset2Adj + offset);
1419 if(littleEndian){
1420 sl = Short.reverseBytes(sl);
1421 sr = Short.reverseBytes(sr);
1422 }
1423 if(sl != sr){
1424 return lessThanUnsignedShort(sl, sr) ? -1: 1;
1425 }
1426 offset += SIZEOF_SHORT;
1427 }
1428 if (minLength - offset == 1) {
1429 int a = (buffer1[(int)(offset1 + offset)] & 0xff);
1430 int b = (buffer2[(int)(offset2 + offset)] & 0xff);
1431 if (a != b) {
1432 return a - b;
1433 }
1434 }
1435 return length1 - length2;
1436 }
1437 }
1438 }
1439
1440
1441
1442
1443
1444
1445 public static boolean equals(final byte [] left, final byte [] right) {
1446
1447
1448 if (left == right) return true;
1449 if (left == null || right == null) return false;
1450 if (left.length != right.length) return false;
1451 if (left.length == 0) return true;
1452
1453
1454
1455
1456 if (left[left.length - 1] != right[right.length - 1]) return false;
1457
1458 return compareTo(left, right) == 0;
1459 }
1460
1461 public static boolean equals(final byte[] left, int leftOffset, int leftLen,
1462 final byte[] right, int rightOffset, int rightLen) {
1463
1464 if (left == right &&
1465 leftOffset == rightOffset &&
1466 leftLen == rightLen) {
1467 return true;
1468 }
1469
1470 if (leftLen != rightLen) {
1471 return false;
1472 }
1473 if (leftLen == 0) {
1474 return true;
1475 }
1476
1477
1478
1479
1480 if (left[leftOffset + leftLen - 1] != right[rightOffset + rightLen - 1]) return false;
1481
1482 return LexicographicalComparerHolder.BEST_COMPARER.
1483 compareTo(left, leftOffset, leftLen, right, rightOffset, rightLen) == 0;
1484 }
1485
1486
1487
1488
1489
1490
1491
1492 public static boolean equals(byte[] a, ByteBuffer buf) {
1493 if (a == null) return buf == null;
1494 if (buf == null) return false;
1495 if (a.length != buf.remaining()) return false;
1496
1497
1498 ByteBuffer b = buf.duplicate();
1499 for (byte anA : a) {
1500 if (anA != b.get()) {
1501 return false;
1502 }
1503 }
1504 return true;
1505 }
1506
1507
1508
1509
1510
1511
1512 public static boolean startsWith(byte[] bytes, byte[] prefix) {
1513 return bytes != null && prefix != null &&
1514 bytes.length >= prefix.length &&
1515 LexicographicalComparerHolder.BEST_COMPARER.
1516 compareTo(bytes, 0, prefix.length, prefix, 0, prefix.length) == 0;
1517 }
1518
1519
1520
1521
1522
1523
1524
1525 public static int hashCode(final byte [] b) {
1526 return hashCode(b, b.length);
1527 }
1528
1529
1530
1531
1532
1533
1534
1535
1536 public static int hashCode(final byte [] b, final int length) {
1537 return WritableComparator.hashBytes(b, length);
1538 }
1539
1540
1541
1542
1543
1544
1545 public static Integer mapKey(final byte [] b) {
1546 return hashCode(b);
1547 }
1548
1549
1550
1551
1552
1553
1554
1555 public static Integer mapKey(final byte [] b, final int length) {
1556 return hashCode(b, length);
1557 }
1558
1559
1560
1561
1562
1563
1564 public static byte [] add(final byte [] a, final byte [] b) {
1565 return add(a, b, EMPTY_BYTE_ARRAY);
1566 }
1567
1568
1569
1570
1571
1572
1573
1574 public static byte [] add(final byte [] a, final byte [] b, final byte [] c) {
1575 byte [] result = new byte[a.length + b.length + c.length];
1576 System.arraycopy(a, 0, result, 0, a.length);
1577 System.arraycopy(b, 0, result, a.length, b.length);
1578 System.arraycopy(c, 0, result, a.length + b.length, c.length);
1579 return result;
1580 }
1581
1582
1583
1584
1585
1586
1587 public static byte [] head(final byte [] a, final int length) {
1588 if (a.length < length) {
1589 return null;
1590 }
1591 byte [] result = new byte[length];
1592 System.arraycopy(a, 0, result, 0, length);
1593 return result;
1594 }
1595
1596
1597
1598
1599
1600
1601 public static byte [] tail(final byte [] a, final int length) {
1602 if (a.length < length) {
1603 return null;
1604 }
1605 byte [] result = new byte[length];
1606 System.arraycopy(a, a.length - length, result, 0, length);
1607 return result;
1608 }
1609
1610
1611
1612
1613
1614
1615 public static byte [] padHead(final byte [] a, final int length) {
1616 byte [] padding = new byte[length];
1617 for (int i = 0; i < length; i++) {
1618 padding[i] = 0;
1619 }
1620 return add(padding,a);
1621 }
1622
1623
1624
1625
1626
1627
1628 public static byte [] padTail(final byte [] a, final int length) {
1629 byte [] padding = new byte[length];
1630 for (int i = 0; i < length; i++) {
1631 padding[i] = 0;
1632 }
1633 return add(a,padding);
1634 }
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645 public static byte [][] split(final byte [] a, final byte [] b, final int num) {
1646 return split(a, b, false, num);
1647 }
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661 public static byte[][] split(final byte[] a, final byte[] b,
1662 boolean inclusive, final int num) {
1663 byte[][] ret = new byte[num + 2][];
1664 int i = 0;
1665 Iterable<byte[]> iter = iterateOnSplits(a, b, inclusive, num);
1666 if (iter == null)
1667 return null;
1668 for (byte[] elem : iter) {
1669 ret[i++] = elem;
1670 }
1671 return ret;
1672 }
1673
1674
1675
1676
1677 public static Iterable<byte[]> iterateOnSplits(final byte[] a,
1678 final byte[] b, final int num)
1679 {
1680 return iterateOnSplits(a, b, false, num);
1681 }
1682
1683
1684
1685
1686 public static Iterable<byte[]> iterateOnSplits(
1687 final byte[] a, final byte[]b, boolean inclusive, final int num)
1688 {
1689 byte [] aPadded;
1690 byte [] bPadded;
1691 if (a.length < b.length) {
1692 aPadded = padTail(a, b.length - a.length);
1693 bPadded = b;
1694 } else if (b.length < a.length) {
1695 aPadded = a;
1696 bPadded = padTail(b, a.length - b.length);
1697 } else {
1698 aPadded = a;
1699 bPadded = b;
1700 }
1701 if (compareTo(aPadded,bPadded) >= 0) {
1702 throw new IllegalArgumentException("b <= a");
1703 }
1704 if (num <= 0) {
1705 throw new IllegalArgumentException("num cannot be <= 0");
1706 }
1707 byte [] prependHeader = {1, 0};
1708 final BigInteger startBI = new BigInteger(add(prependHeader, aPadded));
1709 final BigInteger stopBI = new BigInteger(add(prependHeader, bPadded));
1710 BigInteger diffBI = stopBI.subtract(startBI);
1711 if (inclusive) {
1712 diffBI = diffBI.add(BigInteger.ONE);
1713 }
1714 final BigInteger splitsBI = BigInteger.valueOf(num + 1);
1715
1716 if(diffBI.compareTo(splitsBI) < 0) {
1717 byte[] aPaddedAdditional = new byte[aPadded.length+1];
1718 byte[] bPaddedAdditional = new byte[bPadded.length+1];
1719 for (int i = 0; i < aPadded.length; i++){
1720 aPaddedAdditional[i] = aPadded[i];
1721 }
1722 for (int j = 0; j < bPadded.length; j++){
1723 bPaddedAdditional[j] = bPadded[j];
1724 }
1725 aPaddedAdditional[aPadded.length] = 0;
1726 bPaddedAdditional[bPadded.length] = 0;
1727 return iterateOnSplits(aPaddedAdditional, bPaddedAdditional, inclusive, num);
1728 }
1729 final BigInteger intervalBI;
1730 try {
1731 intervalBI = diffBI.divide(splitsBI);
1732 } catch(Exception e) {
1733 LOG.error("Exception caught during division", e);
1734 return null;
1735 }
1736
1737 final Iterator<byte[]> iterator = new Iterator<byte[]>() {
1738 private int i = -1;
1739
1740 @Override
1741 public boolean hasNext() {
1742 return i < num+1;
1743 }
1744
1745 @Override
1746 public byte[] next() {
1747 i++;
1748 if (i == 0) return a;
1749 if (i == num + 1) return b;
1750
1751 BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger.valueOf(i)));
1752 byte [] padded = curBI.toByteArray();
1753 if (padded[1] == 0)
1754 padded = tail(padded, padded.length - 2);
1755 else
1756 padded = tail(padded, padded.length - 1);
1757 return padded;
1758 }
1759
1760 @Override
1761 public void remove() {
1762 throw new UnsupportedOperationException();
1763 }
1764
1765 };
1766
1767 return new Iterable<byte[]>() {
1768 @Override
1769 public Iterator<byte[]> iterator() {
1770 return iterator;
1771 }
1772 };
1773 }
1774
1775
1776
1777
1778
1779
1780 public static int hashCode(byte[] bytes, int offset, int length) {
1781 int hash = 1;
1782 for (int i = offset; i < offset + length; i++)
1783 hash = (31 * hash) + (int) bytes[i];
1784 return hash;
1785 }
1786
1787
1788
1789
1790
1791 public static byte [][] toByteArrays(final String [] t) {
1792 byte [][] result = new byte[t.length][];
1793 for (int i = 0; i < t.length; i++) {
1794 result[i] = Bytes.toBytes(t[i]);
1795 }
1796 return result;
1797 }
1798
1799
1800
1801
1802
1803 public static byte[][] toBinaryByteArrays(final String[] t) {
1804 byte[][] result = new byte[t.length][];
1805 for (int i = 0; i < t.length; i++) {
1806 result[i] = Bytes.toBytesBinary(t[i]);
1807 }
1808 return result;
1809 }
1810
1811
1812
1813
1814
1815
1816 public static byte [][] toByteArrays(final String column) {
1817 return toByteArrays(toBytes(column));
1818 }
1819
1820
1821
1822
1823
1824
1825 public static byte [][] toByteArrays(final byte [] column) {
1826 byte [][] result = new byte[1][];
1827 result[0] = column;
1828 return result;
1829 }
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846 public static int binarySearch(byte [][]arr, byte []key, int offset,
1847 int length, RawComparator<?> comparator) {
1848 int low = 0;
1849 int high = arr.length - 1;
1850
1851 while (low <= high) {
1852 int mid = (low+high) >>> 1;
1853
1854
1855 int cmp = comparator.compare(key, offset, length,
1856 arr[mid], 0, arr[mid].length);
1857
1858 if (cmp > 0)
1859 low = mid + 1;
1860
1861 else if (cmp < 0)
1862 high = mid - 1;
1863
1864 else
1865 return mid;
1866 }
1867 return - (low+1);
1868 }
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878 public static byte [] incrementBytes(byte[] value, long amount)
1879 {
1880 byte[] val = value;
1881 if (val.length < SIZEOF_LONG) {
1882
1883 byte [] newvalue;
1884 if (val[0] < 0) {
1885 newvalue = new byte[]{-1, -1, -1, -1, -1, -1, -1, -1};
1886 } else {
1887 newvalue = new byte[SIZEOF_LONG];
1888 }
1889 System.arraycopy(val, 0, newvalue, newvalue.length - val.length,
1890 val.length);
1891 val = newvalue;
1892 } else if (val.length > SIZEOF_LONG) {
1893 throw new IllegalArgumentException("Increment Bytes - value too big: " +
1894 val.length);
1895 }
1896 if(amount == 0) return val;
1897 if(val[0] < 0){
1898 return binaryIncrementNeg(val, amount);
1899 }
1900 return binaryIncrementPos(val, amount);
1901 }
1902
1903
1904 private static byte [] binaryIncrementPos(byte [] value, long amount) {
1905 long amo = amount;
1906 int sign = 1;
1907 if (amount < 0) {
1908 amo = -amount;
1909 sign = -1;
1910 }
1911 for(int i=0;i<value.length;i++) {
1912 int cur = ((int)amo % 256) * sign;
1913 amo = (amo >> 8);
1914 int val = value[value.length-i-1] & 0x0ff;
1915 int total = val + cur;
1916 if(total > 255) {
1917 amo += sign;
1918 total %= 256;
1919 } else if (total < 0) {
1920 amo -= sign;
1921 }
1922 value[value.length-i-1] = (byte)total;
1923 if (amo == 0) return value;
1924 }
1925 return value;
1926 }
1927
1928
1929 private static byte [] binaryIncrementNeg(byte [] value, long amount) {
1930 long amo = amount;
1931 int sign = 1;
1932 if (amount < 0) {
1933 amo = -amount;
1934 sign = -1;
1935 }
1936 for(int i=0;i<value.length;i++) {
1937 int cur = ((int)amo % 256) * sign;
1938 amo = (amo >> 8);
1939 int val = ((~value[value.length-i-1]) & 0x0ff) + 1;
1940 int total = cur - val;
1941 if(total >= 0) {
1942 amo += sign;
1943 } else if (total < -256) {
1944 amo -= sign;
1945 total %= 256;
1946 }
1947 value[value.length-i-1] = (byte)total;
1948 if (amo == 0) return value;
1949 }
1950 return value;
1951 }
1952
1953
1954
1955
1956 public static void writeStringFixedSize(final DataOutput out, String s,
1957 int size) throws IOException {
1958 byte[] b = toBytes(s);
1959 if (b.length > size) {
1960 throw new IOException("Trying to write " + b.length + " bytes (" +
1961 toStringBinary(b) + ") into a field of length " + size);
1962 }
1963
1964 out.writeBytes(s);
1965 for (int i = 0; i < size - s.length(); ++i)
1966 out.writeByte(0);
1967 }
1968
1969
1970
1971
1972 public static String readStringFixedSize(final DataInput in, int size)
1973 throws IOException {
1974 byte[] b = new byte[size];
1975 in.readFully(b);
1976 int n = b.length;
1977 while (n > 0 && b[n - 1] == 0)
1978 --n;
1979
1980 return toString(b, 0, n);
1981 }
1982
1983
1984
1985
1986
1987
1988
1989 public static byte [] copy(byte [] bytes) {
1990 if (bytes == null) return null;
1991 byte [] result = new byte[bytes.length];
1992 System.arraycopy(bytes, 0, result, 0, bytes.length);
1993 return result;
1994 }
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004 public static byte [] copy(byte [] bytes, final int offset, final int length) {
2005 if (bytes == null) return null;
2006 byte [] result = new byte[length];
2007 System.arraycopy(bytes, offset, result, 0, length);
2008 return result;
2009 }
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021 public static int unsignedBinarySearch(byte[] a, int fromIndex, int toIndex, byte key) {
2022 int unsignedKey = key & 0xff;
2023 int low = fromIndex;
2024 int high = toIndex - 1;
2025
2026 while (low <= high) {
2027 int mid = (low + high) >>> 1;
2028 int midVal = a[mid] & 0xff;
2029
2030 if (midVal < unsignedKey) {
2031 low = mid + 1;
2032 } else if (midVal > unsignedKey) {
2033 high = mid - 1;
2034 } else {
2035 return mid;
2036 }
2037 }
2038 return -(low + 1);
2039 }
2040
2041
2042
2043
2044
2045
2046
2047
2048 public static byte[] unsignedCopyAndIncrement(final byte[] input) {
2049 byte[] copy = copy(input);
2050 if (copy == null) {
2051 throw new IllegalArgumentException("cannot increment null array");
2052 }
2053 for (int i = copy.length - 1; i >= 0; --i) {
2054 if (copy[i] == -1) {
2055 copy[i] = 0;
2056 } else {
2057 ++copy[i];
2058 return copy;
2059 }
2060 }
2061
2062 byte[] out = new byte[copy.length + 1];
2063 out[0] = 1;
2064 System.arraycopy(copy, 0, out, 1, copy.length);
2065 return out;
2066 }
2067
2068 public static boolean equals(List<byte[]> a, List<byte[]> b) {
2069 if (a == null) {
2070 if (b == null) {
2071 return true;
2072 }
2073 return false;
2074 }
2075 if (b == null) {
2076 return false;
2077 }
2078 if (a.size() != b.size()) {
2079 return false;
2080 }
2081 for (int i = 0; i < a.size(); ++i) {
2082 if (!Bytes.equals(a.get(i), b.get(i))) {
2083 return false;
2084 }
2085 }
2086 return true;
2087 }
2088
2089 public static boolean isSorted(Collection<byte[]> arrays) {
2090 byte[] previous = new byte[0];
2091 for (byte[] array : IterableUtils.nullSafe(arrays)) {
2092 if (Bytes.compareTo(previous, array) > 0) {
2093 return false;
2094 }
2095 previous = array;
2096 }
2097 return true;
2098 }
2099
2100 public static List<byte[]> getUtf8ByteArrays(List<String> strings) {
2101 List<byte[]> byteArrays = Lists.newArrayListWithCapacity(CollectionUtils.nullSafeSize(strings));
2102 for (String s : IterableUtils.nullSafe(strings)) {
2103 byteArrays.add(Bytes.toBytes(s));
2104 }
2105 return byteArrays;
2106 }
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117 public static int indexOf(byte[] array, byte target) {
2118 for (int i = 0; i < array.length; i++) {
2119 if (array[i] == target) {
2120 return i;
2121 }
2122 }
2123 return -1;
2124 }
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137 public static int indexOf(byte[] array, byte[] target) {
2138 checkNotNull(array, "array");
2139 checkNotNull(target, "target");
2140 if (target.length == 0) {
2141 return 0;
2142 }
2143
2144 outer:
2145 for (int i = 0; i < array.length - target.length + 1; i++) {
2146 for (int j = 0; j < target.length; j++) {
2147 if (array[i + j] != target[j]) {
2148 continue outer;
2149 }
2150 }
2151 return i;
2152 }
2153 return -1;
2154 }
2155
2156
2157
2158
2159
2160
2161 public static boolean contains(byte[] array, byte target) {
2162 return indexOf(array, target) > -1;
2163 }
2164
2165
2166
2167
2168
2169
2170 public static boolean contains(byte[] array, byte[] target) {
2171 return indexOf(array, target) > -1;
2172 }
2173
2174
2175
2176
2177
2178 public static void zero(byte[] b) {
2179 zero(b, 0, b.length);
2180 }
2181
2182
2183
2184
2185
2186
2187
2188 public static void zero(byte[] b, int offset, int length) {
2189 checkPositionIndex(offset, b.length, "offset");
2190 checkArgument(length > 0, "length must be greater than 0");
2191 checkPositionIndex(offset + length, b.length, "offset + length");
2192 Arrays.fill(b, offset, offset + length, (byte) 0);
2193 }
2194
2195 private static final SecureRandom RNG = new SecureRandom();
2196
2197
2198
2199
2200
2201 public static void random(byte[] b) {
2202 RNG.nextBytes(b);
2203 }
2204
2205
2206
2207
2208
2209
2210
2211 public static void random(byte[] b, int offset, int length) {
2212 checkPositionIndex(offset, b.length, "offset");
2213 checkArgument(length > 0, "length must be greater than 0");
2214 checkPositionIndex(offset + length, b.length, "offset + length");
2215 byte[] buf = new byte[length];
2216 RNG.nextBytes(buf);
2217 System.arraycopy(buf, 0, b, offset, length);
2218 }
2219
2220
2221
2222
2223
2224
2225 public static byte[] createMaxByteArray(int maxByteCount) {
2226 byte[] maxByteArray = new byte[maxByteCount];
2227 for (int i = 0; i < maxByteArray.length; i++) {
2228 maxByteArray[i] = (byte) 0xff;
2229 }
2230 return maxByteArray;
2231 }
2232
2233
2234
2235
2236
2237
2238
2239 public static byte[] multiple(byte[] srcBytes, int multiNum) {
2240 if (multiNum <= 0) {
2241 return new byte[0];
2242 }
2243 byte[] result = new byte[srcBytes.length * multiNum];
2244 for (int i = 0; i < multiNum; i++) {
2245 System.arraycopy(srcBytes, 0, result, i * srcBytes.length,
2246 srcBytes.length);
2247 }
2248 return result;
2249 }
2250
2251
2252
2253
2254
2255 public static String toHex(byte[] b) {
2256 checkArgument(b.length > 0, "length must be greater than 0");
2257 return String.format("%x", new BigInteger(1, b));
2258 }
2259
2260
2261
2262
2263
2264
2265 public static byte[] fromHex(String hex) {
2266 checkArgument(hex.length() > 0, "length must be greater than 0");
2267 checkArgument(hex.length() % 2 == 0, "length must be a multiple of 2");
2268
2269 hex = hex.toUpperCase();
2270 byte[] b = new byte[hex.length() / 2];
2271 for (int i = 0; i < b.length; i++) {
2272 b[i] = (byte)((toBinaryFromHex((byte)hex.charAt(2 * i)) << 4) +
2273 toBinaryFromHex((byte)hex.charAt((2 * i + 1))));
2274 }
2275 return b;
2276 }
2277
2278 }