Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Rice Pastry API
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## rice.p2p.util
Class MathUtils

    java.lang.Object
      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 3613 2007-02-15 14:45:14Z jstewart $
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 int` | `simpleHash(byte[] b)` A simple and fast hash function for hashing an arbitirary length byte array into an 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

### simpleHash

    public static int simpleHash(byte[] b)

  A simple and fast hash function for hashing an arbitirary 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

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

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Rice Pastry API
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2001-2005 - Rice Pastry.