jBittorrentAPI
Class Utils

java.lang.Object
  extended by jBittorrentAPI.Utils

public class Utils
extends java.lang.Object

A set of utility methods used by the program


Constructor Summary
Utils()
           
 
Method Summary
static boolean[] byteArray2BitArray(byte[] bytes)
          Convert a byte array to a boolean array.
static java.lang.String byteArrayToByteString(byte[] in)
          Convert a byte[] array to readable string format.
static int byteArrayToInt(byte[] b)
          Convert a byte array integer (4 bytes) to its int value
static java.lang.String byteArrayToURLString(byte[] in)
          Convert a byte array to a URL encoded string
static boolean bytesCompare(byte[] a, byte[] b)
          Compare 2 byte arrays byte to byte
static java.lang.String bytesToHex(byte[] data)
          Convenience method to convert a byte array to a hex string.
static java.lang.String byteToHex(byte data)
          Convenience method to convert a byte to a hex string.
static int byteToUnsignedInt(byte b)
           
static byte[] concat(byte[] b1, byte b2)
          Concatenate the byte array and the byte
static byte[] concat(byte[] b1, byte[] b2)
          Concatenate the 2 byte arrays
static byte[] concat2(byte[] a, byte[] b)
          Concatenate the 2 byte arrays
static void copy(byte[] in, byte[] out)
          Copy the input byte array to the output byte array
static byte[] generateID()
          Generate the client id, which is a fixed string of length 8 concatenated with 12 random bytes
static byte[] hash(byte[] hashThis)
          Compute the SHA-1 hash of the given byte array
static byte[] hash(java.nio.ByteBuffer hashThis)
          Compute the SHA-1 hash of the bytes in the given buffer
static byte[] intToByteArray(int value)
          Convert an integer value to its byte array representation
static byte[] intToByteArray4(int i)
           
static byte[] subArray(byte[] b, int offset, int length)
          Return a subarray of the byte array in parameter.
static byte[] toByteArray(java.util.BitSet bits)
           
static char toHexChar(int i)
          Convenience method to convert an int to a hex char.
static long unsignedIntToLong(byte[] b)
          Convert a byte array representing an unsigned integer (4bytes) to its long value
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utils

public Utils()
Method Detail

byteArray2BitArray

public static boolean[] byteArray2BitArray(byte[] bytes)
Convert a byte array to a boolean array. Bit 0 is represented with false, Bit 1 is represented with 1

Parameters:
bytes - byte[]
Returns:
boolean[]

byteArrayToByteString

public static java.lang.String byteArrayToByteString(byte[] in)
Convert a byte[] array to readable string format. This makes the "hex" readable!

Parameters:
in - byte[] buffer to convert to string format
Returns:
result String buffer in String format

byteArrayToInt

public static int byteArrayToInt(byte[] b)
Convert a byte array integer (4 bytes) to its int value

Parameters:
b - byte[]
Returns:
int

byteArrayToURLString

public static java.lang.String byteArrayToURLString(byte[] in)
Convert a byte array to a URL encoded string

Parameters:
in - byte[]
Returns:
String

bytesCompare

public static boolean bytesCompare(byte[] a,
                                   byte[] b)
Compare 2 byte arrays byte to byte

Parameters:
a - byte[]
b - byte[]
Returns:
boolean

bytesToHex

public static java.lang.String bytesToHex(byte[] data)
Convenience method to convert a byte array to a hex string.

Parameters:
data - the byte[] to convert
Returns:
String the converted byte[]

byteToHex

public static java.lang.String byteToHex(byte data)
Convenience method to convert a byte to a hex string.

Parameters:
data - the byte to convert
Returns:
String the converted byte

byteToUnsignedInt

public static int byteToUnsignedInt(byte b)

concat

public static byte[] concat(byte[] b1,
                            byte b2)
Concatenate the byte array and the byte

Parameters:
b1 - byte[]
b2 - byte
Returns:
byte[]

concat

public static byte[] concat(byte[] b1,
                            byte[] b2)
Concatenate the 2 byte arrays

Parameters:
b1 - byte[]
b2 - byte[]
Returns:
byte[]

concat2

public static byte[] concat2(byte[] a,
                             byte[] b)
Concatenate the 2 byte arrays

Parameters:
a - byte[]
b - byte[]
Returns:
byte[]

copy

public static void copy(byte[] in,
                        byte[] out)
Copy the input byte array to the output byte array

Parameters:
in - byte[]
out - byte[]

generateID

public static byte[] generateID()
Generate the client id, which is a fixed string of length 8 concatenated with 12 random bytes

Returns:
byte[]

hash

public static byte[] hash(byte[] hashThis)
Compute the SHA-1 hash of the given byte array

Parameters:
hashThis - byte[]
Returns:
byte[]

hash

public static byte[] hash(java.nio.ByteBuffer hashThis)
Compute the SHA-1 hash of the bytes in the given buffer

Parameters:
hashThis - ByteBuffer
Returns:
byte[]

intToByteArray

public static byte[] intToByteArray(int value)
Convert an integer value to its byte array representation

Parameters:
value - int
Returns:
byte[]

intToByteArray4

public static byte[] intToByteArray4(int i)

subArray

public static byte[] subArray(byte[] b,
                              int offset,
                              int length)
Return a subarray of the byte array in parameter.

Parameters:
b - The original array
offset - Begin index of the subarray
length - Length of the subarray
Returns:
byte[]

toByteArray

public static byte[] toByteArray(java.util.BitSet bits)

toHexChar

public static char toHexChar(int i)
Convenience method to convert an int to a hex char.

Parameters:
i - the int to convert
Returns:
char the converted char

unsignedIntToLong

public static final long unsignedIntToLong(byte[] b)
Convert a byte array representing an unsigned integer (4bytes) to its long value

Parameters:
b - byte[]
Returns:
long