Hello,

After completing my application, my teacher told me I can't use serialization. I currently send objects using the writeObject / readObject methods.

What's the easiest way of transitioning from this to something else? I'm thinking:

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

out = new PrintWriter(new BufferedWriter(
			new OutputStreamWriter( socket.getOutputStream())),true);

Then, I'd have a MyClass.ToString() method for getting a String representation of my object and then calling:

out.println(myObject.ToString());

Is this okay? or does this also inflate packets?

Thanks.

Recommended Answers

All 4 Replies

This depends on what kind of fields your class has. Assuming they are just ordinary data types - ints, Strings etc - then you, it's not hard to have a method that concatenates all the values into a single String with easy field delimiters (eg tabs), and a corresponding constructor that takes a single String as its parameter and uses that to populate all the fields.
You can then send that String anyway you want. I wouldn't use toString as the method name because that is used by many API classes when a user-friendly representation is needed.
Pity though, this is exactly what object serialisation os good for.

Thanks for the reply.

Yes, my class just has plain fields: byte, String, int.

I need some reassurance, since I can't get Packetyzer to work at home - will using the method posted above - writing to a PrintWriter(BufferedWriter(OutputStreamWriter(socket OutputStream)))) cause the packets' size to grow like crazy?

I don't see why you are making it so complicated. I would try a simple
new PrintWriter(socket.getOutputStream());
You don't need the OutputStreamWriter, and since you'll only be writing a single long String, there's nothing to be gained from a buffered stream. Your packets shouldn't have any unnecessary padding.

Ok, I'll try it when I get home.

Thanks for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.