HBASE-3902 Add Bytes.toBigDecimal and Bytes.toBytes(BigDecimal)

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1125098 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2011-05-19 21:02:15 +00:00
parent e90a9858f4
commit 5fc468c60c
2 changed files with 78 additions and 1 deletions

View File

@ -264,12 +264,14 @@ Release 0.90.4 - Unreleased
HBASE-3895 Fix order of parameters after HBASE-1511 HBASE-3895 Fix order of parameters after HBASE-1511
HBASE-3874 ServerShutdownHandler fails on NPE if a plan has a random HBASE-3874 ServerShutdownHandler fails on NPE if a plan has a random
region assignment region assignment
HBASE-3902 Add Bytes.toBigDecimal and Bytes.toBytes(BigDecimal)
(Vaibhav Puranik)
IMPROVEMENT IMPROVEMENT
HBASE-3882 hbase-config.sh needs to be updated so it can auto-detects the HBASE-3882 hbase-config.sh needs to be updated so it can auto-detects the
sun jre provided by RHEL6 (Roman Shaposhnik) sun jre provided by RHEL6 (Roman Shaposhnik)
Release 0.90.3 - Unreleased Release 0.90.3 - May 19th, 2011
BUG FIXES BUG FIXES
HBASE-3746 Clean up CompressionTest to not directly reference HBASE-3746 Clean up CompressionTest to not directly reference

View File

@ -764,6 +764,81 @@ public class Bytes {
return offset + SIZEOF_SHORT; return offset + SIZEOF_SHORT;
} }
/**
* Convert a BigDecimal value to a byte array
*
* @param val
* @return the byte array
*/
public static byte[] toBytes(BigDecimal val) {
byte[] valueBytes = val.unscaledValue().toByteArray();
byte[] result = new byte[valueBytes.length + SIZEOF_INT];
int offset = putInt(result, 0, val.scale());
putBytes(result, offset, valueBytes, 0, valueBytes.length);
return result;
}
/**
* Converts a byte array to a BigDecimal
*
* @param bytes
* @return the char value
*/
public static BigDecimal toBigDecimal(byte[] bytes) {
return toBigDecimal(bytes, 0, bytes.length);
}
/**
* Converts a byte array to a BigDecimal value
*
* @param bytes
* @param offset
* @return the char value
*/
public static BigDecimal toBigDecimal(byte[] bytes, int offset) {
return toBigDecimal(bytes, offset, bytes.length);
}
/**
* Converts a byte array to a BigDecimal value
*
* @param bytes
* @param offset
* @param length
* @return the char value
*/
public static BigDecimal toBigDecimal(byte[] bytes, int offset, final int length) {
if (bytes == null || length < SIZEOF_INT + 1 ||
(offset + length > bytes.length)) {
return null;
}
int scale = toInt(bytes, 0);
byte[] tcBytes = new byte[length - SIZEOF_INT];
System.arraycopy(bytes, SIZEOF_INT, tcBytes, 0, length - SIZEOF_INT);
return new BigDecimal(new BigInteger(tcBytes), scale);
}
/**
* Put a BigDecimal value out to the specified byte array position.
*
* @param bytes the byte array
* @param offset position in the array
* @param val BigDecimal to write out
* @return incremented offset
*/
public static int putBigDecimal(byte[] bytes, int offset, BigDecimal val) {
if (bytes == null) {
return offset;
}
byte[] valueBytes = val.unscaledValue().toByteArray();
byte[] result = new byte[valueBytes.length + SIZEOF_INT];
offset = putInt(result, offset, val.scale());
return putBytes(result, offset, valueBytes, 0, valueBytes.length);
}
/** /**
* @param vint Integer to make a vint of. * @param vint Integer to make a vint of.
* @return Vint as bytes array. * @return Vint as bytes array.