Sameer said:
It is not possible to do 'Object Sending + NIO functionality'
simultaneously???
Given the context of this thread, I'll take that as "It is not possible
to send and receive Objects without blocking???"
One last time: no, it is not possible.
Fuller explanation:
* Sending an object down a byte stream has two steps, (1) creating a
byte sequence representing the Object and all the other Objects it
refers to directly or indirectly, and (2) sending the bytes.
ObjectOutputStream bundles all this up into its writeObject() method.
* Receiving an object from a byte stream has two steps, (1) receiving
all the bytes for the Object itself and for all the other objects it
refers to directly or indirectly, and (2) assembling the objects.
ObjectInputStream bundles all this up into its readObject() method.
* It is a fundamental characteristic of nonblocking I/O that the number
of bytes transferred in a single operation is unpredictable; to send
some fixed number of bytes you need to queue them and repeatedly,
whenever the output channel is ready to accept some, send as many as you
can without blocking. To receive bytes you do much the opposite,
reading as many bytes as are available whenever any are available, and
buffering them or queuing them for further handling by the application.
There is typically a delay between the time one non-blocking
operation is dispatched and the time another one can be successfully
performed (with more than zero bytes transferred). All of that
(including both writing and reading for multiple channels) can be
managed in a single thread if it is done correctly.
It is possible to handle an object write without blocking by capturing
the output to a buffer (possibly by using a ByteArrayOutputStream) and
then enqueuing the contents of the buffer and sending them in a
non-blocking manner as described above. This is broken with respect to
preserving referential integrity across the network link (which requires
use of a single, continuous stream of objects, whereas this scheme would
need to use a separate stream for each top-level object), but it could
conceivably serve your purpose.
It is *not* possible to perform an object read across a network (or from
disk) without the possibility of blocking. This is because you need to
receive *all* the bytes of the object before you can reconstitute it.
At a low level you could conceivably use non-blocking I/O to get the
bytes, but the overall "read object" operation cannot ever be guaranteed
to complete without blocking because you are never guaranteed to get all
the bytes in one read. Depending on the number of bytes required and
the size of the various buffers involved, it might not be possible to
*ever* get all the bytes in one read. Note also that with that being
the case, your server would need to be multithreaded in any case -- the
(nonblocking) network I/O operations would need to run in their own thread.