HBASE-1260 Bytes utility class changes: remove usage of ByteBuffer and provide additional ByteBuffer primitives

git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@768429 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2009-04-24 22:25:09 +00:00
parent 0b8953f29b
commit 292a753757
3 changed files with 155 additions and 27 deletions

View File

@ -163,6 +163,8 @@ Release 0.20.0 - Unreleased
HBASE-1176 Javadocs in HBA should be clear about which functions are HBASE-1176 Javadocs in HBA should be clear about which functions are
asynchronous and which are synchronous asynchronous and which are synchronous
(Evgeny Ryabitskiy via Stack) (Evgeny Ryabitskiy via Stack)
HBASE-1260 Bytes utility class changes: remove usage of ByteBuffer and
provide additional ByteBuffer primitives (Jon Gray via Stack)
Release 0.19.0 - 01/21/2009 Release 0.19.0 - 01/21/2009
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -49,15 +49,20 @@ public class Bytes {
public static final int SIZEOF_INT = Integer.SIZE/Byte.SIZE; public static final int SIZEOF_INT = Integer.SIZE/Byte.SIZE;
/** /**
* Size of int in bytes * Size of short in bytes
*/ */
public static final int SIZEOF_SHORT = Short.SIZE/Byte.SIZE; public static final int SIZEOF_SHORT = Short.SIZE/Byte.SIZE;
/** /**
* Size of int in bytes * Size of float in bytes
*/ */
public static final int SIZEOF_FLOAT = Float.SIZE/Byte.SIZE; 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;
/** /**
* Size of byte in bytes * Size of byte in bytes
*/ */
@ -78,11 +83,9 @@ public class Bytes {
public ByteArrayComparator() { public ByteArrayComparator() {
super(); super();
} }
@Override
public int compare(byte [] left, byte [] right) { public int compare(byte [] left, byte [] right) {
return compareTo(left, right); return compareTo(left, right);
} }
@Override
public int compare(byte [] b1, int s1, int l1, byte [] b2, int s2, int l2) { public int compare(byte [] b1, int s1, int l1, byte [] b2, int s2, int l2) {
return compareTo(b1, s1, l1, b2, s2, l2); return compareTo(b1, s1, l1, b2, s2, l2);
} }
@ -95,12 +98,13 @@ public class Bytes {
new ByteArrayComparator(); new ByteArrayComparator();
/** /**
* Pass this to TreeMaps where byte [] are keys. * Use comparing byte arrays, byte-by-byte
*/ */
public static RawComparator<byte []> BYTES_RAWCOMPARATOR = public static RawComparator<byte []> BYTES_RAWCOMPARATOR =
new ByteArrayComparator(); new ByteArrayComparator();
/** /**
* Read byte-array written with a WritableableUtils.vint prefix.
* @param in Input to read from. * @param in Input to read from.
* @return byte array read off <code>in</code> * @return byte array read off <code>in</code>
* @throws IOException * @throws IOException
@ -117,6 +121,8 @@ public class Bytes {
} }
/** /**
* Read byte-array written with a WritableableUtils.vint prefix.
* IOException is converted to a RuntimeException.
* @param in Input to read from. * @param in Input to read from.
* @return byte array read off <code>in</code> * @return byte array read off <code>in</code>
*/ */
@ -129,6 +135,7 @@ public class Bytes {
} }
/** /**
* Write byte-array with a WritableableUtils.vint prefix.
* @param out * @param out
* @param b * @param b
* @throws IOException * @throws IOException
@ -139,6 +146,7 @@ public class Bytes {
} }
/** /**
* Write byte-array to out with a vint length prefix.
* @param out * @param out
* @param b * @param b
* @throws IOException * @throws IOException
@ -150,6 +158,15 @@ public class Bytes {
out.write(b, offset, length); out.write(b, offset, length);
} }
/**
* Write byte-array from src to tgt with a vint length prefix.
* @param tgt
* @param tgtOffset
* @param src
* @param srcOffset
* @param srcLength
* @return New offset in src array.
*/
public static int writeByteArray(final byte [] tgt, final int tgtOffset, public static int writeByteArray(final byte [] tgt, final int tgtOffset,
final byte [] src, final int srcOffset, final int srcLength) { final byte [] src, final int srcOffset, final int srcLength) {
byte [] vint = vintToBytes(srcLength); byte [] vint = vintToBytes(srcLength);
@ -160,7 +177,7 @@ public class Bytes {
} }
/** /**
* Write a long value out to the specified byte array position. * Put bytes at the specified byte array position.
* @param tgtBytes the byte array * @param tgtBytes the byte array
* @param tgtOffset position in the array * @param tgtOffset position in the array
* @param srcBytes byte to write out * @param srcBytes byte to write out
@ -185,7 +202,7 @@ public class Bytes {
} }
/** /**
* Returns a new byte array, copied from the specified ByteBuffer. * Returns a new byte array, copied from the passed ByteBuffer.
* @param bb A ByteBuffer * @param bb A ByteBuffer
* @return the byte array * @return the byte array
*/ */
@ -203,7 +220,13 @@ public class Bytes {
public static String toString(final byte [] b) { public static String toString(final byte [] b) {
return toString(b, 0, b.length); return toString(b, 0, b.length);
} }
/**
* @param b Presumed UTF-8 encoded byte array.
* @param off
* @param len
* @return String made from <code>b</code>
*/
public static String toString(final byte [] b, int off, int len) { public static String toString(final byte [] b, int off, int len) {
String result = null; String result = null;
try { try {
@ -233,6 +256,7 @@ public class Bytes {
} }
/** /**
* Convert a boolean to a byte array.
* @param b * @param b
* @return <code>b</code> encoded in a byte array. * @return <code>b</code> encoded in a byte array.
*/ */
@ -280,6 +304,7 @@ public class Bytes {
/** /**
* Converts a byte array to a long value * Converts a byte array to a long value
* @param bytes * @param bytes
* @param offset
* @return the long value * @return the long value
*/ */
public static long toLong(byte[] bytes, int offset) { public static long toLong(byte[] bytes, int offset) {
@ -289,6 +314,8 @@ public class Bytes {
/** /**
* Converts a byte array to a long value * Converts a byte array to a long value
* @param bytes * @param bytes
* @param offset
* @param length
* @return the long value * @return the long value
*/ */
public static long toLong(byte[] bytes, int offset, final int length) { public static long toLong(byte[] bytes, int offset, final int length) {
@ -305,7 +332,7 @@ public class Bytes {
} }
/** /**
* Write a long value out to the specified byte array position. * Put a long value out to the specified byte array position.
* @param bytes the byte array * @param bytes the byte array
* @param offset position in the array * @param offset position in the array
* @param val long to write out * @param val long to write out
@ -322,7 +349,79 @@ public class Bytes {
bytes[offset] = (byte)(val); bytes[offset] = (byte)(val);
return offset + SIZEOF_LONG; return offset + SIZEOF_LONG;
} }
/**
* Presumes float encoded as IEEE 754 floating-point "single format"
* @param bytes
* @return Float made from passed byte array.
*/
public static float toFloat(byte [] bytes) {
return toFloat(bytes, 0);
}
/**
* Presumes float encoded as IEEE 754 floating-point "single format"
* @param bytes
* @param offset
* @return Float made from passed byte array.
*/
private static float toFloat(byte [] bytes, int offset) {
int i = toInt(bytes, offset);
return Float.intBitsToFloat(i);
}
/**
* @param bytes
* @param offset
* @param f
* @return New offset in <code>bytes</bytes>
*/
public static int putFloat(byte [] bytes, int offset, float f) {
int i = Float.floatToRawIntBits(f);
return putInt(bytes, offset, i);
}
public static byte [] toBytes(final float f) {
// Encode it as int
int i = Float.floatToRawIntBits(f);
return Bytes.toBytes(i);
}
/**
* @param bytes
* @return Return double made from passed bytes.
*/
public static double toDouble(final byte [] bytes) {
return toDouble(bytes, 0);
}
/**
* @param bytes
* @param offset
* @return Return double made from passed bytes.
*/
public static double toDouble(final byte [] bytes, final int offset) {
long l = toLong(bytes, offset);
return Double.longBitsToDouble(l);
}
/**
* @param bytes
* @param offset
* @param d
* @return New offset into array <code>bytes</code>
*/
public static int putDouble(byte [] bytes, int offset, double d) {
long l = Double.doubleToLongBits(d);
return putLong(bytes, offset, l);
}
public static byte [] toBytes(final double d) {
// Encode it as a long
long l = Double.doubleToRawLongBits(d);
return Bytes.toBytes(l);
}
/** /**
* Convert an int value to a byte array * Convert an int value to a byte array
* @param val * @param val
@ -330,7 +429,7 @@ public class Bytes {
*/ */
public static byte[] toBytes(int val) { public static byte[] toBytes(int val) {
byte [] b = new byte[4]; byte [] b = new byte[4];
for(int i=3;i>0;i--) { for(int i = 3; i > 0; i--) {
b[i] = (byte)(val); b[i] = (byte)(val);
val >>>= 8; val >>>= 8;
} }
@ -350,6 +449,7 @@ public class Bytes {
/** /**
* Converts a byte array to an int value * Converts a byte array to an int value
* @param bytes * @param bytes
* @param offset
* @return the int value * @return the int value
*/ */
public static int toInt(byte[] bytes, int offset) { public static int toInt(byte[] bytes, int offset) {
@ -359,6 +459,8 @@ public class Bytes {
/** /**
* Converts a byte array to an int value * Converts a byte array to an int value
* @param bytes * @param bytes
* @param offset
* @param length
* @return the int value * @return the int value
*/ */
public static int toInt(byte[] bytes, int offset, final int length) { public static int toInt(byte[] bytes, int offset, final int length) {
@ -375,7 +477,7 @@ public class Bytes {
} }
/** /**
* Write an int value out to the specified byte array position. * Put an int value out to the specified byte array position.
* @param bytes the byte array * @param bytes the byte array
* @param offset position in the array * @param offset position in the array
* @param val int to write out * @param val int to write out
@ -385,7 +487,7 @@ public class Bytes {
if (bytes == null || (bytes.length - offset < SIZEOF_INT)) { if (bytes == null || (bytes.length - offset < SIZEOF_INT)) {
return offset; return offset;
} }
for(int i=offset+3;i>offset;i--) { for(int i= offset+3; i > offset; i--) {
bytes[i] = (byte)(val); bytes[i] = (byte)(val);
val >>>= 8; val >>>= 8;
} }
@ -399,7 +501,7 @@ public class Bytes {
* @return the byte array * @return the byte array
*/ */
public static byte[] toBytes(short val) { public static byte[] toBytes(short val) {
byte[] b = new byte[2]; byte[] b = new byte[SIZEOF_SHORT];
b[1] = (byte)(val); b[1] = (byte)(val);
val >>>= 8; val >>>= 8;
b[0] = (byte)(val); b[0] = (byte)(val);
@ -442,7 +544,7 @@ public class Bytes {
} }
/** /**
* Write a short value out to the specified byte array position. * Put a short value out to the specified byte array position.
* @param bytes the byte array * @param bytes the byte array
* @param offset position in the array * @param offset position in the array
* @param val short to write out * @param val short to write out
@ -689,14 +791,15 @@ public class Bytes {
* @param comparator a comparator to compare. * @param comparator a comparator to compare.
* @return index of key * @return index of key
*/ */
public static int binarySearch(byte [][]arr, byte []key, int offset, int length, public static int binarySearch(byte [][]arr, byte []key, int offset,
RawComparator<byte []> comparator) { int length, RawComparator<byte []> comparator) {
int low = 0; int low = 0;
int high = arr.length - 1; int high = arr.length - 1;
while (low <= high) { while (low <= high) {
int mid = (low+high) >>> 1; int mid = (low+high) >>> 1;
int cmp = comparator.compare(arr[mid], 0, arr[mid].length, key, offset, length); int cmp = comparator.compare(arr[mid], 0, arr[mid].length, key, offset,
length);
if (cmp < 0) if (cmp < 0)
low = mid + 1; low = mid + 1;
else if (cmp > 0) else if (cmp > 0)
@ -706,4 +809,4 @@ public class Bytes {
} }
return - (low+1); return - (low+1);
} }
} }

View File

@ -31,7 +31,23 @@ public class TestBytes extends TestCase {
assertEquals(longs[i], Bytes.toLong(b)); assertEquals(longs[i], Bytes.toLong(b));
} }
} }
public void testToFloat() throws Exception {
float [] floats = {-1f, 123.123f, Float.MAX_VALUE};
for (int i = 0; i < floats.length; i++) {
byte [] b = Bytes.toBytes(floats[i]);
assertEquals(floats[i], Bytes.toFloat(b));
}
}
public void testToDouble() throws Exception {
double [] doubles = {Double.MIN_VALUE, Double.MAX_VALUE};
for (int i = 0; i < doubles.length; i++) {
byte [] b = Bytes.toBytes(doubles[i]);
assertEquals(doubles[i], Bytes.toDouble(b));
}
}
public void testBinarySearch() throws Exception { public void testBinarySearch() throws Exception {
byte [][] arr = { byte [][] arr = {
{1}, {1},
@ -48,12 +64,19 @@ public class TestBytes extends TestCase {
byte [] key2_2 = {4}; byte [] key2_2 = {4};
byte [] key3 = {5,11}; byte [] key3 = {5,11};
assertEquals(1, Bytes.binarySearch(arr, key1, 0, 1, Bytes.BYTES_RAWCOMPARATOR)); assertEquals(1, Bytes.binarySearch(arr, key1, 0, 1,
assertEquals(0, Bytes.binarySearch(arr, key1, 1, 1, Bytes.BYTES_RAWCOMPARATOR)); Bytes.BYTES_RAWCOMPARATOR));
assertEquals(-(2+1), Arrays.binarySearch(arr, key2_2, Bytes.BYTES_COMPARATOR)); assertEquals(0, Bytes.binarySearch(arr, key1, 1, 1,
assertEquals(-(2+1), Bytes.binarySearch(arr, key2, 0, 1, Bytes.BYTES_RAWCOMPARATOR)); Bytes.BYTES_RAWCOMPARATOR));
assertEquals(4, Bytes.binarySearch(arr, key2, 1, 1, Bytes.BYTES_RAWCOMPARATOR)); assertEquals(-(2+1), Arrays.binarySearch(arr, key2_2,
assertEquals(2, Bytes.binarySearch(arr, key3, 0, 1, Bytes.BYTES_RAWCOMPARATOR)); Bytes.BYTES_COMPARATOR));
assertEquals(5, Bytes.binarySearch(arr, key3, 1, 1, Bytes.BYTES_RAWCOMPARATOR)); assertEquals(-(2+1), Bytes.binarySearch(arr, key2, 0, 1,
Bytes.BYTES_RAWCOMPARATOR));
assertEquals(4, Bytes.binarySearch(arr, key2, 1, 1,
Bytes.BYTES_RAWCOMPARATOR));
assertEquals(2, Bytes.binarySearch(arr, key3, 0, 1,
Bytes.BYTES_RAWCOMPARATOR));
assertEquals(5, Bytes.binarySearch(arr, key3, 1, 1,
Bytes.BYTES_RAWCOMPARATOR));
} }
} }