• Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialization with Nio

 
Dave Jones
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello again..
I would be gratefull to get a code sample example for serialization with nio, read and write ( just so I understand how these things work)
Thank you all so much!
Dave
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're kind of grasping at two different straws there. Serialization is what we use to persist objects (examples in the tutorial here). The "New IO" (To JDK 1.4, anyway) gave Java some capabilities that didn't exist in the API before, like file locking, direct memory access, memory mapped files and so on (Top Ten New Things You Can Do With New IO). There's nothing special about using serialization with NIO. Works the same way as normal IO (behold, the miracle of Object-Oriented Design!). As a matter of fact, the "old IO" classes in java.io were re-written to use NIO behind-the-scenes, so unless you specifically need the NIO functionality it may be easier to use the "old IO" classes, the use of which is well-described in the tutorial, above.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialization itself is still performed by ObjectOutputStream and ObjectInputStream - which are still traditional streams, not channels. If you want to connect these to NIO channels, you probably want to use things like the Channels class which provides adapters between streams and channels:

I agree with Joe that in many cases, there's not a big benefit to switching over to NIO. And for serialization, specifically, there's really no benefit. However it may be that you need NIO for other reasons - e.g. you're trying to build a high-volume scalable server for many concurrent users, and you want to benefit from Selectors to avoid having zillions of threads listening to all the different users. In thins case, you'll probably find yourself dealing with SocketChannels rather than SocketInputStreams and SocketOutputStreams. Which means that when it's time to do serialization, you need to connect to channels rather than streams. Which is what the adapters in the Channels class are for, as shown in the code above.
 
Dave Jones
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much , both of you !!
you were very helpfull !
Thanks and have a nice day
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!