converting an object[] array as a byte array

hello,

i have an object defined by this constructor

public HelloRouteInfo(InetAddress destIP,long t)

{

destIP = destIPAddr;

t= Tnd;

}

and i also have an array of HelloRouteInfo[] type which contains 10 elements of HelloRouteInfo objects .

HelloRouteInfo[] arrayDestInfo =new HelloRouteInfo[10];

i need to represent this array as an array of bytes.

any suggestions?

thanks for your time

[612 byte] By [ang_marneridesa] at [2007-11-27 11:09:13]
# 1

Please tell us, what is your application-level requirement? For example, do you need an object serialization for networking purpose?

hiwaa at 2007-7-29 13:34:03 > top of Java-index,Java Essentials,Java Programming...
# 2

im developing a security-aware routing protocol for mobile ad-hoc networks which is going to be plugged as an overlay on a component-based middleware platform (project GridKit)

this array that i need to convert in to bytes is one of the arguments in the contructor defining my route reply message

public REBA(ConfigInfo cfg, IAntHocState cur, boolean mf, InetAddress sendto, short ttl,Timestamp sT,Timestamp eT,

Vector<nodesStackEntry> visited,boolean rf, boolean af, byte ps,byte hc,InetAddress da, int dsn, InetAddress oa,int lft,long t,

HelloRouteInfo[] hRi)

throws Exception{

super(cfg, cur, mf, sendto, ttl,sT,eT,visited);

repairFlag = rf;

ackFlag = af;

prefixSize = ps;

hopCount = hc;

destIPAddr = da;

destSeqNum = dsn;

origIPAddr = oa;

hArray = hRi; //pending ,destIPAddr,long

t=tmac;

lft = lifeTime;

if(cfgInfo.ipVersionVal == ConfigInfo.IPv4_VERSION_VAL)

buildDgramUsingIPv4REBA();

else if(cfgInfo.ipVersionVal == ConfigInfo.IPv6_VERSION_VAL)

buildDgramUsingIPv6REBA();

else

throw new Exception("Invalid IP version");

}

the builDgramUsingIPv4REBA method:

/**

* Method to build the DatagramPacket using the information

* in the IPv4 REBA

* @excepttion - return in case of errors, when building REBA

* anthocnet implementation by marnerides

*/

private void buildDgramUsingIPv4REBA() throws Exception {

byte msg[] = new byte[43];

msg[0] = Ant_REBA_MSG_CODE;

// build flags

msg[1] = 0x00;

if(repairFlag)

msg[1] = (byte) (msg[1] | 0x80);

if(ackFlag)

msg[1] = (byte) (msg[1] | 0x40);

// Prefix Size

msg[2] = prefixSize;

// hop count

msg[3] = hopCount;

// place destination IP address

byte addr1[] = destIPAddr.getAddress();

for(int i = 0; i < 4; i++)

msg[4 + i] = addr1[i];

// place destination seq num

//for(int i = 0; i < 4; i++)

//msg[8 + i] = (byte) ((destSeqNum >>> (8 * (3 - i))) & (byte) 0xFF);

msg[8] = msg[9] = msg[10] = msg[11] = (byte) 0;

msg[8] |= (byte) ((destSeqNum & 0xFF000000) >>> 24);

msg[9] |= (byte) ((destSeqNum & 0x00FF0000) >>> 16);

msg[10] |= (byte) ((destSeqNum & 0x0000FF00) >>> 8);

msg[11] |= (byte) (destSeqNum & 0x000000FF);

// place originator IP address

byte addr2[] = origIPAddr.getAddress();

for(int i = 0; i < 4; i++)

msg[12 + i] = addr2[i];

// place lifetime

//for(int i = 0; i < 4; i++)

//msg[16 + i] = (byte) ((lifeTime >>> (8 * (3 - i))) & 0xFF);

msg[16] = msg[17] = msg[18] = msg[19] = (byte) 0;

msg[16] |= (byte) ((lifeTime & 0xFF000000) >>> 24);

msg[17] |= (byte) ((lifeTime & 0x00FF0000) >>> 16);

msg[18] |= (byte) ((lifeTime & 0x0000FF00) >>> 8);

msg[19] |= (byte) (lifeTime & 0x000000FF);

//tmac-->long 8 bytes

int tmp = (int) (tmac>>>32);

msg[20] = (byte)(tmp>>>24);

msg[21] = (byte) ((tmp>>>16) & 0x0ff);

msg[22] = (byte) ((tmp>>>8)& 0x0ff);

msg[23] = (byte) tmp;

tmp = (int) tmac;

msg[24] = (byte) (tmp>>>24);

msg[25] = (byte) ((tmp >>> 16) & 0x0ff);

msg[26] = (byte) ((tmp >>> 8) & 0x0ff);

msg[27] = (byte) tmp;

//needs Timestamps

//startStamp --> long

int temp = (int) (sT>>>32);

msg[28] = (byte)(temp>>>24);

msg[29] = (byte) ((temp>>>16) & 0x0ff);

msg[30] = (byte) ((temp>>>8)& 0x0ff);

msg[31] = (byte) temp;

temp = (int) sT;

msg[32] = (byte) (temp>>>24);

msg[33] = (byte) ((temp >>> 16) & 0x0ff);

msg[34] = (byte) ((temp >>> 8) & 0x0ff);

msg[35] = (byte) temp;

//endStamp --> long

int tempr = (int) (eT>>>32);

msg[36] = (byte)(tempr>>>24);

msg[37] = (byte) ((tempr>>>16) & 0x0ff);

msg[38] = (byte) ((tempr>>>8)& 0x0ff);

msg[39] = (byte) tempr;

tempr = (int) eT;

msg[40] = (byte) (tempr>>>24);

msg[41] = (byte) ((tempr >>> 16) & 0x0ff);

msg[42] = (byte) ((tempr >>> 8) & 0x0ff);

msg[43] = (byte) tempr;

//here i need to pass the HelloRouteInfo[] and convert it to bytes

javaUDPDgram = new DatagramPacket(msg, msg.length, toIP, AntHocNet_PORT);

}

