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