public class Bytes extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
Bytes.ByteArrayComparator
Byte array comparator class.
|
| Modifier and Type | Field and Description |
|---|---|
static Comparator<byte[]> |
BYTES_COMPARATOR
Pass this to TreeMaps where byte [] are keys.
|
static byte[] |
EMPTY_BYTE_ARRAY
Byte array of size zero.
|
static int |
SIZEOF_BOOLEAN
Size of boolean in bytes.
|
static int |
SIZEOF_BYTE
Size of byte in bytes.
|
static int |
SIZEOF_CHAR
Size of char in bytes.
|
static int |
SIZEOF_DOUBLE
Size of double in bytes.
|
static int |
SIZEOF_FLOAT
Size of float in bytes.
|
static int |
SIZEOF_INT
Size of int in bytes.
|
static int |
SIZEOF_LONG
Size of long in bytes.
|
static int |
SIZEOF_SHORT
Size of short in bytes.
|
static String |
UTF8_ENCODING
When we encode strings, we always specify UTF8 encoding.
|
| Constructor and Description |
|---|
Bytes() |
| Modifier and Type | Method and Description |
|---|---|
static byte[] |
add(byte[] a,
byte[] b)
Concatenate two byte arrays.
|
static byte[] |
add(byte[] a,
byte[] b,
byte[] c)
Concatenate three byte arrays.
|
static int |
compareTo(byte[] left,
byte[] right)
Lexicographically compare two arrays.
|
static int |
compareTo(byte[] buffer1,
int offset1,
int length1,
byte[] buffer2,
int offset2,
int length2)
Lexicographically compare two arrays.
|
static byte[] |
concat(byte[]... arrays)
Returns the values from each provided array combined into a single array.
|
static boolean |
equals(byte[] left,
byte[] right)
Checks two byte arrays for equality.
|
static boolean |
equals(byte[] left,
int leftOffset,
int leftLen,
byte[] right,
int rightOffset,
int rightLen)
Checks segments of two byte arrays for equality.
|
static byte[] |
getBytes(ByteBuffer buf)
This method will get a sequence of bytes from pos -> limit,
but will restore pos after.
|
static int |
hashBytes(byte[] bytes,
int length)
Compute hash for binary data.
|
static int |
hashBytes(byte[] bytes,
int offset,
int length)
Compute hash for binary data.
|
static int |
hashCode(byte[] b)
Compute hash for binary data.
|
static int |
hashCode(byte[] b,
int length)
Compute hash for binary data.
|
static int |
hashCode(byte[] bytes,
int offset,
int length) |
static byte[] |
head(byte[] a,
int length)
Returns first
length bytes from byte array. |
static <T> NavigableMap<byte[],T> |
immutableSortedMapOf(byte[] key,
T value)
Creates immutable sorted map with one entry with given key and value.
|
static <T> NavigableMap<byte[],T> |
immutableSortedMapOf(byte[] key1,
T value1,
byte[] key2,
T value2)
Creates immutable sorted map with two entries with given keys and values.
|
static byte[] |
incrementBytes(byte[] value,
long amount)
Bytewise binary increment/deincrement of long contained in byte array
on given amount.
|
static Iterable<byte[]> |
iterateOnSplits(byte[] a,
byte[] b,
boolean inclusive,
int num)
Iterate over keys within the passed range.
|
static Iterable<byte[]> |
iterateOnSplits(byte[] a,
byte[] b,
int num)
Iterate over keys within the passed range, splitting at an [a,b) boundary.
|
static Integer |
mapKey(byte[] b)
Returns a hash of a byte array as an Integer that can be used as key in Maps.
|
static Integer |
mapKey(byte[] b,
int length)
Returns a hash of a byte array segment as an Integer that can be used as key in Maps.
|
static byte[] |
padHead(byte[] a,
int length)
Return a byte array with value in
a plus length prepended 0 bytes. |
static byte[] |
padTail(byte[] a,
int length)
Return a byte array with value in
a plus length appended 0 bytes. |
static int |
putBigDecimal(byte[] bytes,
int offset,
BigDecimal val)
Put a BigDecimal value out to the specified byte array position.
|
static int |
putByte(byte[] bytes,
int offset,
byte b)
Write a single byte out to the specified byte array position.
|
static int |
putBytes(byte[] tgtBytes,
int tgtOffset,
byte[] srcBytes,
int srcOffset,
int srcLength)
Put bytes at the specified byte array position.
|
static int |
putDouble(byte[] bytes,
int offset,
double d)
Put a double value out to the specified byte array position.
|
static int |
putFloat(byte[] bytes,
int offset,
float f)
Put a float value out to the specified byte array position.
|
static int |
putInt(byte[] bytes,
int offset,
int val)
Put an int value out to the specified byte array position.
|
static int |
putLong(byte[] bytes,
int offset,
long val)
Put a long value out to the specified byte array position.
|
static int |
putShort(byte[] bytes,
int offset,
short val)
Put a short value out to the specified byte array position.
|
static byte[][] |
split(byte[] a,
byte[] b,
boolean inclusive,
int num)
Split passed range.
|
static byte[][] |
split(byte[] a,
byte[] b,
int num)
Split passed range.
|
static boolean |
startsWith(byte[] bytes,
byte[] prefix)
Return true if the byte array on the right is a prefix of the byte
array on the left.
|
static byte[] |
stopKeyForPrefix(byte[] prefix)
Returns the given prefix, incremented by one, in the form that will be suitable for prefix matching.
|
static byte[] |
tail(byte[] a,
int length)
Returns last
length bytes from byte array. |
static BigDecimal |
toBigDecimal(byte[] bytes)
Converts a byte array to a BigDecimal.
|
static BigDecimal |
toBigDecimal(byte[] bytes,
int offset,
int length)
Converts a byte array to a BigDecimal value.
|
static byte |
toBinaryFromHex(byte ch)
Takes a ASCII digit in the range A-F0-9 and returns
the corresponding integer/ordinal value.
|
static boolean |
toBoolean(byte[] b)
Reverses
toBytes(boolean). |
static byte[][] |
toByteArrays(byte[] column)
Returns an array of byte arrays where first and only entry is.
|
static byte[][] |
toByteArrays(String column)
Returns an array of byte arrays where first and only entry is.
|
static byte[][] |
toByteArrays(String[] t)
Returns an array of byte arrays made from passed array of Text.
|
static byte[] |
toBytes(BigDecimal val)
Convert a BigDecimal value to a byte array.
|
static byte[] |
toBytes(boolean b)
Convert a boolean to a byte array.
|
static byte[] |
toBytes(ByteBuffer bb)
Returns a new byte array, copied from the passed ByteBuffer.
|
static byte[] |
toBytes(double d)
Serialize a double as the IEEE 754 double format output.
|
static byte[] |
toBytes(float f) |
static byte[] |
toBytes(int val)
Convert an int value to a byte array.
|
static byte[] |
toBytes(long val)
Convert a long value to a byte array using big-endian.
|
static byte[] |
toBytes(short val)
Convert a short value to a byte array of
SIZEOF_SHORT bytes long. |
static byte[] |
toBytes(String s)
Converts a string to a UTF-8 byte array.
|
static byte[] |
toBytesBinary(String in) |
static double |
toDouble(byte[] bytes)
Return double made from passed bytes.
|
static double |
toDouble(byte[] bytes,
int offset)
Return double made from passed bytes.
|
static float |
toFloat(byte[] bytes)
Presumes float encoded as IEEE 754 floating-point "single format".
|
static float |
toFloat(byte[] bytes,
int offset)
Presumes float encoded as IEEE 754 floating-point "single format".
|
static String |
toHexString(byte[] bytes)
Returns a string containing each byte, in order, as a two-digit unsigned
hexadecimal number in lower case.
|
static int |
toInt(byte[] bytes)
Converts a byte array to an int value.
|
static int |
toInt(byte[] bytes,
int offset)
Converts a byte array to an int value.
|
static int |
toInt(byte[] bytes,
int offset,
int length)
Converts a byte array to an int value.
|
static long |
toLong(byte[] bytes)
Converts a byte array to a long value.
|
static long |
toLong(byte[] bytes,
int offset)
Converts a byte array to a long value.
|
static long |
toLong(byte[] bytes,
int offset,
int length)
Converts a byte array to a long value.
|
static short |
toShort(byte[] bytes)
Converts a byte array to a short value.
|
static short |
toShort(byte[] bytes,
int offset)
Converts a byte array to a short value.
|
static short |
toShort(byte[] bytes,
int offset,
int length)
Converts a byte array to a short value.
|
static String |
toString(byte[] b)
This method will convert utf8 encoded bytes into a string.
|
static String |
toString(byte[] b,
int off,
int len)
This method will convert utf8 encoded bytes into a string.
|
static String |
toString(byte[] b1,
String sep,
byte[] b2)
Joins two byte arrays together using a separator.
|
static String |
toString(ByteBuffer buf)
This method will convert the remaining bytes of a UTF8
encoded byte buffer into a string.
|
static String |
toStringBinary(byte[] b)
Write a printable representation of a byte array.
|
static String |
toStringBinary(byte[] b,
int off,
int len)
Write a printable representation of a byte array.
|
static String |
toStringBinary(ByteBuffer buf)
Converts the given byte buffer, from its array offset to its limit, to
a string.
|
static void |
writeStringFixedSize(DataOutput out,
String s,
int size)
Writes a string as a fixed-size field, padded with zeros.
|
public static final int SIZEOF_BOOLEAN
public static final int SIZEOF_BYTE
public static final int SIZEOF_CHAR
public static final int SIZEOF_DOUBLE
public static final int SIZEOF_FLOAT
public static final int SIZEOF_INT
public static final int SIZEOF_LONG
public static final int SIZEOF_SHORT
public static final Comparator<byte[]> BYTES_COMPARATOR
public static final String UTF8_ENCODING
public static final byte[] EMPTY_BYTE_ARRAY
public static int putBytes(byte[] tgtBytes,
int tgtOffset,
byte[] srcBytes,
int srcOffset,
int srcLength)
tgtBytes - the byte arraytgtOffset - position in the arraysrcBytes - array to write outsrcOffset - source offsetsrcLength - source lengthpublic static int putByte(byte[] bytes,
int offset,
byte b)
bytes - the byte arrayoffset - position in the arrayb - byte to write outpublic static byte[] toBytes(ByteBuffer bb)
bb - A ByteBufferpublic static String toString(byte[] b)
b - Presumed UTF-8 encoded byte array.bpublic static String toString(byte[] b1, String sep, byte[] b2)
b1 - The first byte array.sep - The separator to use.b2 - The second byte array.public static String toString(byte[] b, int off, int len)
b - Presumed UTF-8 encoded byte array.off - offset into arraylen - length of utf-8 sequenceb or nullpublic static String toString(ByteBuffer buf)
buf - Presumed UTF-8 encoded byte buffer.buf or nullpublic static String toStringBinary(byte[] b)
b - byte arraytoStringBinary(byte[], int, int)public static String toHexString(byte[] bytes)
public static String toStringBinary(ByteBuffer buf)
buf - a byte bufferpublic static String toStringBinary(byte[] b, int off, int len)
b - array to write outoff - offset to start atlen - length to writepublic static byte toBinaryFromHex(byte ch)
ch - The hex digit.public static byte[] toBytesBinary(String in)
public static byte[] toBytes(String s)
s - stringpublic static byte[] toBytes(boolean b)
b - valueb encoded in a byte array.public static boolean toBoolean(byte[] b)
toBytes(boolean).b - arraypublic static byte[] toBytes(long val)
val - value to convertpublic static long toLong(byte[] bytes)
toBytes(long)bytes - arraypublic static long toLong(byte[] bytes,
int offset)
SIZEOF_LONG bytes available.bytes - bytesoffset - offsetpublic static long toLong(byte[] bytes,
int offset,
int length)
bytes - array of bytesoffset - offset into arraylength - length of data (must be SIZEOF_LONG)IllegalArgumentException - if length is not SIZEOF_LONG or
if there's not enough room in the array at the offset indicated.public static int putLong(byte[] bytes,
int offset,
long val)
bytes - the byte arrayoffset - position in the arrayval - long to write outIllegalArgumentException - if the byte array given doesn't have
enough room at the offset specified.public static float toFloat(byte[] bytes)
bytes - byte arraypublic static float toFloat(byte[] bytes,
int offset)
bytes - array to convertoffset - offset into arraypublic static int putFloat(byte[] bytes,
int offset,
float f)
bytes - byte arrayoffset - offset to write tof - float valuebytespublic static byte[] toBytes(float f)
f - float valuepublic static double toDouble(byte[] bytes)
bytes - byte arraypublic static double toDouble(byte[] bytes,
int offset)
bytes - byte arrayoffset - offset where double ispublic static int putDouble(byte[] bytes,
int offset,
double d)
bytes - byte arrayoffset - offset to write tod - valuebytespublic static byte[] toBytes(double d)
d - valuepublic static byte[] toBytes(int val)
val - valuepublic static int toInt(byte[] bytes)
bytes - byte arraypublic static int toInt(byte[] bytes,
int offset)
bytes - byte arrayoffset - offset into arraypublic static int toInt(byte[] bytes,
int offset,
int length)
bytes - byte arrayoffset - offset into arraylength - length of int (has to be SIZEOF_INT)IllegalArgumentException - if length is not SIZEOF_INT or
if there's not enough room in the array at the offset indicated.public static int putInt(byte[] bytes,
int offset,
int val)
bytes - the byte arrayoffset - position in the arrayval - int to write outIllegalArgumentException - if the byte array given doesn't have
enough room at the offset specified.public static byte[] toBytes(short val)
SIZEOF_SHORT bytes long.val - valuepublic static short toShort(byte[] bytes)
bytes - byte arraypublic static short toShort(byte[] bytes,
int offset)
bytes - byte arrayoffset - offset into arraypublic static short toShort(byte[] bytes,
int offset,
int length)
bytes - byte arrayoffset - offset into arraylength - length, has to be SIZEOF_SHORTIllegalArgumentException - if length is not SIZEOF_SHORT
or if there's not enough room in the array at the offset indicated.public static byte[] getBytes(ByteBuffer buf)
buf - public static int putShort(byte[] bytes,
int offset,
short val)
bytes - the byte arrayoffset - position in the arrayval - short to write outIllegalArgumentException - if the byte array given doesn't have
enough room at the offset specified.public static byte[] toBytes(BigDecimal val)
val - public static BigDecimal toBigDecimal(byte[] bytes)
bytes - public static BigDecimal toBigDecimal(byte[] bytes, int offset, int length)
bytes - offset - length - public static int putBigDecimal(byte[] bytes,
int offset,
BigDecimal val)
bytes - the byte arrayoffset - position in the arrayval - BigDecimal to write outpublic static int compareTo(byte[] left,
byte[] right)
left - left operandright - right operandpublic static int compareTo(byte[] buffer1,
int offset1,
int length1,
byte[] buffer2,
int offset2,
int length2)
buffer1 - left operandbuffer2 - right operandoffset1 - Where to start comparing in the left bufferoffset2 - Where to start comparing in the right bufferlength1 - How much to compare from the left bufferlength2 - How much to compare from the right bufferpublic static boolean equals(byte[] left,
byte[] right)
left - left operandright - right operandpublic static boolean equals(byte[] left,
int leftOffset,
int leftLen,
byte[] right,
int rightOffset,
int rightLen)
left - left operandleftOffset - offset from which to start comparisonleftLen - length of left segmentright - right operandrightOffset - offset from which to start comparisonrightLen - length of right segmentpublic static boolean startsWith(byte[] bytes,
byte[] prefix)
public static int hashCode(byte[] b)
b - bytes to hashhashBytes(byte[], int) on the
passed in array.public static int hashCode(byte[] b,
int length)
b - valuelength - length of the valuehashBytes(byte[], int) on the
passed in array.public static int hashBytes(byte[] bytes,
int offset,
int length)
public static int hashBytes(byte[] bytes,
int length)
public static Integer mapKey(byte[] b)
b - bytes to hashb as an Integer that can be used as key in
Maps.public static Integer mapKey(byte[] b, int length)
b - bytes to hashlength - length to hashb as an Integer that can be used as key in
Maps.public static byte[] add(byte[] a,
byte[] b)
a - lower halfb - upper halfpublic static byte[] add(byte[] a,
byte[] b,
byte[] c)
a - first thirdb - second thirdc - third thirdpublic static byte[] concat(byte[]... arrays)
concat(new byte[] {a, b}, new byte[] {}, new
byte[] {c} returns the array {a, b, c}. This method is copied from google guava library.arrays - zero or more byte arrayspublic static byte[] head(byte[] a,
int length)
length bytes from byte array.a - arraylength - amount of bytes to grablength bytes from apublic static byte[] tail(byte[] a,
int length)
length bytes from byte array.a - arraylength - amount of bytes to snarflength bytes from apublic static byte[] padHead(byte[] a,
int length)
a plus length prepended 0 bytes.a - arraylength - new array sizea plus length prepended 0 bytespublic static byte[] padTail(byte[] a,
int length)
a plus length appended 0 bytes.a - arraylength - new array sizea plus length appended 0 bytespublic static byte[][] split(byte[] a,
byte[] b,
int num)
a - Beginning of rangeb - End of rangenum - Number of times to split range. Pass 1 if you want to split
the range in two; i.e. one split.public static byte[][] split(byte[] a,
byte[] b,
boolean inclusive,
int num)
a - Beginning of rangeb - End of rangeinclusive - Whether the end of range is prefix-inclusive or is
considered an exclusive boundary. Automatic splits are generally exclusive
and manual splits with an explicit range utilize an inclusive end of range.num - Number of times to split range. Pass 1 if you want to split
the range in two; i.e. one split.public static Iterable<byte[]> iterateOnSplits(byte[] a, byte[] b, int num)
public static Iterable<byte[]> iterateOnSplits(byte[] a, byte[] b, boolean inclusive, int num)
public static int hashCode(byte[] bytes,
int offset,
int length)
bytes - array to hashoffset - offset to start fromlength - length to hashpublic static byte[][] toByteArrays(String[] t)
t - operandspublic static byte[][] toByteArrays(String column)
columncolumn - operandcolumnpublic static byte[][] toByteArrays(byte[] column)
columncolumn - operandcolumnpublic static byte[] incrementBytes(byte[] value,
long amount)
value - - array of bytes containing long (length <= SIZEOF_LONG)amount - value will be incremented on (deincremented if negative)public static void writeStringFixedSize(DataOutput out, String s, int size) throws IOException
IOException@Nullable public static byte[] stopKeyForPrefix(byte[] prefix)
prefix - the prefix to increment for the stop keypublic static <T> NavigableMap<byte[],T> immutableSortedMapOf(byte[] key, T value)
key - key of the entryvalue - value of the entryNavigableMappublic static <T> NavigableMap<byte[],T> immutableSortedMapOf(byte[] key1, T value1, byte[] key2, T value2)
key1 - key of the first entryvalue1 - value of the first entrykey2 - key of the second entryvalue2 - value of the second entryNavigableMapCopyright © 2015 Cask Data, Inc. Licensed under the Apache License, Version 2.0.