|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.lapetus_ltd.api.common.TLptsBytesUtil
public class TLptsBytesUtil
Class Description : Factory class for dealing with conversions to and from bytes and objects.
Use this class to convert a class object (like a String or Int) to bytes and then back again.
Complex Class Objects (that are created in applications) can be converted for transport by implementing the
readExternal and writeExternal of the Externalizable interface. See object2Bytes(java.lang.Object)
for an example.
(or see TestGuiUtils for a complete example).
$LastChangedRevision: 1186 $
$LastChangedDate:: 2010-11-17 08:27:54#$
Method Summary | |
---|---|
static java.lang.Double |
bytes2Double(byte[] data)
Converts bytes to a primitive double (not object). |
static java.lang.Integer |
bytes2Int(byte[] data)
Converts bytes to a primitive integer (not object). |
static java.lang.Long |
bytes2Long(byte[] data)
Converts bytes to a primitive long (not object). |
static java.lang.Object |
bytes2Object(byte[] bytes)
Converts bytes to an object that implements Externalizable . |
static java.lang.String |
bytes2String(byte[] data)
Converts bytes to a String. |
static byte[] |
bytesFromHexString(java.lang.String hexStr)
Converts a hexString to bytes. |
static byte[] |
concatenateBytes(byte[] b1,
byte[] b2)
Creates a byte array from another 2 byte arrays, by concatenating in the order provided. |
static byte[] |
double2Bytes(double _double)
Converts an double primitive to bytes. |
static java.lang.String |
getHexString(byte[] b)
Converts bytes to a String. |
static byte |
initByteFromInteger(int i)
Converts integer to byte. |
static byte[] |
int2Bytes(int integer)
Converts an integer primitive to bytes. |
static byte[] |
long2Bytes(long _long)
Converts an long primitive to bytes. |
static byte[] |
object2Bytes(java.lang.Object obj)
Converts an object that implements Externalizable . |
static byte[] |
string2Bytes(java.lang.String str)
Converts a string to bytes. |
static byte[] |
xor(byte[] b1,
byte[] b2)
EXclusive OR for two byte arrays. |
static void |
zI()
Obfuscated, as it is not required by the application. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static java.lang.Double bytes2Double(byte[] data) throws java.io.IOException
double d = 1234567890.0123456789; byte[] doubleBytes = TLptsBytesUtil.double2Bytes(d); System.out.println(); System.out.print("Bytes of double are :"); for (byte b : doubleBytes) System.out.print(String.format("%02X",b)); System.out.println(); double d1 = 0; try { d1 = TLptsBytesUtil.bytes2Double(doubleBytes); System.out.println("Returned number is " + d1); } catch (IOException e) { TLptsLogger.logError("Exception during byte2Double conversion.",e); } Result : Bytes of double are :41D26580B480CA46 Returned number is 1234567890.0123456789
data
- the bytes that make up a double (exactly 8 bytes).
java.io.IOException
public static java.lang.Integer bytes2Int(byte[] data) throws java.io.IOException
Converts bytes to a primitive integer (not object).
Thread Safe : Yes Spawns its own Thread : No May Return NULL : n/a. throws IOException on error. Notes : Example :int x = 1234567890; byte[] intBytes = TLptsBytesUtil.int2Bytes(x); System.out.println(); System.out.print("Bytes of integer are :"); for (byte b : intBytes) System.out.print(String.format("%02X",b)); System.out.println(); int x1 = 0; try { x1 = TLptsBytesUtil.bytes2Int(intBytes); System.out.println("Returned number is " + x1); } catch (IOException e) { TLptsLogger.logError("Exception during byte2Int conversion.",e); } Result : Bytes of integer are :499602D2 Returned number is 1234567890
data
- the bytes that make up an integer (exactly 4 bytes).
java.io.IOException
public static java.lang.Long bytes2Long(byte[] data) throws java.io.IOException
long x = 3214569870L; byte[] longBytes = TLptsBytesUtil.long2Bytes(x); System.out.println(); System.out.print("Bytes of long are :"); for (byte b : longBytes) System.out.print(String.format("%02X",b)); System.out.println(); long x1 = 0; try { x1 = TLptsBytesUtil.bytes2Long(longBytes); System.out.println("Returned number is " + x1); } catch (IOException e) { TLptsLogger.logError("Exception during byte2Long conversion.",e); } Result : Bytes of long are :00000000BF9A718E Returned number is 3214569870
data
- the bytes that make up a long (exactly 8 bytes).
java.io.IOException
public static java.lang.Object bytes2Object(byte[] bytes)
Converts bytes to an object that implements Externalizable
.
// took the output from the previous example, so we should get the same object as used above (MyObject). String hexStr = "ACED0005737200084D794F626A656374F3B562405B12055B0C0000787077130000000A000D4A61766120697320636F6F6C2178"; // first we need to convert the string to bytes (be careful here: This is not String.getBytes()). // We convert the hex representative value like 'AA' as a string to 0xAA. byte[] inBytes = TLptsBytesUtil.bytesFromHexString(hexStr); Object obj = TLptsBytesUtil.bytes2Object(inBytes); if (obj instanceof MyObject) System.out.println("My Object int & string = " + ((MyObject)obj).myInt + " " + ((MyObject)obj).myString); Result: My Object int & string = 10 Java is cool!
bytes
- The bytes of an object, as written by 'writeExternal' of Externalizable
for the object.
public static java.lang.String bytes2String(byte[] data)
String str = "This is my test string"; byte[] stringBytes = TLptsBytesUtil.string2Bytes(str); System.out.println(); System.out.print("Bytes of string are :"); for (byte b : stringBytes) System.out.print(String.format("%02X",b)); System.out.println(); String str1; str1 = TLptsBytesUtil.bytes2String(stringBytes); System.out.println("Returned string is " + str1); Result : Bytes of string are :001654686973206973206D79207465737420737472696E67 Returned string is This is my test string
data
- the bytes that make up a String.
public static byte[] bytesFromHexString(java.lang.String hexStr)
String hexStr = "2ecead19db"; byte[] hexStringBytes = TLptsBytesUtil.bytesFromHexString(hexStr); System.out.println(); System.out.print("Bytes of hexString are :"); for (byte b : hexStringBytes) System.out.print(String.format("%02X",b)); System.out.println(); String hexStr1; hexStr1 = TLptsBytesUtil.getHexString(hexStringBytes); System.out.println("Returned string is " + hexStr1); Result : Bytes of string are :2ECEAD19DB Returned string is 2ecead19db
hexStr
- the String value which is to be converted to bytes.
public static byte[] concatenateBytes(byte[] b1, byte[] b2)
Creates a byte array from another 2 byte arrays, by concatenating in the order provided.
Thread Safe : Yes Spawns its own Thread : No May Return NULL : Never, even if the two byte arrays supplied are null. Notes : Licence limit of 32 bytes. Example :byte[] a = new byte[] {(byte) 0xAA, (byte) 0xEE, (byte) 0xAA}; byte[] b = new byte[] {(byte) 0xBB, (byte) 0xCC, (byte) 0xBB}; for (byte ab : TLptsBytesUtil.concatenateBytes(a,b)) System.out.print(String.format("%X ",ab)); RESULT : AA EE AA BB CC BB
b1
- The first byte arrayb2
- The second byte array to concatenate to the end of b1.
public static byte[] double2Bytes(double _double)
double d = 1234567890.0123456789; byte[] doubleBytes = TLptsBytesUtil.double2Bytes(d); System.out.println(); System.out.print("Bytes of double are :"); for (byte b : doubleBytes) System.out.print(String.format("%02X",b)); System.out.println(); double d1 = 0; try { d1 = TLptsBytesUtil.bytes2Double(doubleBytes); System.out.println("Returned number is " + d1); } catch (IOException e) { TLptsLogger.logError("Exception during byte2Double conversion.",e); } Result : Bytes of double are :41D26580B480CA46 Returned number is 1234567890.0123456789
_double
- the double value which is to be converted to bytes.
public static java.lang.String getHexString(byte[] b)
String hexStr = "2ecead19db"; byte[] hexStringBytes = TLptsBytesUtil.bytesFromHexString(hexStr); System.out.println(); System.out.print("Bytes of hexString are :"); for (byte b : hexStringBytes) System.out.print(String.format("%02X",b)); System.out.println(); String hexStr1; hexStr1 = TLptsBytesUtil.getHexString(hexStringBytes); System.out.println("Returned string is " + hexStr1); Result : Bytes of string are :2ECEAD19DB Returned string is 2ecead19db
b
- the bytes that make up a hexString.
public static byte initByteFromInteger(int i)
String strBytes = ""; for(int j = 0; j < 256; j++) strBytes += String.format("%02X",TLptsBytesUtil.initByteFromInteger(j)); System.out.println(strBytes); Result : Returned string is 000102030405060708090A0B0C0D0E0F101112 131415161718191A1B1C1D1E1F202122232425 262728292A2B2C2D2E2F303132333435363738 393A3B3C3D3E3F404142434445464748494A4B 4C4D4E4F505152535455565758595A5B5C5D5E 5F606162636465666768696A6B6C6D6E6F7071 72737475767778797A7B7C7D7E7F8081828384 85868788898A8B8C8D8E8F9091929394959697 98999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AA ABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBD BEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0 D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3 E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6 F7F8F9FAFBFCFDFEFF
i
- the integer to convert.
public static byte[] int2Bytes(int integer)
int x = 1234567890; byte[] intBytes = TLptsBytesUtil.int2Bytes(x); System.out.println(); System.out.print("Bytes of integer are :"); for (byte b : intBytes) System.out.print(String.format("%02X",b)); System.out.println(); int x1 = 0; try { x1 = TLptsBytesUtil.bytes2Int(intBytes); System.out.println("Returned number is " + x1); } catch (IOException e) { TLptsLogger.logError("Exception during byte2Int conversion.",e); } Result : Bytes of integer are :499602D2 Returned number is 1234567890
integer
- the integer value which is to be converted to bytes.
public static byte[] long2Bytes(long _long)
long x = 3214569870L; byte[] longBytes = TLptsBytesUtil.long2Bytes(x); System.out.println(); System.out.print("Bytes of long are :"); for (byte b : longBytes) System.out.print(String.format("%02X",b)); System.out.println(); long x1 = 0; try { x1 = TLptsBytesUtil.bytes2Long(longBytes); System.out.println("Returned number is " + x1); } catch (IOException e) { TLptsLogger.logError("Exception during byte2Long conversion.",e); } Result : Bytes of long are :00000000BF9A718E Returned number is 3214569870
_long
- the long value which is to be converted to bytes.
public static byte[] object2Bytes(java.lang.Object obj)
Converts an object that implements Externalizable
.
// MyObject.java class MyObject implements Externalizable, Serializable { public int myInt; public String myString; MyObject(int myInt, String myString) { this.myInt = myInt; this.myString = myString; } // the order of the reading and writing is vital. They must match. public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(this.myInt); out.writeUTF(this.myString); } // the order of the reading and writing is vital. They must match. public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.myInt = in.readInt(); this.myString = in.readUTF(); } } // MyTestArea.java public class MyTestArea { public void testObject() { MyObject myObject = new MyObject(10,"Java is cool!"); for (byte bt : TLptsBytesUtil.object2Bytes(myObject)) System.out.print(String.format("%02X",bt)); } } Result : ACED0005737200084D794F626A656374F3B562405B12055B0C0000787077130000000A000D4A61766120697320636F6F6C2178
obj
- The object to convert. Unsupported types need overwrites.
public static byte[] string2Bytes(java.lang.String str)
String str = "This is my test string"; byte[] stringBytes = TLptsBytesUtil.string2Bytes(str); System.out.println(); System.out.print("Bytes of string are :"); for (byte b : stringBytes) System.out.print(String.format("%02X",b)); System.out.println(); String str1; str1 = TLptsBytesUtil.bytes2String(stringBytes); System.out.println("Returned string is " + str1); Result : Bytes of string are :001654686973206973206D79207465737420737472696E67 Returned string is This is my test string
str
- the String value which is to be converted to bytes.
public static byte[] xor(byte[] b1, byte[] b2)
byte[] b1 = new byte[256]; byte[] b2 = new byte[256]; Random rand = new Random(); for(int j = 0; j<256; j++) b1[j] = TLptsBytesUtil.initByteFromInteger(rand.nextInt()); for(int j = 0; j<256; j++) b2[j] = TLptsBytesUtil.initByteFromInteger(rand.nextInt()); System.out.println(); System.out.print("Bytes of b1 are :"); for (byte b : b1) System.out.print(String.format("%02X",b)); System.out.println(); System.out.print("Bytes of b2 are :"); for (byte b : b2) System.out.print(String.format("%02X",b)); byte[] xor = TLptsBytesUtil.xor(b1,b2); xor = TLptsBytesUtil.xor(b1,xor); Result : b2 bytes and xor bytes are the same
b1
- the bytes of first Array for xor.b2
- the bytes of second Array for xor.
public static void zI()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |