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