com.limegroup.gnutella
Class ByteOrder

java.lang.Object
  extended bycom.limegroup.gnutella.ByteOrder

public class ByteOrder
extends java.lang.Object

Various static routines for solving endian problems.


Constructor Summary
ByteOrder()
           
 
Method Summary
static int beb2int(byte[] x, int offset)
          Big-endian bytes to int.
static void int2leb(int x, byte[] buf, int offset)
          Int to little-endian bytes: writes x to buf[offset..]
static void int2leb(int x, java.io.OutputStream os)
          Int to little-endian bytes: writes x to given stream
static byte[] int2minLeb(int x)
          Returns the minimum number of bytes needed to encode x in little-endian format, assuming x is non-negative.
static int leb2int(byte[] x, int offset)
          Little-endian bytes to int
static int leb2int(byte[] x, int offset, int n)
          Little-endian bytes to int.
static int leb2int(java.io.InputStream is)
          Little-endian bytes to int - stream version
static short leb2short(byte[] x, int offset)
          Little-endian bytes to short
static short leb2short(java.io.InputStream is)
          Little-endian bytes to short - stream version
static int long2int(long l)
          Returns the int value that is closest to l.
static byte[] reverse(byte[] x)
          Returns the reverse of x.
static void short2leb(short x, byte[] buf, int offset)
          Short to little-endian bytes: writes x to buf[offset...]
static void short2leb(short x, java.io.OutputStream os)
          Short to little-endian bytes: writes x to given stream
static int ubyte2int(byte x)
          Interprets the value of x as an unsigned byte, and returns it as integer.
static int ubytes2int(short x)
          Interprets the value of x as am unsigned two-byte number
static long ubytes2long(int x)
          Interprets the value of x as an unsigned two-byte number
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteOrder

public ByteOrder()
Method Detail

reverse

public static byte[] reverse(byte[] x)
Returns the reverse of x.


leb2short

public static short leb2short(byte[] x,
                              int offset)
Little-endian bytes to short


leb2short

public static short leb2short(java.io.InputStream is)
                       throws java.io.IOException
Little-endian bytes to short - stream version

Throws:
java.io.IOException

leb2int

public static int leb2int(byte[] x,
                          int offset)
Little-endian bytes to int


beb2int

public static int beb2int(byte[] x,
                          int offset)
Big-endian bytes to int.


leb2int

public static int leb2int(java.io.InputStream is)
                   throws java.io.IOException
Little-endian bytes to int - stream version

Throws:
java.io.IOException

leb2int

public static int leb2int(byte[] x,
                          int offset,
                          int n)
                   throws java.lang.IndexOutOfBoundsException,
                          java.lang.IllegalArgumentException
Little-endian bytes to int. Unlike leb2int(x, offset), this version can read fewer than 4 bytes. If n<4, the returned value is never negative.

Parameters:
x - the source of the bytes
offset - the index to start reading bytes
n - the number of bytes to read, which must be between 1 and 4, inclusive
Returns:
the value of x[offset..offset+N] as an int, assuming x is interpreted as an unsigned little-endian number (i.e., x[offset] is LSB).
Throws:
java.lang.IllegalArgumentException - n is less than 1 or greater than 4
java.lang.IndexOutOfBoundsException - offset<0 or offset+n>x.length

short2leb

public static void short2leb(short x,
                             byte[] buf,
                             int offset)
Short to little-endian bytes: writes x to buf[offset...]


short2leb

public static void short2leb(short x,
                             java.io.OutputStream os)
                      throws java.io.IOException
Short to little-endian bytes: writes x to given stream

Throws:
java.io.IOException

int2leb

public static void int2leb(int x,
                           byte[] buf,
                           int offset)
Int to little-endian bytes: writes x to buf[offset..]


int2leb

public static void int2leb(int x,
                           java.io.OutputStream os)
                    throws java.io.IOException
Int to little-endian bytes: writes x to given stream

Throws:
java.io.IOException

int2minLeb

public static byte[] int2minLeb(int x)
                         throws java.lang.IllegalArgumentException
Returns the minimum number of bytes needed to encode x in little-endian format, assuming x is non-negative. Note that leb2int(int2leb(x))==x.

Parameters:
x - a non-negative integer
Throws:
java.lang.IllegalArgumentException - x is negative

ubyte2int

public static int ubyte2int(byte x)
Interprets the value of x as an unsigned byte, and returns it as integer. For example, ubyte2int(0xFF)==255, not -1.


ubytes2int

public static int ubytes2int(short x)
Interprets the value of x as am unsigned two-byte number


ubytes2long

public static long ubytes2long(int x)
Interprets the value of x as an unsigned two-byte number


long2int

public static int long2int(long l)
Returns the int value that is closest to l. That is, if l can fit into a 32-bit unsigned number, returns (int)l. Otherwise, returns either Integer.MAX_VALUE or Integer.MIN_VALUE as appropriate.