Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About ObjectOutputStream

 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple question :

Given two TCP sockets, can one invocation of writeObject(Object o) on one end result in multiple invocations of readObject() in the other?

(I have read this doesn't happen when you're testing locally)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. One writeObject() corresponds to one readObject().

 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok this leads me to another question, why do network programmers bother writing protocols to map objects to their byte representation? Can't all of it be skipped by making network stuff implement Serializable then relying writeObject() and readObject()? Also, can I use these two methods with NIO? All the NIO examples I've read use ByteBuffers.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marwen Bakkar wrote:Ok this leads me to another question, why do network programmers bother writing protocols to map objects to their byte representation? Can't all of it be skipped by making network stuff implement Serializable then relying writeObject() and readObject()?


Because Java's built-in serialization, while a reasonable general case mechanism for Java-to-Java communication, is not appropriate in all use cases. Sometimes we need it to be human readable, or portable to other platforms, so we may use XML (such as provided by XStream or javabeans) or some other representation. Sometimes we may have specific performance needs in terms of space or size for which the built-in mechanism is not up to the task, so we might write our own protocol that's geared toward our needs.

Also, can I use these two methods with NIO? All the NIO examples I've read use ByteBuffers.


I don't know. I've never really used NIO, so I don't know what it provides. I expect there are streams though, and if there are, you can wrap then in OOS/OIS.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NIO works with channels, not with streams. However, you can turn several channels into streams by using the static methods of java.nio.channel.Channels. For instance, to get an InputStream for a SocketChannel all you need to do is call Channels.new InputStream(socketChannel). In a similar way you can get an OutputStream.
 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SocketChannel has socket() method that returns its socket. I wonder if it's possible to test for readiness selection then instead of reading bytes I get an OIS and use readObject().

What I want to do is instead of constructing packets of the sort :

content length, content type, data

I wrap the data bytes inside a serialization proxy with a single byte array field and instead of writing the raw bytes to the stream I'd write the proxy with writeObject(). Wouldn't this avoid the need of for the headers and buffering on the other side until we get a meaningful sequence of bytes? (Since writeObject() always results in a single readObject())
 
Paul Clapham
Sheriff
Posts: 21126
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's certainly one way of producing a simple communications protocol. As long as you only want to send byte arrays, and you don't mind materializing the whole byte array in memory at both client and server, that is.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic