Berkeley DB
version 4.5.20

com.sleepycat.util
Class PackedInteger

java.lang.Object
  extended by com.sleepycat.util.PackedInteger

public class PackedInteger
extends Object

Static methods for reading and writing packed integers.

Note that packed integers are not sorted naturally for a byte-by-byte comparison because they have a preceding length and are little endian; therefore, they are typically not used for keys.

Values in the inclusive range [-119,119] are stored in a single byte. For values outside that range, the first byte stores the sign and the number of additional bytes. The additional bytes store (abs(value) - 119) as an unsigned little endian integer.

To read and write packed integer values, call readInt(byte[], int) and writeInt(byte[], int, int). To get the length of a packed integer without reading it, call getReadIntLength(byte[], int). To get the length of an unpacked integer without writing it, call getWriteIntLength(int).

Note that the packed integer format is designed to accomodate long integers using up to 9 bytes of storage. Currently only int values are implemented, but the same format may be used in future for long values.


Field Summary
static int MAX_LENGTH
          The maximum number of bytes needed to store an int value (5).
 
Constructor Summary
PackedInteger()
           
 
Method Summary
static int getReadIntLength(byte[] buf, int off)
          Returns the number of bytes that would be read by readInt(byte[], int).
static int getWriteIntLength(int value)
          Returns the number of bytes that would be written by writeInt(byte[], int, int).
static int readInt(byte[] buf, int off)
          Reads a packed integer at the given buffer offset and returns it.
static int writeInt(byte[] buf, int offset, int value)
          Writes a packed integer starting at the given buffer offset and returns the next offset to be written.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_LENGTH

public static final int MAX_LENGTH
The maximum number of bytes needed to store an int value (5). The fifth byte is only needed for values greater than (Integer.MAX_VALUE - 119) or less than (Integer.MIN_VALUE + 119).

See Also:
Constant Field Values
Constructor Detail

PackedInteger

public PackedInteger()
Method Detail

readInt

public static int readInt(byte[] buf,
                          int off)
Reads a packed integer at the given buffer offset and returns it.

Parameters:
buf - the buffer to read from.
off - the offset in the buffer at which to start reading.
Returns:
the integer that was read.

getReadIntLength

public static int getReadIntLength(byte[] buf,
                                   int off)
Returns the number of bytes that would be read by readInt(byte[], int).

Parameters:
buf - the buffer to read from.
off - the offset in the buffer at which to start reading.
Returns:
the number of bytes that would be read.

writeInt

public static int writeInt(byte[] buf,
                           int offset,
                           int value)
Writes a packed integer starting at the given buffer offset and returns the next offset to be written.

Parameters:
buf - the buffer to write to.
offset - the offset in the buffer at which to start writing.
value - the integer to be written.
Returns:
the offset past the bytes written.

getWriteIntLength

public static int getWriteIntLength(int value)
Returns the number of bytes that would be written by writeInt(byte[], int, int).

Parameters:
value - the integer to be written.
Returns:
the number of bytes that would be used to write the given integer.

Berkeley DB
version 4.5.20

Copyright(c) 1996-2006 Oracle Corporation - All rights reserved.