jdbm.helper
Class LongPacker

java.lang.Object
  extended by jdbm.helper.LongPacker

public final class LongPacker
extends java.lang.Object

Packing utility for non-negative long values.

Originaly distributed under CognitiveWeb Open Source License Version, got permission to relicense under Apache2 license

Version:
$Id: LongPacker.java,v 1.4 2006/05/02 16:20:07 thompsonbry Exp $
Author:
Bryan Thompson

Constructor Summary
LongPacker()
           
 
Method Summary
static int packInt(java.io.DataOutput os, int v)
           
static int packLong(java.io.DataOutput os, long v)
          Packs a non-negative long value into the minimum #of bytes in which the value can be represented and writes those bytes onto the output stream.
static int unpackInt(java.io.DataInput is)
           
static long unpackLong(java.io.DataInput is)
          Unpack a long value from the input stream.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LongPacker

public LongPacker()
Method Detail

packLong

public static final int packLong(java.io.DataOutput os,
                                 long v)
                          throws java.io.IOException
Packs a non-negative long value into the minimum #of bytes in which the value can be represented and writes those bytes onto the output stream. The first byte determines whether or not the long value was packed and, if packed, how many bytes were required to represent the packed long value. When the high bit of the first byte is a one (1), then the long value could not be packed and the long value is found by clearing the high bit and interpreting the first byte plus the next seven (7) bytes as a long. Otherwise the next three (3) bits are interpreted as an unsigned integer giving the #of bytes (nbytes) required to represent the packed long value. To recover the long value the high nibble is cleared and the first byte together with the next nbytes are interpeted as an unsigned long value whose leading zero bytes were not written.
    
 [0|1|2|3|4|5|6|7]
  1 - - -         nbytes = 8, clear high bit and interpret this plus the next 7 bytes as a long.
  0 1 1 1         nbytes = 7, clear high nibble and interpret this plus the next 6 bytes as a long. 
  0 1 1 0         nbytes = 6, clear high nibble and interpret this plus the next 5 bytes as a long. 
  0 1 0 1         nbytes = 5, clear high nibble and interpret this plus the next 4 bytes as a long.
  0 1 0 0         nbytes = 4, clear high nibble and interpret this plus the next 3 bytes as a long.
  0 0 1 1         nbytes = 3, clear high nibble and interpret this plus the next 3 bytes as a long.
  0 0 1 0         nbytes = 2, clear high nibble and interpret this plus the next byte as a long.
  0 0 0 1         nbytes = 1, clear high nibble.  value is the low nibble.
  
 

Throws:
java.io.IOException

unpackLong

public static final long unpackLong(java.io.DataInput is)
                             throws java.io.IOException
Unpack a long value from the input stream.

Parameters:
is - The input stream.
Returns:
The long value.
Throws:
java.io.IOException

packInt

public static final int packInt(java.io.DataOutput os,
                                int v)
                         throws java.io.IOException
Throws:
java.io.IOException

unpackInt

public static final int unpackInt(java.io.DataInput is)
                           throws java.io.IOException
Throws:
java.io.IOException


Cees de Groot (C) 2000. All rights reserved http://jdbm.sourceforge.net