Rice Pastry API

rice.p2p.util
Class MathUtils

java.lang.Object
  extended by rice.p2p.util.MathUtils

public class MathUtils
extends java.lang.Object

This class contains a large number of static methods for performing math operations.

Version:
$Id: MathUtils.java 4549 2008-10-16 18:21:19Z jeffh $
Author:
amislove

Field Summary
static char[] HEX_ARRAY
          The array used for conversion to hexadecimal
 
Method Summary
static int byteArrayToInt(byte[] input)
          Utility method for converting a byte[] into a int
static int byteArrayToInt(byte[] input, int offset)
           
static long byteArrayToLong(byte[] input)
          Utility method for converting a byte[] into a long
static long byteArrayToLong(byte[] input, int offset)
           
static short byteArrayToShort(byte[] input)
           
static short byteArrayToShort(byte[] input, int offset)
           
static byte[] fromBase64(java.lang.String text)
          Utility method which converts a base64 string to a byte[]
static byte[] fromHex(java.lang.String text)
          Utility method which converts a hex string to a byte[]
protected static byte getByte(char c)
          Utility method for converting a char to a byte
static byte[] intToByteArray(int input)
          Utility method for converting a int into a byte[]
static void intToByteArray(int input, byte[] output, int offset)
          Utility method for converting a int into a byte[]
static byte intToUByte(int i)
           
static byte[] longToByteArray(long input)
          Utility method for converting a long into a byte[]
static void longToByteArray(long input, byte[] output, int offset)
          Utility method for converting a long into a byte[]
static int mod(int a, int b)
          Utility which does *proper* modding, where the result is guaranteed to be positive.
static byte[] randomBytes(int len, RandomSource random)
          Method which returns a specified number of random bytes
static int randomInt(RandomSource random)
          Method which returns a random int
static int simpleHash(byte[] b)
          A simple and fast hash function for hashing an arbitrary length byte array into an int.
static java.lang.String toBase64(byte[] text)
          Utility method which converts a byte[] to a base64 string of characters, in lower case
static java.lang.String toHex(byte[] text)
          Utility method which converts a byte[] to a hexadecimal string of characters, in lower case
static int uByteToInt(byte b)
           
static int uShortToInt(short b)
           
static byte[] xor(byte[] a, byte[] b)
          Utility method which xors two given byte arrays, of equal length, and returns the results
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HEX_ARRAY

public static final char[] HEX_ARRAY
The array used for conversion to hexadecimal

Method Detail

mod

public static int mod(int a,
                      int b)
Utility which does *proper* modding, where the result is guaranteed to be positive.

Parameters:
a - The modee
b - The value to mod by
Returns:
The result

xor

public static byte[] xor(byte[] a,
                         byte[] b)
Utility method which xors two given byte arrays, of equal length, and returns the results

Parameters:
a - The first array
b - The second array
Returns:
A new byte array, xored

randomBytes

public static byte[] randomBytes(int len,
                                 RandomSource random)
Method which returns a specified number of random bytes

Parameters:
len - The number of random bytes to generate

randomInt

public static int randomInt(RandomSource random)
Method which returns a random int

Parameters:
len - The number of random bytes to generate

toHex

public static java.lang.String toHex(byte[] text)
Utility method which converts a byte[] to a hexadecimal string of characters, in lower case

Parameters:
text - The array to convert
Returns:
A string representation

fromHex

public static byte[] fromHex(java.lang.String text)
Utility method which converts a hex string to a byte[]

Parameters:
text - The text to convert
Returns:
The bytes

toBase64

public static java.lang.String toBase64(byte[] text)
Utility method which converts a byte[] to a base64 string of characters, in lower case

Parameters:
text - The array to convert
Returns:
A string representation

fromBase64

public static byte[] fromBase64(java.lang.String text)
Utility method which converts a base64 string to a byte[]

Parameters:
text - The text to convert
Returns:
The bytes

simpleHash

public static int simpleHash(byte[] b)
A simple and fast hash function for hashing an arbitrary length byte array into an int. Not for crypto. Not to be trusted. From Wikipedia: http://en.wikipedia.org/wiki/Hash_table

Parameters:
b -
Returns:
A hash of b

getByte

protected static byte getByte(char c)
Utility method for converting a char to a byte

Parameters:
c - The char
Returns:
The byte

intToByteArray

public static byte[] intToByteArray(int input)
Utility method for converting a int into a byte[]

Parameters:
input - The log to convert
Returns:
a byte[] representation

intToByteArray

public static void intToByteArray(int input,
                                  byte[] output,
                                  int offset)
Utility method for converting a int into a byte[]

Parameters:
input - The log to convert

byteArrayToInt

public static int byteArrayToInt(byte[] input)
Utility method for converting a byte[] into a int

Parameters:
input - The byte[] to convert
Returns:
a int representation

byteArrayToInt

public static int byteArrayToInt(byte[] input,
                                 int offset)

byteArrayToShort

public static short byteArrayToShort(byte[] input)

byteArrayToShort

public static short byteArrayToShort(byte[] input,
                                     int offset)

longToByteArray

public static byte[] longToByteArray(long input)
Utility method for converting a long into a byte[]

Parameters:
input - The log to convert
Returns:
a byte[] representation

longToByteArray

public static void longToByteArray(long input,
                                   byte[] output,
                                   int offset)
Utility method for converting a long into a byte[]

Parameters:
input - The log to convert

byteArrayToLong

public static long byteArrayToLong(byte[] input)
Utility method for converting a byte[] into a long

Parameters:
input - The byte[] to convert
Returns:
a long representation

byteArrayToLong

public static long byteArrayToLong(byte[] input,
                                   int offset)

uByteToInt

public static int uByteToInt(byte b)

uShortToInt

public static int uShortToInt(short b)

intToUByte

public static byte intToUByte(int i)

Rice Pastry API

Copyright © 2001-2005 - Rice Pastry.


Imprint-Dataprotection