as you can see i need this array as well as the rest of the arguments to be on a single byte buffer to be used by my javaUDPDgram

the first two parameters in the REBA constructor are referencing two component interfaces

by the way thanks for such a quick reply!

ang_marneridesa at 2007-7-29 13:34:03 > top of Java-index,Java Essentials,Java Programming...
# 3

If the raw byte data is your absolute requirement, don't use object, InetAddress, in the first place because object->byte conversion and restoration are not officially/formally supported in Java.

hiwaa at 2007-7-29 13:34:03 > top of Java-index,Java Essentials,Java Programming...
# 4

I disagree. Do have objects but some that can provide a byte[] representation of themselves.

CeciNEstPasUnProgrammeura at 2007-7-29 13:34:03 > top of Java-index,Java Essentials,Java Programming...
# 5

I don't reazlly understand what you are talking about hiwa...

What about serialization and getBytes() methods ?

Anyway what about something based on this:

ByteArrayOutputStream o = new ByteArrayOutputStream(4096);

ObjectOutputStream oo = new ObjectOutputStream(o);

Object anySerializableObject = new Object();

oo.writeObject(anySerializableObject);

ibanna at 2007-7-29 13:34:03 > top of Java-index,Java Essentials,Java Programming...
# 6

hi all and thanks for your suggestions

using a Serializable objects didn't really helped me so i took each instance of the object independently..

so

/**

* Method to build the DatagramPacket using the information

* in the IPv4 REBA

* @excepttion - return in case of errors, when building REBA

* anthocnet implementation by marnerides

*/

