|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectccs.utils.Unsigned
public class Unsigned
When working with integers which you wish were unsigned, it's extraordinarily easy to foul up the type conversions and, say, inadvertently fill the high-order half with 1 bits. This class provides a collection of common conversion operations.
It also provides methods for handling "packed" representations of longs, and
a stand-alone readFully method which doesn't really fit, but is
needed by the packed-representation methods and is generally useful.
The compose / decompose methods each come in a base version and an "LE" version; the base version uses big-endian byte order (ie. most significant byte first, as you'd natuarally write it down) whereas the LE version uses little-endian byte order (least significant byte first). Where you get a choice, we recommend big-endian order; it's more natural and in Java you can't optimise either way so there's little to choose between them otherwise.
| Field Summary | |
|---|---|
static long |
MAXPACKED
The maximum long which can be represented in "packed" form. |
| Constructor Summary | |
|---|---|
Unsigned()
|
|
| Method Summary | |
|---|---|
static boolean |
areEqual(byte[] b1,
byte[] b2)
Test two bytestrings for equality - same length, same contents. |
static long |
combine(int left,
int right)
convert 2 logically-unsigned ints into a long. |
static int |
compose(byte[] buf,
int off)
Compose an int from its bytes in big-endian byte order. |
static int |
composeLE(byte[] buf,
int off)
Compose an int from its bytes in little-endian byte order. |
static long |
composeLong(byte[] buf,
int off)
Compose a long from its bytes in big-endian byte order. |
static long |
composeLongLE(byte[] buf,
int off)
Compose a long from its bytes in little-endian byte order. |
static void |
decompose(int x,
byte[] buf,
int off)
Decompose an int into its respective bytes in big-endian byte order. |
static void |
decomposeLE(int x,
byte[] buf,
int off)
Decompose an int into its respective bytes in little-endian byte order. |
static void |
decomposeLong(long x,
byte[] buf,
int off)
Decompose a long into its respective bytes in big-endian byte order. |
static void |
decomposeLongLE(long x,
byte[] buf,
int off)
Decompose a long into its respective bytes in little-endian byte order. |
static int |
getPackedLength(long n)
The number of bytes required to write the supplied long in "packed" format. |
static int |
highOrder(long x)
Returns the high-order (left) half of a long, as an int. |
static int |
lowOrder(long x)
Returns the low-order (right) half of a long, as an int. |
static void |
readFully(java.io.InputStream is,
byte[] wad)
Read the specified array fully, a la DataInput. |
static void |
readFully(java.io.InputStream is,
byte[] wad,
int off,
int len)
Read the specified subarray fully, a la DataInput. |
static long |
readPacked(java.io.InputStream is,
byte[] temp)
Read a packed long from a stream. |
static java.lang.String |
toString(byte[] wad)
Return a readable representation of a medium-length bytestring, such as a hash. |
static java.lang.String |
toString(byte[] wad,
int off,
int len)
Return a readable representation of a medium-length bytestring, such as a hash. |
static void |
writePacked(java.io.OutputStream os,
long n,
byte[] temp)
Write a long onto a stream, in a simple, variable-length, "packed" format whose length depends on the size of the quantity being written. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final long MAXPACKED
| Constructor Detail |
|---|
public Unsigned()
| Method Detail |
|---|
public static long combine(int left,
int right)
left - The left-hand (high-order) halfright - The right-hand (low-order) halfpublic static int highOrder(long x)
public static int lowOrder(long x)
public static void decompose(int x,
byte[] buf,
int off)
x - The source int.buf - The target buffer.off - The offset into buf.
public static void decomposeLE(int x,
byte[] buf,
int off)
x - The source int.buf - The target buffer.off - The offset into buf.
public static void decomposeLong(long x,
byte[] buf,
int off)
x - The source long.buf - The target buffer.off - The offset into buf.
public static void decomposeLongLE(long x,
byte[] buf,
int off)
x - The source long.buf - The target buffer.off - The offset into buf.
public static int compose(byte[] buf,
int off)
buf - The target buffer.off - The offset into buf.
public static int composeLE(byte[] buf,
int off)
buf - The target buffer.off - The offset into buf.
public static long composeLong(byte[] buf,
int off)
buf - The target buffer.off - The offset into buf.
public static long composeLongLE(byte[] buf,
int off)
buf - The target buffer.off - The offset into buf.
public static void readFully(java.io.InputStream is,
byte[] wad,
int off,
int len)
throws java.io.IOException
DataInputStream just for this
function.
is - The stream to read from.wad - The array to read into.off - Offset to start fromlen - Number of bytes to read.
java.io.EOFException - if the stream ends before the array has been read.
java.io.IOException
public static void readFully(java.io.InputStream is,
byte[] wad)
throws java.io.IOException
DataInputStream just for this
function.
is - The stream to read from.wad - The array to read into.
java.io.IOExceptionpublic static int getPackedLength(long n)
n - The long in question; 0 <= n <= 0x3FFFFFFFFFFFFFFFL.
public static void writePacked(java.io.OutputStream os,
long n,
byte[] temp)
throws java.io.IOException
os - The stream to write to.n - The long to write; 0 <= n <= 0x3FFFFFFFFFFFFFFFL.temp - An array at least 8 bytes long to be used as scratch space.
java.io.IOException - If os does.
public static long readPacked(java.io.InputStream is,
byte[] temp)
throws java.io.IOException
is - The stream to read from.temp - An array at least 8 bytes long to be used as scratch space.
java.io.EOFException - If the stream ends before the full value can be
retreived.
java.io.IOExceptionpublic static java.lang.String toString(byte[] wad)
wad - The bytestring to convert
public static java.lang.String toString(byte[] wad,
int off,
int len)
wad - The bytestring to convertoff - The offset in wad at which to start.len - Number of bytes of wad to use.
public static boolean areEqual(byte[] b1,
byte[] b2)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||