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