HBASE-884 Double and float converters for Bytes class

git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@698090 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2008-09-23 05:09:14 +00:00
parent 2dbdd19ccc
commit 0b96e8d5ec
2 changed files with 58 additions and 0 deletions

View File

@ -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

View File

@ -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