|
Berkeley DB version 4.8.30 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.sleepycat.util.PackedInteger
public class PackedInteger
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)
.
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 |
---|
public static final int MAX_LENGTH
public static final int MAX_LONG_LENGTH
Constructor Detail |
---|
public PackedInteger()
Method Detail |
---|
public static int readInt(byte[] buf, int off)
buf
- the buffer to read from.off
- the offset in the buffer at which to start reading.
public static long readLong(byte[] buf, int off)
buf
- the buffer to read from.off
- the offset in the buffer at which to start reading.
public static int getReadIntLength(byte[] buf, int off)
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.
buf
- the buffer to read from.off
- the offset in the buffer at which to start reading.
public static int getReadLongLength(byte[] buf, int off)
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.
buf
- the buffer to read from.off
- the offset in the buffer at which to start reading.
public static int writeInt(byte[] buf, int offset, int value)
buf
- the buffer to write to.offset
- the offset in the buffer at which to start writing.value
- the integer to be written.
public static int writeLong(byte[] buf, int offset, long value)
buf
- the buffer to write to.offset
- the offset in the buffer at which to start writing.value
- the long integer to be written.
public static int getWriteIntLength(int value)
writeInt(byte[], int, int)
.
value
- the integer to be written.
public static int getWriteLongLength(long value)
writeLong(byte[], int, long)
.
value
- the long integer to be written.
|
Berkeley DB version 4.8.30 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |