Many bugfixes and improvements
This commit is contained in:
parent
c3e3bbf787
commit
363e0c6cfc
52 changed files with 2021 additions and 982 deletions
|
|
@ -15,9 +15,8 @@ public class Converter {
|
|||
/**
|
||||
* Converts an object to an array of bytes.
|
||||
*
|
||||
* @param object the object to convert.
|
||||
* @return the associated byte array.
|
||||
* @throws IOException
|
||||
* @param object the object to convert.
|
||||
* @return the associated byte array.
|
||||
*/
|
||||
public static byte[] toBytes(Object object) throws IOException{
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
|
@ -33,8 +32,8 @@ public class Converter {
|
|||
/**
|
||||
* Converts a Integer to an byte array
|
||||
*
|
||||
* @param num is the number to convert
|
||||
* @return an byte array of four bytes
|
||||
* @param num is the number to convert
|
||||
* @return an byte array of four bytes
|
||||
*/
|
||||
public static byte[] toBytes(int num){
|
||||
return new byte[]{
|
||||
|
|
@ -45,22 +44,62 @@ public class Converter {
|
|||
}
|
||||
|
||||
/**
|
||||
* Converts a Integer to an byte
|
||||
* Converts a Integer to a byte
|
||||
*
|
||||
* @param num is the number to convert
|
||||
* @return an byte
|
||||
* @param num is the number to convert
|
||||
* @return an byte
|
||||
*/
|
||||
public static byte toByte(int num){
|
||||
return (byte)(num & 0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts hex chars to a byte
|
||||
*
|
||||
* @param quad1 is the first hex value
|
||||
* @param quad2 is the second hex value
|
||||
* @return a byte that corresponds to the hex
|
||||
*/
|
||||
public static int hexToByte( char quad1, char quad2){
|
||||
byte b = hexToByte( quad2 );
|
||||
b |= hexToByte( quad1 ) << 4;
|
||||
return toInt(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a hex chars to a byte
|
||||
*
|
||||
* @param quad1 is the hex value
|
||||
* @return a byte that corresponds to the hex
|
||||
*/
|
||||
public static byte hexToByte( char hex ){
|
||||
switch( Character.toLowerCase(hex) ){
|
||||
case '0': return 0x00;
|
||||
case '1': return 0x01;
|
||||
case '2': return 0x02;
|
||||
case '3': return 0x03;
|
||||
case '4': return 0x04;
|
||||
case '5': return 0x05;
|
||||
case '6': return 0x06;
|
||||
case '7': return 0x07;
|
||||
case '8': return 0x08;
|
||||
case '9': return 0x09;
|
||||
case 'a': return 0x0a;
|
||||
case 'b': return 0x0b;
|
||||
case 'c': return 0x0c;
|
||||
case 'd': return 0x0d;
|
||||
case 'e': return 0x0e;
|
||||
case 'f': return 0x0f;
|
||||
}
|
||||
return (byte)0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an array of bytes back to its constituent object. The
|
||||
* input array is assumed to have been created from the original object.
|
||||
*
|
||||
* @param bytes the byte array to convert.
|
||||
* @return the associated object.
|
||||
* @throws Exception
|
||||
* @param bytes the byte array to convert.
|
||||
* @return the associated object.
|
||||
*/
|
||||
public static Object toObject(byte[] bytes) throws Exception{
|
||||
Object object = null;
|
||||
|
|
@ -76,9 +115,8 @@ public class Converter {
|
|||
* Converts an array of bytes back to its constituent object. The
|
||||
* input array is assumed to have been created from the original object.
|
||||
*
|
||||
* @param bytes the byte array to convert.
|
||||
* @return the associated object.
|
||||
* @throws Exception
|
||||
* @param bytes the byte array to convert.
|
||||
* @return the associated object.
|
||||
*/
|
||||
public static Object toObject(DynamicByteArrayStream bytes) throws Exception{
|
||||
Object object = null;
|
||||
|
|
@ -93,9 +131,9 @@ public class Converter {
|
|||
/**
|
||||
* Checks if the given interface is implemented in the object
|
||||
*
|
||||
* @param object the object to look for the interface
|
||||
* @param interf the interface to look for
|
||||
* @return true if the interface is implemented else false
|
||||
* @param object the object to look for the interface
|
||||
* @param interf the interface to look for
|
||||
* @return true if the interface is implemented else false
|
||||
*/
|
||||
public static boolean isInstanceOf(Object object, Class<?> interf){
|
||||
Class<?>[] objectInterf = object.getClass().getInterfaces();
|
||||
|
|
@ -112,8 +150,8 @@ public class Converter {
|
|||
/**
|
||||
* Converts a byte Array to a Hex String
|
||||
*
|
||||
* @param raw the byte array to convert
|
||||
* @return a Hex String
|
||||
* @param raw the byte array to convert
|
||||
* @return a Hex String
|
||||
*/
|
||||
public static String toHexString(byte[][] raw){
|
||||
StringBuffer ret = new StringBuffer();
|
||||
|
|
@ -144,8 +182,8 @@ public class Converter {
|
|||
/**
|
||||
* Converts a byte Array to a Hex String
|
||||
*
|
||||
* @param raw the byte array to convert
|
||||
* @return a Hex String
|
||||
* @param raw the byte array to convert
|
||||
* @return a Hex String
|
||||
*/
|
||||
public static String toHexString(byte[] raw){
|
||||
StringBuffer ret = new StringBuffer();
|
||||
|
|
@ -161,8 +199,8 @@ public class Converter {
|
|||
/**
|
||||
* Converts a byte to a Hex String
|
||||
*
|
||||
* @param raw the byte to convert
|
||||
* @return a Hex String
|
||||
* @param raw the byte to convert
|
||||
* @return a Hex String
|
||||
*/
|
||||
public static String toHexString(byte raw){
|
||||
String ret = ""+HEX_CHARS[(int) (raw >>> 0x04)& 0x0F ];
|
||||
|
|
@ -174,8 +212,8 @@ public class Converter {
|
|||
/**
|
||||
* Converts the given byte to a String with 1's and 0's
|
||||
*
|
||||
* @param raw the byte to convert
|
||||
* @return a String with 1's and 0's
|
||||
* @param raw the byte to convert
|
||||
* @return a String with 1's and 0's
|
||||
*/
|
||||
public static String toString(byte raw){
|
||||
StringBuffer ret = new StringBuffer();
|
||||
|
|
@ -188,8 +226,8 @@ public class Converter {
|
|||
/**
|
||||
* Converts the given byte array to a String with 1's and 0's
|
||||
*
|
||||
* @param raw the byte array to convert
|
||||
* @return a String with 1's and 0's
|
||||
* @param raw the byte array to convert
|
||||
* @return a String with 1's and 0's
|
||||
*/
|
||||
public static String toString(byte[] raw){
|
||||
StringBuffer ret = new StringBuffer();
|
||||
|
|
@ -204,8 +242,8 @@ public class Converter {
|
|||
/**
|
||||
* Converts a BitSet to a Integer
|
||||
*
|
||||
* @param bits the BitSet to convert
|
||||
* @return a Integer
|
||||
* @param bits the BitSet to convert
|
||||
* @return a Integer
|
||||
*/
|
||||
public static int toInt(BitSet bits){
|
||||
int ret = 0;
|
||||
|
|
@ -220,8 +258,8 @@ public class Converter {
|
|||
/**
|
||||
* Converts a boolean array(bit sequence whit most significant bit at index 0) to a Integer
|
||||
*
|
||||
* @param bits the boolean array to convert
|
||||
* @return a Integer
|
||||
* @param bits the boolean array to convert
|
||||
* @return a Integer
|
||||
*/
|
||||
public static int toInt(boolean[] bits){
|
||||
int ret = 0;
|
||||
|
|
@ -232,12 +270,22 @@ public class Converter {
|
|||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a byte to a integer
|
||||
*
|
||||
* @param num is the byte to convert
|
||||
* @return an integer
|
||||
*/
|
||||
public static int toInt(byte b){
|
||||
return (int)(b & 0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a Integer to a BitSet
|
||||
*
|
||||
* @param i the Integer to convert
|
||||
* @return a BitSet object
|
||||
* @param i the Integer to convert
|
||||
* @return a BitSet object
|
||||
*/
|
||||
public static BitSet toBitSet(int num){
|
||||
BitSet ret = new BitSet();
|
||||
|
|
@ -253,10 +301,10 @@ public class Converter {
|
|||
/**
|
||||
* Converts a given String to a specified class
|
||||
*
|
||||
* @param <T> is the resulting class
|
||||
* @param data is the String data to be converted
|
||||
* @param c is the class to convert to
|
||||
* @return a instance of the class with the value in the string or null if there was an problem
|
||||
* @param <T> is the resulting class
|
||||
* @param data is the String data to be converted
|
||||
* @param c is the class to convert to
|
||||
* @return a instance of the class with the value in the string or null if there was an problem
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T fromString(String data, Class<T> c){
|
||||
|
|
@ -283,4 +331,40 @@ public class Converter {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces reserved and unsafe characters in URLs with hex values
|
||||
*/
|
||||
public static String urlEncode( String str ){
|
||||
StringBuilder out = new StringBuilder();
|
||||
for( char c : str.toCharArray() ){
|
||||
if( c>='0' && c<='9' || c>='A' && c<='Z' || c>='a' && c<='z' ||
|
||||
c=='$' || c=='-' || c=='_' || c=='.' || c=='+' || c=='!' ||
|
||||
c=='*' || c=='\'' || c=='(' || c==')' || c==',' )
|
||||
out.append( c );
|
||||
else{
|
||||
out.append( '%' ).append( toHexString((byte)c) );
|
||||
}
|
||||
}
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces hex values from a URL with the proper characters
|
||||
*/
|
||||
public static String urlDecode( String str ){
|
||||
StringBuilder out = new StringBuilder();
|
||||
char[] array = str.toCharArray();
|
||||
for( int i=0; i<array.length ;i++ ){
|
||||
char c = array[i];
|
||||
if( c == '%' && i+2<array.length ){
|
||||
out.append( (char)hexToByte( array[++i], array[++i]) );
|
||||
}
|
||||
else
|
||||
out.append( c );
|
||||
}
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue