ePOST 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 2551 2005-06-06 19:01:02Z jeffh $
Author:
amislove

Field Summary
static char[] HEX_ARRAY
          The array used for conversion to hexidecimal
 
Method Summary
static int byteArrayToInt(byte[] input)
          Utility method for converting a byte[] into a int
static long byteArrayToLong(byte[] input)
          Utility method for converting a byte[] into a long
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[] 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 java.lang.String toHex(byte[] text)
          Utility method which converts a byte[] to a hexidecimal string of characters, in lower case
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 hexidecimal

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 hexidecimal 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

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

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

ePOST API

Copyright © 2001-2005 - Rice Pastry.