private void buildDgramUsingIPv4REBA() throws Exception {

byte msg[] = new byte[163];

msg[0] = Ant_REBA_MSG_CODE;

// build flags

msg[1] = 0x00;

if(repairFlag)

msg[1] = (byte) (msg[1] | 0x80);

if(ackFlag)

msg[1] = (byte) (msg[1] | 0x40);

// Prefix Size

msg[2] = prefixSize;

// hop count

msg[3] = hopCount;

// place destination IP address

byte addr1[] = destIPAddr.getAddress();

for(int i = 0; i < 4; i++)

msg[4 + i] = addr1[i];

// place destination seq num

//for(int i = 0; i < 4; i++)

//msg[8 + i] = (byte) ((destSeqNum >>> (8 * (3 - i))) & (byte) 0xFF);

msg[8] = msg[9] = msg[10] = msg[11] = (byte) 0;

msg[8] |= (byte) ((destSeqNum & 0xFF000000) >>> 24);

msg[9] |= (byte) ((destSeqNum & 0x00FF0000) >>> 16);

msg[10] |= (byte) ((destSeqNum & 0x0000FF00) >>> 8);

msg[11] |= (byte) (destSeqNum & 0x000000FF);

// place originator IP address

byte addr2[] = origIPAddr.getAddress();

for(int i = 0; i < 4; i++)

msg[12 + i] = addr2[i];

// place lifetime

//for(int i = 0; i < 4; i++)

//msg[16 + i] = (byte) ((lifeTime >>> (8 * (3 - i))) & 0xFF);

msg[16] = msg[17] = msg[18] = msg[19] = (byte) 0;

msg[16] |= (byte) ((lifeTime & 0xFF000000) >>> 24);

msg[17] |= (byte) ((lifeTime & 0x00FF0000) >>> 16);

msg[18] |= (byte) ((lifeTime & 0x0000FF00) >>> 8);

msg[19] |= (byte) (lifeTime & 0x000000FF);

//tmac-->long 8 bytes

int tmp = (int) (tmac>>>32);

msg[20] = (byte)(tmp>>>24);

msg[21] = (byte) ((tmp>>>16) & 0x0ff);

msg[22] = (byte) ((tmp>>>8)& 0x0ff);

msg[23] = (byte) tmp;

tmp = (int) tmac;

msg[24] = (byte) (tmp>>>24);

msg[25] = (byte) ((tmp >>> 16) & 0x0ff);

msg[26] = (byte) ((tmp >>> 8) & 0x0ff);

msg[27] = (byte) tmp;

//needs Timestamps

//startStamp --> long

int temp = (int) (sT>>>32);

msg[28] = (byte)(temp>>>24);

msg[29] = (byte) ((temp>>>16) & 0x0ff);

msg[30] = (byte) ((temp>>>8)& 0x0ff);

msg[31] = (byte) temp;

temp = (int) sT;

msg[32] = (byte) (temp>>>24);

msg[33] = (byte) ((temp >>> 16) & 0x0ff);

msg[34] = (byte) ((temp >>> 8) & 0x0ff);

msg[35] = (byte) temp;

//endStamp --> long

int tempr = (int) (eT>>>32);

msg[36] = (byte)(tempr>>>24);

msg[37] = (byte) ((tempr>>>16) & 0x0ff);

msg[38] = (byte) ((tempr>>>8)& 0x0ff);

msg[39] = (byte) tempr;

tempr = (int) eT;

msg[40] = (byte) (tempr>>>24);

msg[41] = (byte) ((tempr >>> 16) & 0x0ff);

msg[42] = (byte) ((tempr >>> 8) & 0x0ff);

msg[43] = (byte) tempr;

//HelloRoute list , destIPAddr + long (Tnd)

HelloRouteInfo[] arrayDestInfo = pStateComp.getHArray();

//assign the the helloroute info in bytes to be sent with the msg

for (int i=0; i<arrayDestInfo.length; i++)

{

byte addr3[]=arrayDestInfo[i].getDestIPAddr().getAddress(); //take the destIPAddr

//assign the values of the destIPAddr in bytes and then to msg[]

for(int j=0; j><4; j++)

{

msg[43+j] = addr3[j];

}

//then assing the long Tnd next to the destIPAddr

byte[] tnd = new byte [(int)arrayDestInfo[i].getTnd()]; //take the tnd

for(int t=0; t<8; t++)

{

msg[47 + t] = tnd[t];

}

}

javaUDPDgram = new DatagramPacket(msg, msg.length, toIP, AntHocNet_PORT);

}

so, i have this loop where i first take the element of the array with its destIPAddr and convert it as a byte array....afterwards, i collect the same element of the array and get its long value and i add it right next to the byte values of the DestIPAddrr...

im about to test if it actually works on my testbed on monday...i'll let you know! ;)

thanks all again

regards

ang_marneridesa at 2007-7-29 13:34:03 > top of Java-index,Java Essentials,Java Programming...