ants.p2p.utils.donkey
Class Convert

java.lang.Object
  extended by ants.p2p.utils.donkey.Convert

public class Convert
extends java.lang.Object

Provides various conversion ( e.g. str <-> hex ) utility methods.


Constructor Summary
Convert()
           
 
Method Summary
static void appendAsHex(java.lang.StringBuffer sb, byte[] data)
          appends a byte array values to a StringBuffer as hex digits.
static void appendAsHex(java.lang.StringBuffer sb, java.lang.Byte[] data)
          appends a byte array values to a StringBuffer as hex digits.
static void appendAsHex(java.lang.StringBuffer sb, java.nio.ByteBuffer data)
          appends a ByteBuffers values to a StringBuffer as hex digits.
static java.lang.StringBuffer appendHexDigit(java.lang.StringBuffer sb, byte b)
          convience method to append a byte b to the given StringBuffer sb.
static void bitSetToBytes(java.util.BitSet bitSet, byte[] bytes, int pos)
           
static byte[] bitSetToBytes(java.util.BitSet bitSet, int realsize)
           
static java.lang.String byteArrayToIpString(byte[] barray)
           
static java.lang.String byteBufferToHexString(java.nio.ByteBuffer data)
          converts a ByteBuffer values to a String of hex digits.
static java.lang.String byteBufferToHexString(java.nio.ByteBuffer data, int offset, int length)
          converts a ByteBuffer values to a String of hex digits.
static java.util.BitSet bytesToBitset(byte[] buffer, int pos, int length)
           
static java.lang.String bytesToHexString(byte[] data)
          converts a byte array values to a String of hex digits.
static java.lang.String bytesToHexString(java.lang.Byte[] data)
          converts a Byte array values to a String of hex digits.
static java.lang.String byteToHex(byte b)
          converts the byte b to its String representation.
static int byteToInt(byte myByte)
          converts the myByte to a int.
static byte[] hexStringToBytes(java.lang.String hexString)
          converts a hexString to the byte represenation,
static byte hexToByte(java.lang.String hex, int pos)
          converts a hex String (like "FF0B") to byte[].
static byte[] intArrayToBytes(int[] iarray)
          converts an array of ints to an array of bytes using the Convert.intToByte() method.
static java.lang.String intArrayToIpString(int[] iarray)
          converts the array of integers to an InternetProtocol ver.
static byte intToByte(int value)
          converts the given int value to byte.
static byte[] ipStringToBytes(java.lang.String ipString)
          converts a textual representation of an InternetProtocol (IP ) ver.
static java.util.Locale parseLocale(java.lang.String localeAsStr)
           
static boolean stringToBoolean(java.lang.String str)
          Interprets a string as boolean value.
static long unsignedIntToLong(int i)
           
static int unsignedShortToInt(short i)
          converts a unsigned short into integer.
static int[] unsingedByteArrayToInts(byte[] barray)
          coverts an array of unsigned bytes to an array of integers using the Convert.byteToInt().
static long unsingedByteToLong(byte b)
          Interprets a byte as unsigned and cast it to long.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Convert

public Convert()
Method Detail

ipStringToBytes

public static final byte[] ipStringToBytes(java.lang.String ipString)
converts a textual representation of an InternetProtocol (IP ) ver. 4 address to an array of bytes.

Returns:
array of bytes like '1.2.3.4' -> return_bytes[3] = 1; return_bytes[2] = 2; return_bytes[1] = 3; return_bytes[0] = 4

hexStringToBytes

public static final byte[] hexStringToBytes(java.lang.String hexString)
converts a hexString to the byte represenation,


appendHexDigit

public static final java.lang.StringBuffer appendHexDigit(java.lang.StringBuffer sb,
                                                          byte b)
convience method to append a byte b to the given StringBuffer sb.


appendAsHex

public static final void appendAsHex(java.lang.StringBuffer sb,
                                     byte[] data)
