Serialize/Deserialize using Streams

Posted on 2007-10-01
Medium Priority
Last Modified: 2008-09-13

I am using following streams to send objects on NIO sockets:

For sending data( Few code snippet):==>

ByteArrayOutputStream   bStream = new ByteArrayOutputStream();
ObjectOutputStream oStream = new ObjectOutputStream( bStream );

// serializedata is a object to be sent on socketchannel
oStream.writeObject (serializedata);

byteArray = bStream.toByteArray();

// objOutput is ByteBuffer


numWrite = sc.write(objOutput);

// Some extra logic to stop sender


For receiving data ( Few code snippet):==>

numRead = sc.read(objInput );
byteArray  = objInput.array();

ByteArrayInputStream bStream1 = new ByteArrayInputStream(byteArray, offset,limit - offset);

ObjectInputStream iStream = new ObjectInputStream( bStream1 );

serializeData = (SerializedData) iStream.readObject();


My program is working fine the only problem is that it is taking more time than old IO.

I want to use global Stream objects (rather than creating a new object for each message as used in code snippet above).

When i am trying to use global objects .. getting StreamCorruptedException.

Can anyone help me and let me know the reason for this exception and also the way to use global stream objects to speedup.?
Any other way to improve performance will be of great help.

Thanks in advance.
Question by:srinitin
LVL 86

Accepted Solution

CEHJ earned 500 total points
ID: 19991932
Streams aren't meant to be reused if that's what you're planning. Also, it would have negligible effect on  performance eeven if they were reusable

Author Comment

ID: 19992004
Thanks CEHJ for quick input.
Does it means that for each message i have to use new objects of streams as i am already using in my sample program or any other better way?

Then what are the probable problem in my program and why it is slower than my old IO program.
It is running perfectly fine ( as expected) and i am not getting any error.

Say for sending 50000 msgs:
using old IO time taken : 32 secs
and with NIO , time taken : 49 secs.

Any help will be greatly appreciated.

Author Comment

ID: 19992043
Does the used buffer size matters? as for sending data i have used BUFFER_SIZE=10000 and for reading data i have used BUFFER_SIZE=35000.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 23

Expert Comment

ID: 19992050
How are you sending the messages using old IO? Can you post that code?

Author Comment

ID: 19992203
Sure Ajay.  Sharing some logic here.

The old IO using Socket in place of socketChannel:

Some code snippet==>

For sending:

 protected ObjectOutputStream  objOutput;

  objOutput = new ObjectOutputStream(sc.getOutputStream());

For receiving:

 protected ObjectInputStream   objInput;
 objInput = new ObjectInputStream(sc.getInputStream());

msg = (MessageImpl) objInput.readObject();

One extra logic which i have used in my NIO program on receiver side is as follows:
To handle the scenario when multiple messages receives at once

// limit: number of bytes received in buffer
// offset : received message size
offset =0;
while( offset < limit)
bStream = new ByteArrayInputStream(byteArray,offset,limit - offset);
ObjectInputStream iStream = new ObjectInputStream( bStream );
serializeData = (SerializedData) iStream.readObject();
System.out.println("Msg: " + serializeData.getData());

offset= limit-bStream.available();

It is working fine.

Author Comment

ID: 19992271
I am working in synchronous mode and without using selectors. ( due to limitation of my existing system).  Only want to take advantage of bytebuffers and socketchannel.

Just want to know what should be expected behavior of NIO over old IO in such scenario i.e sending/receiving bytearray using socketchannel in synchronous mode.

1.) For small size of messages
2.) For large size messages.

Is there always increase in speed or it may degrade the performance also?
My intent is just to sppedup the things. is it good to use NIO?

Author Comment

ID: 19993764
any inputs?
LVL 86

Expert Comment

ID: 19993941
Certainly you can't expect NIO to deliver improved performance in all cases. Sometimes an enhancement will only appear in heavy operations.

To enhance your current code, try enlarging the send buffer on the Socket. Call allocateDirect to allocate the buffer and make sure the buffer size aligns to word boundaries
LVL 86

Expert Comment

ID: 19993987

final int BUF_SIZE = 1 << 16; // 64K
LVL 92

Assisted Solution

objects earned 500 total points
ID: 19994971
> My intent is just to sppedup the things. is it good to use NIO?

Don't see it gains you anything in your case, would stick with standard IO

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question