• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to recieve Objects using NIO Channels?

 
merwin pinto
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How is it possible to send objects thru NIO channels? ..... I am having problems recieving objects using NIO channels.
In my program i am sending an object using the ObjectOutputStream .....
and at the server i am using the SocketChannel read(ByteBuffer bb) , method .... now i tried the Following
---------------------------------------------------
SocketChannel sc; /// this is the socket channel used to read input
ByteBuffer bb = ByteBuffer.allocate(5000);
int bytesRead = sc.read(bb);

if (bytesRead > 0) {

bb.flip();
Object o = null;
ByteArrayInputStream bais = new ByteArrayInputStream(bb.array());

ObjectInputStream ois = new ObjectInputStream(bais);
o = ois.readObject();
}
-------------------------------------------------------------
The above throws ..StreamCorruptedException .... this happens because the sc.read(bb);
method in the above code does not read the entire bytes of an object at one go ....hence the above error .....
is there any way i can block the channel so that the socketChannel can wait until the entire bytes of an object has been recieved.
can any one help me regarding this
Thanks a lot
[ June 23, 2005: Message edited by: merwin pinto ]
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see you've been working this problem for a couple of weeks. My advice is: if you don't need some NIO-specific functionality (i.e. nonblocking sockets), don't use NIO. NIO is nowhere near as well documented as the IO and net classes. The IO and net classes are actually NIO under the covers so there's no big performance difference for similar operations. The community doesn't have a lot of experience with NIO so if you get stuck, as you have found out, you are on your own.
That said, if you are sold on NIO, invest in a book. Sure, a book may run $30, but what's your TIME worth?
I don't have much experience with NIO, but it's common when using the regular IO classes to accumulate bytes in a buffer like so:

There may be a similar usage of an accumulator in NIO. . .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic