Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Berkeley DB Java Edition version 4.0.103
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.sleepycat.util
Class PackedInteger

    java.lang.Object
      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. However, it so happens that packed integers in the range `0` to `630` (inclusive) are sorted correctly in a byte-by-byte comparison, and this may be useful for some applications.

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)` or for long values `readLong(byte[], int)` and `writeLong(byte[], int, long)`. To get the length of a packed integer without reading it, call `getReadIntLength(byte[], int)` or `getReadLongLength(byte[], int)`. To get the length of an unpacked integer without writing it, call `getWriteIntLength(int)` or `getWriteLongLength(long)`.

Because the same packed format is used for int and long values, stored int values may be expanded to long values without introducing a format incompatibility. You can treat previously stored packed int values as long values by calling `readLong(byte[], int)` and `getReadLongLength(byte[], int)`.

Author:
  Mark Hayes

Field Summary
`static int` | `MAX_LENGTH` The maximum number of bytes needed to store an int value (5).
`static int` | `MAX_LONG_LENGTH` The maximum number of bytes needed to store a long value (9).

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` | `getReadLongLength(byte[] buf, int off)` Returns the number of bytes that would be read by `readLong(byte[], int)`.
`static int` | `getWriteIntLength(int value)` Returns the number of bytes that would be written by `writeInt(byte[], int, int)`.
`static int` | `getWriteLongLength(long value)` Returns the number of bytes that would be written by `writeLong(byte[], int, long)`.
`static int` | `readInt(byte[] buf, int off)` Reads a packed integer at the given buffer offset and returns it.
`static long` | `readLong(byte[] buf, int off)` Reads a packed long 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.
`static int` | `writeLong(byte[] buf, int offset, long value)` Writes a packed long 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).

  See Also:
    Constant Field Values

### MAX_LONG_LENGTH

    public static final int MAX_LONG_LENGTH

  The maximum number of bytes needed to store a long value (9).

  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.

### readLong

    public static long readLong(byte[] buf,
                                int off)

  Reads a packed long 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 long 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)`.

  Because the length is stored in the first byte, this method may be called with only the first byte of the packed integer in the given buffer. This method only accesses one byte at the given offset.

  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.

### getReadLongLength

    public static int getReadLongLength(byte[] buf,
                                        int off)

  Returns the number of bytes that would be read by `readLong(byte[], int)`.

  Because the length is stored in the first byte, this method may be called with only the first byte of the packed integer in the given buffer. This method only accesses one byte at the given offset.

  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.

### writeLong

    public static int writeLong(byte[] buf,
                                int offset,
                                long value)

  Writes a packed long 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 long 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.

### getWriteLongLength

    public static int getWriteLongLength(long value)

  Returns the number of bytes that would be written by `writeLong(byte[], int, long)`.

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

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Berkeley DB Java Edition version 4.0.103
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright (c) 2004-2010 Oracle. All rights reserved.