protected byte[] toBytes(String str) { if (str == null) { return new byte[] { DimensionEncoding.NULL }; } return Bytes.toBytes(str); }
/** * @param b Presumed UTF-8 encoded byte array. * @return String made from <code>b</code> */ public static String toString(final byte[] b) { if (b == null) { return null; } return toString(b, 0, b.length); }
/** * Treat the byte[] as an unsigned series of bytes, most significant bits first. Start by adding * 1 to the rightmost bit/byte and carry over all overflows to the more significant bits/bytes. * * @param input The byte[] to increment. * @return The incremented copy of "in". May be same length or 1 byte longer. */ public static byte[] unsignedCopyAndIncrement(final byte[] input) { byte[] copy = copy(input); if (copy == null) { throw new IllegalArgumentException("cannot increment null array"); } for (int i = copy.length - 1; i >= 0; --i) { if (copy[i] == -1) {// -1 is all 1-bits, which is the unsigned maximum copy[i] = 0; } else { ++copy[i]; return copy; } } // we maxed out the array byte[] out = new byte[copy.length + 1]; out[0] = 1; System.arraycopy(copy, 0, out, 1, copy.length); return out; }
/** * @param column operand * @return A byte array of a byte array where first and only entry is * <code>column</code> */ public static byte[][] toByteArrays(final String column) { return toByteArrays(toBytes(column)); }
/** * Converts the given byte buffer to a printable representation, * from the index 0 (inclusive) to the limit (exclusive), * regardless of the current position. * The position and the other index parameters are not changed. * * @param buf a byte buffer * @return a string representation of the buffer's binary contents * @see #toBytes(ByteBuffer) * @see #getBytes(ByteBuffer) */ public static String toStringBinary(ByteBuffer buf) { if (buf == null) return "null"; if (buf.hasArray()) { return toStringBinary(buf.array(), buf.arrayOffset(), buf.limit()); } return toStringBinary(toBytes(buf)); }
if (isStatistics) { long cuboidId = Bytes.toLong(key.getBytes(), 1, Bytes.SIZEOF_LONG); for (Text value : values) { HLLCounter hll = new HLLCounter(cubeConfig.getCubeStatsHLLPrecision()); String value = Bytes.toString(key.getBytes(), 1, key.getLength() - 1); logAFewRows(value); builder.addValue(value); } else { byte[] keyBytes = Bytes.copy(key.getBytes(), 1, key.getLength() - 1);
public long parseCuboid(byte[] bytes) { int offset = enableSharding ? RowConstants.ROWKEY_SHARDID_LEN : 0; return Bytes.toLong(bytes, offset, RowConstants.ROWKEY_CUBOIDID_LEN); } /**
@Override public int compareTo(ByteArray o) { if (this.data == null && o.data == null) return 0; else if (this.data == null) return -1; else if (o.data == null) return 1; else return Bytes.compareTo(this.data, this.offset, this.length, o.data, o.offset, o.length); }
return getMinId(); //searching value smaller than the smallest value in dict } else { throw new IllegalArgumentException("Value '" + Bytes.toString(value, offset, len) + "' (" + Bytes.toStringBinary(value, offset, len) + ") not exists!"); index++; if (index >= trees.size()) throw new IllegalArgumentException("Value '" + Bytes.toString(value, offset, len) + "' (" + Bytes.toStringBinary(value, offset, len) + ") not exists!"); int id = tree.getIdFromValueBytesWithoutCache(value, offset, len, roundingFlag); if (id == -1) throw new IllegalArgumentException("Value '" + Bytes.toString(value, offset, len) + "' (" + Bytes.toStringBinary(value, offset, len) + ") not exists!"); id = id + accuOffset.get(index); id += baseId;
/** * Write a printable representation of a byte array. * * @param b byte array * @return string * @see #toStringBinary(byte[], int, int) */ public static String toStringBinary(final byte[] b) { if (b == null) return "null"; return toStringBinary(b, 0, b.length); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ByteArray o = (ByteArray) obj; if (this.data == null && o.data == null) return true; else if (this.data == null || o.data == null) return false; else return Bytes.equals(this.data, this.offset, this.length, o.data, o.offset, o.length); }
byte[] bPadded; if (a.length < b.length) { aPadded = padTail(a, b.length - a.length); bPadded = b; } else if (b.length < a.length) { aPadded = a; bPadded = padTail(b, a.length - b.length); } else { aPadded = a; bPadded = b; if (compareTo(aPadded, bPadded) >= 0) { throw new IllegalArgumentException("b <= a"); final BigInteger startBI = new BigInteger(add(prependHeader, aPadded)); final BigInteger stopBI = new BigInteger(add(prependHeader, bPadded)); BigInteger diffBI = stopBI.subtract(startBI); if (inclusive) {
private ArrayList<SelfDefineSortableKey> createKeyList(List<String> strNumList, byte typeFlag) { int partationId = 0; ArrayList<SelfDefineSortableKey> keyList = new ArrayList<>(); for (String str : strNumList) { ByteBuffer keyBuffer = ByteBuffer.allocate(4096); int offset = keyBuffer.position(); keyBuffer.put(Bytes.toBytes(partationId)[3]); keyBuffer.put(Bytes.toBytes(str)); Bytes.copy(keyBuffer.array(), 1, keyBuffer.position() - offset - 1); Text outputKey = new Text(); outputKey.set(keyBuffer.array(), offset, keyBuffer.position() - offset); SelfDefineSortableKey sortableKey = new SelfDefineSortableKey(); sortableKey.init(outputKey, typeFlag); keyList.add(sortableKey); } return keyList; }
/** * Writes a string as a fixed-size field, padded with zeros. */ public static void writeStringFixedSize(final DataOutput out, String s, int size) throws IOException { byte[] b = toBytes(s); if (b.length > size) { throw new IOException("Trying to write " + b.length + " bytes (" + toStringBinary(b) + ") into a field of length " + size); } out.writeBytes(s); for (int i = 0; i < size - s.length(); ++i) out.writeByte(0); }
/** * Converts a byte array to a long value. Reverses * {@link #toBytes(long)} * * @param bytes array * @return the long value */ public static long toLong(byte[] bytes) { return toLong(bytes, 0, SIZEOF_LONG); }