appends a byte array values to a StringBuffer as hex digits. The hexdigist are 2 chars in length.


appendAsHex

public static final void appendAsHex(java.lang.StringBuffer sb,
                                     java.lang.Byte[] data)
appends a byte array values to a StringBuffer as hex digits. The hexdigist are 2 chars in length.


appendAsHex

public static final void appendAsHex(java.lang.StringBuffer sb,
                                     java.nio.ByteBuffer data)
appends a ByteBuffers values to a StringBuffer as hex digits. The hexdigist are 2 chars in length. begins at ByteBuffer.position(), appends to the StringBuffer until limit() and then sets position() back to it's start value.


byteToHex

public static final java.lang.String byteToHex(byte b)
converts the byte b to its String representation.


bytesToHexString

public static final java.lang.String bytesToHexString(byte[] data)
converts a byte array values to a String of hex digits.


bytesToHexString

public static final java.lang.String bytesToHexString(java.lang.Byte[] data)
converts a Byte array values to a String of hex digits.


byteBufferToHexString

public static final java.lang.String byteBufferToHexString(java.nio.ByteBuffer data)
converts a ByteBuffer values to a String of hex digits. we read the buffer from the current position to the end. After reading we set the old position() back. this version behaves just like bytesToHexString( data, data.position(); data.limit() )


byteBufferToHexString

public static final java.lang.String byteBufferToHexString(java.nio.ByteBuffer data,
                                                           int offset,
                                                           int length)
converts a ByteBuffer values to a String of hex digits. we read length bytes from the given offset. After reading we set the old position() back.


hexToByte

public static final byte hexToByte(java.lang.String hex,
                                   int pos)
converts a hex String (like "FF0B") to byte[]. and whats does Byte.decode(String) ?


stringToBoolean

public static final boolean stringToBoolean(java.lang.String str)
Interprets a string as boolean value. If the given @param str is not null and equals to 't(rue)' 'y(es)' ( or a localized equivalent) or '1', it will be treated as true, else false.


bytesToBitset

public static final java.util.BitSet bytesToBitset(byte[] buffer,
                                                   int pos,
                                                   int length)

bitSetToBytes

public static final void bitSetToBytes(java.util.BitSet bitSet,
                                       byte[] bytes,
                                       int pos)

bitSetToBytes

public static final byte[] bitSetToBytes(java.util.BitSet bitSet,
                                         int realsize)
Parameters:
bitSet - BitSet to convert
realsize - maximum used bits in set possibly more than bitSet.length() and less than bitSet.size().
Returns:
a byte[] k with k.length() == (realsize + 7) / 8

byteToInt

public static final int byteToInt(byte myByte)
converts the myByte to a int.


intToByte

public static final byte intToByte(int value)
converts the given int value to byte.


intArrayToBytes

public static final byte[] intArrayToBytes(int[] iarray)
converts an array of ints to an array of bytes using the Convert.intToByte() method.


unsingedByteArrayToInts

public static final int[] unsingedByteArrayToInts(byte[] barray)
coverts an array of unsigned bytes to an array of integers using the Convert.byteToInt().


intArrayToIpString

public static final java.lang.String intArrayToIpString(int[] iarray)
converts the array of integers to an InternetProtocol ver. 4 address in the form "iarray[0].iarray[1].iarray[2].iarray[3]"

Parameters:
iarray - must be of the length = 4; all other entrys are ignored.

byteArrayToIpString

public static final java.lang.String byteArrayToIpString(byte[] barray)

unsingedByteToLong

public static final long unsingedByteToLong(byte b)
Interprets a byte as unsigned and cast it to long.

Parameters:
b - the unsigned byte as java byte.
Returns:
a non-negative long k with (byte)k==b.

unsignedIntToLong

public static final long unsignedIntToLong(int i)

unsignedShortToInt

public static final int unsignedShortToInt(short i)
converts a unsigned short into integer.


parseLocale

public static final java.util.Locale parseLocale(java.lang.String localeAsStr)