Size of object?

Hey guys I was wondering if its possble to figure out how big an object is.

For instance, here's my message object class:

public class Message {

String Identifer;
String Serial;
String Node;
String NodeAlias;
String Manager;
String Agent;
String AlertGroup;
String AlertKey;
String Severity;
String Summary;
String StateChange;
String FirstOccurance;
String LastOccurance;
String InternalLast;
String EventId;
String LocalNodeAlias;

//these are datamembers that will be used during
//the sending of the message to the reciever
//packetID should be incremented each time a new message is created
public static Integer PacketID;
Integer sizeOfPacket; //I'll store the message's size in this data member.
String packetType;




Message()
{
Identifer = "";
Serial = "";
Node = "";
NodeAlias = "";
Manager = "";
Agent = "";
AlertGroup = "";
AlertKey = "";
Severity = "";
Summary = "";
StateChange = "";
FirstOccurance = "";
LastOccurance = "";
InternalLast = "";
EventId = "";
LocalNodeAlias = "";
}

void print()
{
System.out.println("\nIdentifer: " + this.Identifer + '\n'
+ "Serial: " + this.Serial + '\n'
+ "Node: " + this.Node + '\n'
+ "NodeAlias: " + this.NodeAlias + '\n'
+ "Manager: " + this.Manager + '\n'
+ "Agent: " + this.Agent + '\n'
+ "AlertGroup: " + this.AlertGroup + '\n'
+ "AlertKey: " + this.AlertKey + '\n'
+ "Severity: " + this.Severity + '\n'
+ "Summary: " + this.Summary + '\n'
+ "StateChange: " + this.StateChange + '\n'
+ "FirstOccurance: " + this.FirstOccurance + '\n'
+ "LastOccurance: " + this.LastOccurance + '\n'
+ "InternalLast: " + this.InternalLast + '\n'
+ "EventId: " + this.EventId + '\n'
+ "LocalNodeAlias: " + this.LocalNodeAlias + '\n');
}


}

Once I get a message from an event server, I create an object and populate the message's data members.

Now I want to know how big that message is, meaning all the messages datamembers once I fill them up.

The "real" size isn't what is important, the important thing is when I send this message object to the reciever the reciever is going to compare how many bytes it got, and look at the message's data member, Integer sizeOfPacket;

If the sizeOfPacket matches how much the reciever got then all is well. If not then packet loss must of occured so resend the message.

any help would be great! thanks!
[2977 byte] By [voidflux] at [2007-11-20 9:01:45]
# 1 Re: Size of object?
Well, you need to decide how you want to 'serialize' that object into a message, then you should be able to 'calculate' what size the message is. If oyu pack everything into one long (comma seperated or whatever) string, then you can just take the length of that string - and voila - you got the size of your message.

- petter
wildfrog at 2007-11-10 2:15:14 >
# 2 Re: Size of object?
Thanks for the help Petter,

Well Im actually sent a massive string initially, and I parse out the unneeded information and put the information into a message object.

I never did anything like this before and I'm alittle confused on how people go about sending data via sockets.

I do have a message object, but you can't send the "object" itself right? Even if you could the receving program wouldn't have any idea how to deal with a message object.

So I just write a function in the message object that just concatenates all the objects data members together seperated by a comma, and sends that string to the other program right?

Here's an example of a string I get:

UPDATE: "GATEWAY:@barneyfifedisconnectedMon Jun 11 16:46:12 2007",446,"barneyfife","","ConnectionWatch","","Gateway","GATEWAY:",0,"A GATEWAY process running on barneyfife has disconnected as username gateway",06/11/07 16:50:12,06/11/07 16:46:12,06/11/07

I turn it into this and put it into a map based on its serial.

Identifer: UPDATE: "@dyn9027132063.raleigh.ibm.com->NmosPingFail1"
Serial: 149
Node: "9.27.132.63"
NodeAlias: ""
Manager: "Omnibus"
Agent: "Precision Monitor Probe"
AlertGroup: "Precision Monitor"
AlertKey: "@dyn9027132063.raleigh.ibm.com->NmosPingFail"
Severity: 3
Summary: "Ping fail for 9.27.132.63: ICMP Host Unreachable: host unreachable from gateway 9.27.132.107"
StateChange: 07/02/07 21:50:19
FirstOccurance: 06/29/07 15:42:15
LastOccurance: 07/02/07 21:50:19
InternalLast: 07/02/07 21:50:19
EventId: "NmosPingFail"
LocalNodeAlias: "9.27.132.63"

The string doesn't match up with the string sent in because I just copied and pasted random incoming string and my output, but you get the idea
voidflux at 2007-11-10 2:16:08 >
# 3 Re: Size of object?
I do have a message object, but you can't send the "object" itself right? Even if you could the receving program wouldn't have any idea how to deal with a message object.
Not necessarily true. If the object implements Serializable than it can be serialised, sent and deserialised at the other end provided the receiver understands the object type. Having said that serialization isn't the answer to all prays and it may well be easier and quicker and more reliable to construct and parse a string as you are doing now. There are many articles on serialiazation that are worth reading such as this one (http://www273.pair.com/med/columns/Durable4.html).
keang at 2007-11-10 2:17:12 >
# 4 Re: Size of object?
Thanks guys!
voidflux at 2007-11-10 2:18:15 >