This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes I/O and Streams and the fly likes How to recieve Objects using NIO Channels? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to recieve Objects using NIO Channels?" Watch "How to recieve Objects using NIO Channels?" New topic
Author

How to recieve Objects using NIO Channels?

merwin pinto
Ranch Hand

Joined: Feb 16, 2005
Posts: 39
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

Joined: Oct 29, 2001
Posts: 8873
    
    8

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. . .


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
jQuery in Action, 2nd edition
 
subject: How to recieve Objects using NIO Channels?