incubator-hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edwardy...@apache.org
Subject svn commit: r697001 - /incubator/hama/trunk/src/java/org/apache/hama/util/Numeric.java
Date Fri, 19 Sep 2008 09:03:59 GMT
Author: edwardyoon
Date: Fri Sep 19 02:03:58 2008
New Revision: 697001

URL: http://svn.apache.org/viewvc?rev=697001&view=rev
Log:
Local test passed. If HBASE-884 done, we should change it.

Modified:
    incubator/hama/trunk/src/java/org/apache/hama/util/Numeric.java

Modified: incubator/hama/trunk/src/java/org/apache/hama/util/Numeric.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/util/Numeric.java?rev=697001&r1=697000&r2=697001&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/util/Numeric.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/util/Numeric.java Fri Sep 19 02:03:58 2008
@@ -19,6 +19,8 @@
  */
 package org.apache.hama.util;
 
+import java.nio.ByteBuffer;
+
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.hama.Constants;
 
@@ -26,8 +28,8 @@
  * Provides a number format conversion
  */
 public class Numeric {
-  private static long BITMASK = 0xFF;
-
+  public static final int SIZEOF_DOUBLE = Double.SIZE/Byte.SIZE;
+  
   /**
    * Bytes to integer conversion
    * 
@@ -55,30 +57,18 @@
    * @return the converted value
    */
   public static double bytesToDouble(byte[] bytes) {
-    long value = 0;
-    for (int i = 0; i < 8; i++)
-      value = ((((long)(bytes[i])) & 0xFF) << (56 - i * 8)) | value;
-    return Double.longBitsToDouble(value);
+    return ByteBuffer.wrap(bytes).getDouble();
   }
 
   /**
    * Double to bytes conversion
    * 
-   * @param doubleValue
+   * @param value
    * @return the converted value
    */
-  public static byte[] doubleToBytes(Double doubleValue) {
-    byte[] buf = new byte[8];
-    long longVal = Double.doubleToLongBits(doubleValue);
-    buf[0] = (Long.valueOf((longVal & (BITMASK << 56)) >>> 56)).byteValue();
-    buf[1] = (Long.valueOf((longVal & (BITMASK << 48)) >>> 48)).byteValue();
-    buf[2] = (Long.valueOf((longVal & (BITMASK << 40)) >>> 40)).byteValue();
-    buf[3] = (Long.valueOf((longVal & (BITMASK << 32)) >>> 32)).byteValue();
-    buf[4] = (Long.valueOf((longVal & (long)0x00000000FF000000) >>> 24)).byteValue();
-    buf[5] = (Long.valueOf((longVal & (long)0x0000000000FF0000) >>> 16)).byteValue();
-    buf[6] = (Long.valueOf((longVal & (long)0x000000000000FF00) >>>  8)).byteValue();
-    buf[7] = (Long.valueOf((longVal & (long)0x00000000000000FF))).byteValue();
-    return buf;
+  public static byte[] doubleToBytes(Double value) {
+    // If HBASE-884 done, we should change it.
+    return ByteBuffer.allocate(SIZEOF_DOUBLE).putDouble(value).array();
   }
 
   /**



Mime
View raw message