Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.commons.io;
  
Helper to read and write unsigned numerics

Author(s):
Manik Surtani
Since:
4.0
 
 public class UnsignedNumeric {
   
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(ObjectInput inthrows IOException {
       byte b = in.readByte();
       int i = b & 0x7F;
       for (int shift = 7; (b & 0x80) != 0; shift += 7) {
          b = in.readByte();
          i |= (b & 0x7FL) << shift;
       }
       return i;
    }
 
    public static int readUnsignedInt(InputStream inthrows IOException {
       int b = in.read();
       int i = b & 0x7F;
       for (int shift = 7; (b & 0x80) != 0; shift += 7) {
          b = in.read();
          i |= (b & 0x7FL) << shift;
       }
       return i;
    }
 
    public static int readUnsignedInt(java.nio.ByteBuffer in) {
       int b = in.get();
       int i = b & 0x7F;
       for (int shift = 7; (b & 0x80) != 0; shift += 7) {
          b = in.get();
          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.

Parameters:
i int to write
 
    public static void writeUnsignedInt(ObjectOutput outint ithrows IOException {
       while ((i & ~0x7F) != 0) {
          out.writeByte((byte) ((i & 0x7f) | 0x80));
          i >>>= 7;
       }
       out.writeByte((bytei);
    }
 
    public static void writeUnsignedInt(OutputStream outint ithrows IOException {
       while ((i & ~0x7F) != 0) {
          out.write((byte) ((i & 0x7f) | 0x80));
          i >>>= 7;
       }
       out.write((bytei);
    }
 
    public static byte sizeUnsignedInt(int i) {
       byte size = 1;
       while ((i & ~0x7F) != 0) {
          size += 1;
          i >>>= 7;
       }
       return size;
    }
 
    public static void writeUnsignedInt(java.nio.ByteBuffer outint i) {
       while ((i & ~0x7F) != 0) {
          out.put((byte) ((i & 0x7f) | 0x80));
          i >>>= 7;
       }
       out.put((bytei);
    }


   
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(ObjectInput inthrows IOException {
       byte b = in.readByte();
       long i = b & 0x7F;
       for (int shift = 7; (b & 0x80) != 0; shift += 7) {
          b = in.readByte();
          i |= (b & 0x7FL) << shift;
      }
      return i;
   }
   public static long readUnsignedLong(InputStream inthrows IOException {
      int b = in.read();
      long i = b & 0x7F;
      for (int shift = 7; (b & 0x80) != 0; shift += 7) {
         b = in.read();
         i |= (b & 0x7FL) << shift;
      }
      return i;
   }
   public static long readUnsignedLong(java.nio.ByteBuffer in) {
      int b = in.get();
      long i = b & 0x7F;
      for (int shift = 7; (b & 0x80) != 0; shift += 7) {
         b = in.get();
         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.

Parameters:
i int to write
   public static void writeUnsignedLong(ObjectOutput outlong ithrows IOException {
      while ((i & ~0x7F) != 0) {
         out.writeByte((byte) ((i & 0x7f) | 0x80));
         i >>>= 7;
      }
      out.writeByte((bytei);
   }
   public static void writeUnsignedLong(OutputStream outlong ithrows IOException {
      while ((i & ~0x7F) != 0) {
         out.write((byte) ((i & 0x7f) | 0x80));
         i >>>= 7;
      }
      out.write((bytei);
   }
   public static void writeUnsignedLong(java.nio.ByteBuffer outlong i) {
      while ((i & ~0x7F) != 0) {
         out.put((byte) ((i & 0x7f) | 0x80));
         i >>>= 7;
      }
      out.put((bytei);
   }

     
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[] bytesint offset) {
      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.

Parameters:
i int to write
   public static void writeUnsignedInt(byte[] bytesint offsetint i) {
      while ((i & ~0x7F) != 0) {
         bytes[offset++] = (byte) ((i & 0x7f) | 0x80);
         i >>>= 7;
      }
      bytes[offset] = (bytei;
   }


   
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[] bytesint offset) {
      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.

Parameters:
i int to write
   public static void writeUnsignedLong(byte[] bytesint offsetlong i) {
      while ((i & ~0x7F) != 0) {
         bytes[offset++] = (byte) ((i & 0x7f) | 0x80);
         i >>>= 7;
      }
      bytes[offset] = (bytei;
   }
New to GrepCode? Check out our FAQ X