srinitin
asked on
Serialize/Deserialize using Streams
Hi,
I am using following streams to send objects on NIO sockets:
For sending data( Few code snippet):==>
while(true)
{
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
objOutput.clear();
objOutput.put(byteArray);
objOutput.flip();
numWrite = sc.write(objOutput);
// Some extra logic to stop sender
}
For receiving data ( Few code snippet):==>
while(true)
{
numRead = sc.read(objInput );
objInput.flip();
byteArray = objInput.array();
ByteArrayInputStream bStream1 = new ByteArrayInputStream(byteA rray, 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.
I am using following streams to send objects on NIO sockets:
For sending data( Few code snippet):==>
while(true)
{
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
objOutput.clear();
objOutput.put(byteArray);
objOutput.flip();
numWrite = sc.write(objOutput);
// Some extra logic to stop sender
}
For receiving data ( Few code snippet):==>
while(true)
{
numRead = sc.read(objInput );
objInput.flip();
byteArray = objInput.array();
ByteArrayInputStream bStream1 = new ByteArrayInputStream(byteA
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
How are you sending the messages using old IO? Can you post that code?
ASKER
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.getO utputStrea m());
objOutput.writeObject(msg) ;
objOutput.flush();
For receiving:
protected ObjectInputStream objInput;
objInput = new ObjectInputStream(sc.getIn putStream( ));
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(byteA rray,offse t,limit - offset);
ObjectInputStream iStream = new ObjectInputStream( bStream );
serializeData = (SerializedData) iStream.readObject();
System.out.println("Msg: " + serializeData.getData());
offset= limit-bStream.available();
bStream.reset();
}
++++++++++++++++++++++++++ ++++++++++ ++++++++++ ++++++++++ +++++
It is working fine.
The old IO using Socket in place of socketChannel:
Some code snippet==>
For sending:
protected ObjectOutputStream objOutput;
objOutput = new ObjectOutputStream(sc.getO
objOutput.writeObject(msg)
objOutput.flush();
For receiving:
protected ObjectInputStream objInput;
objInput = new ObjectInputStream(sc.getIn
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(byteA
ObjectInputStream iStream = new ObjectInputStream( bStream );
serializeData = (SerializedData) iStream.readObject();
System.out.println("Msg: " + serializeData.getData());
offset= limit-bStream.available();
bStream.reset();
}
++++++++++++++++++++++++++
It is working fine.
ASKER
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?
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?
ASKER
any inputs?
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
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
Try
final int BUF_SIZE = 1 << 16; // 64K
final int BUF_SIZE = 1 << 16; // 64K
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.