diff --git a/CHANGES.txt b/CHANGES.txt index c861439796d..ca4e66f5236 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -15,6 +15,8 @@ Release 0.19.0 - Unreleased new table (Sishen Freecity via Stack) HBASE-886, HBASE-895 Sort the tables in the web UI, [shell] 'list' command should emit a sorted list of tables (Krzysztof Szlapinski via Stack) + HBASE-884 Double and float converters for Bytes class + (Doğacan Güney via Stack) NEW FEATURES diff --git a/src/java/org/apache/hadoop/hbase/util/Bytes.java b/src/java/org/apache/hadoop/hbase/util/Bytes.java index 8a416214ec7..1cfc358096e 100644 --- a/src/java/org/apache/hadoop/hbase/util/Bytes.java +++ b/src/java/org/apache/hadoop/hbase/util/Bytes.java @@ -27,6 +27,16 @@ public class Bytes { * Size of int in bytes */ public static final int SIZEOF_INT = Integer.SIZE/Byte.SIZE; + + /** + * Size of float in bytes + */ + public static final int SIZEOF_FLOAT = Float.SIZE/Byte.SIZE; + + /** + * Size of double in bytes + */ + public static final int SIZEOF_DOUBLE = Double.SIZE/Byte.SIZE; /** * Pass this to TreeMaps where byte [] are keys. @@ -143,6 +153,52 @@ public class Bytes { } return ByteBuffer.wrap(bytes).getInt(); } + + /** + * Convert an float value to a byte array + * @param val + * @return the byte array + */ + public static byte[] toBytes(final float val) { + ByteBuffer bb = ByteBuffer.allocate(SIZEOF_FLOAT); + bb.putFloat(val); + return bb.array(); + } + + /** + * Converts a byte array to a float value + * @param bytes + * @return the float value + */ + public static float toFloat(byte[] bytes) { + if (bytes == null || bytes.length == 0) { + return -1; + } + return ByteBuffer.wrap(bytes).getFloat(); + } + + /** + * Convert an double value to a byte array + * @param val + * @return the byte array + */ + public static byte[] toBytes(final double val) { + ByteBuffer bb = ByteBuffer.allocate(SIZEOF_DOUBLE); + bb.putDouble(val); + return bb.array(); + } + + /** + * Converts a byte array to a double value + * @param bytes + * @return the double value + */ + public static double toDouble(byte[] bytes) { + if (bytes == null || bytes.length == 0) { + return -1; + } + return ByteBuffer.wrap(bytes).getDouble(); + } /** * @param left