[infinispan-commits] Infinispan SVN: r1361 - trunk/core/src/main/java/org/infinispan/io.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Mon Jan 11 13:52:16 EST 2010


Author: manik.surtani at jboss.com
Date: 2010-01-11 13:52:16 -0500 (Mon, 11 Jan 2010)
New Revision: 1361

Modified:
   trunk/core/src/main/java/org/infinispan/io/UnsignedNumeric.java
Log:
Overloaded to use byte arrays

Modified: trunk/core/src/main/java/org/infinispan/io/UnsignedNumeric.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/io/UnsignedNumeric.java	2010-01-11 15:36:00 UTC (rev 1360)
+++ trunk/core/src/main/java/org/infinispan/io/UnsignedNumeric.java	2010-01-11 18:52:16 UTC (rev 1361)
@@ -68,4 +68,60 @@
       out.writeByte((byte) i);
    }
 
+     /**
+    * Reads an int stored in variable-length format.  Reads between one and five bytes.  Smaller values take fewer
+    * bytes.  Negative numbers are not supported.
+    */
+   public static int readUnsignedInt(byte[] bytes, int offset) throws IOException {
+      byte b = bytes[offset++];
+      int i = b & 0x7F;
+      for (int shift = 7; (b & 0x80) != 0; shift += 7) {
+         b = bytes[offset++];
+         i |= (b & 0x7FL) << shift;
+      }
+      return i;
+   }
+
+   /**
+    * Writes an int in a variable-length format.  Writes between one and five bytes.  Smaller values take fewer bytes.
+    * Negative numbers are not supported.
+    *
+    * @param i int to write
+    */
+   public static void writeUnsignedInt(byte[] bytes, int offset, int i) throws IOException {
+      while ((i & ~0x7F) != 0) {
+         bytes[offset++] = (byte) ((i & 0x7f) | 0x80);
+         i >>>= 7;
+      }
+      bytes[offset] = (byte) i;
+   }
+
+
+   /**
+    * Reads an int stored in variable-length format.  Reads between one and nine bytes.  Smaller values take fewer
+    * bytes.  Negative numbers are not supported.
+    */
+   public static long readUnsignedLong(byte[] bytes, int offset) throws IOException {
+      byte b = bytes[offset++];
+      long i = b & 0x7F;
+      for (int shift = 7; (b & 0x80) != 0; shift += 7) {
+         b = bytes[offset++];
+         i |= (b & 0x7FL) << shift;
+      }
+      return i;
+   }
+
+   /**
+    * Writes an int in a variable-length format.  Writes between one and nine bytes.  Smaller values take fewer bytes.
+    * Negative numbers are not supported.
+    *
+    * @param i int to write
+    */
+   public static void writeUnsignedLong(byte[] bytes, int offset, long i) throws IOException {
+      while ((i & ~0x7F) != 0) {
+         bytes[offset++] = (byte) ((i & 0x7f) | 0x80);
+         i >>>= 7;
+      }
+      bytes[offset] = (byte) i;
+   }
 }



More information about the infinispan-commits mailing list