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