com.bigdata.io
Class LongPacker

java.lang.Object
  extended by com.bigdata.io.LongPacker

public class LongPacker
extends Object

Packing utility for non-negative long values.

Version:
$Id: LongPacker.java 6279 2012-04-12 15:27:30Z thompsonbry $
Author:
Bryan Thompson

Nested Class Summary
static interface LongPacker.IByteBuffer
          Narrow interface to support packing against different buffer classes.
 
Constructor Summary
LongPacker()
           
 
Method Summary
static int getByteLength(long v)
          Return the #of non-zero bytes in the packed long value.
static int getNibbleLength(long v)
          Return the #of non-zero nibbles, counting from the first non-zero nibble in the long value.
static int packLong(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 packLong(long v, byte[] pbuf, LongPacker.IByteBuffer buf)
          Packs a non-negative long value into the minimum #of bytes in which the value can be represented and writes those bytes onto the buffer.
static int packLong(OutputStream 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(DataInput is)
          Convenience method unpacks long and throws an exception if the value exceeds Integer.MAX_VALUE.
static int unpackInt(InputStream is)
          Convenience method unpacks long and throws an exception if the value exceeds Integer.MAX_VALUE.
static long unpackLong(byte[] buf, int off)
          Unpack a long value from the buffer position.
static long unpackLong(DataInput is)
          Unpack a long value from the input stream.
static long unpackLong(InputStream is)
          Unpack a long value from the input stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LongPacker

public LongPacker()
Method Detail

packLong

public static int packLong(DataOutput os,
                           long v)
                    throws 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:
IOException

packLong

public static int packLong(OutputStream os,
                           long v)
                    throws 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:
IOException

packLong

public static final int packLong(long v,
                                 byte[] pbuf,
                                 LongPacker.IByteBuffer buf)
Packs a non-negative long value into the minimum #of bytes in which the value can be represented and writes those bytes onto the buffer. 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 interpreted 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.
  
 

Parameters:
v - The unsigned long value.
Returns:
The #of bytes onto which the unsigned long value was packed.

getNibbleLength

public static int getNibbleLength(long v)
Return the #of non-zero nibbles, counting from the first non-zero nibble in the long value. A value of 0L is considered to be one nibble for our purposes.

Parameters:
v - The long value.
Returns:
The #of nibbles in [1:16].

getByteLength

public static int getByteLength(long v)
Return the #of non-zero bytes in the packed long value.

Parameters:
v - The long value.
Returns:
The #bytes in [1:8].

unpackLong

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

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

unpackLong

public static long unpackLong(InputStream is)
                       throws IOException
Unpack a long value from the input stream.

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

unpackLong

public static final long unpackLong(byte[] buf,
                                    int off)
Unpack a long value from the buffer position.

Parameters:
buf - The buffer
off - The offset from which the data will be unpacked.
Returns:
The long value.

unpackInt

public static int unpackInt(DataInput is)
                     throws IOException
Convenience method unpacks long and throws an exception if the value exceeds Integer.MAX_VALUE.

Parameters:
is - The input stream.
Returns:
The integer value.
Throws:
IOException

unpackInt

public static int unpackInt(InputStream is)
                     throws IOException
Convenience method unpacks long and throws an exception if the value exceeds Integer.MAX_VALUE.

Parameters:
is - The input stream.
Returns:
The integer value.
Throws:
IOException


Copyright © 2006-2012 SYSTAP, LLC. All Rights Reserved.