• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is Java NIO and when should I use it?

 
John Holme
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would appreciate a high-level overview of how the java.nio package differs from the java.io package, and when I will be likely to want to call on its capabilities. All I know about java.nio is what's in "Java in a Nutshell". thanks!
 
Enrique Javier
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,
You may start with Top Ten New Things You Can Do with NIO http://www.onjava.com/pub/a/onjava/2002/10/02/javanio.html, and then go to Ron's information site on his book Java NIO http://www.javanio.info/.
[ April 15, 2003: Message edited by: Enrique Javier ]
 
Ron Hitchens
Author
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, those sound like great ideas.
Let me also give a very short summary here. For full details, read my book.
NIO is an alternate approach to doing I/O on the Java platform. It began life as JSR 51. This Java Specification Request basically boils down to "Hey, we need to be able to use the full I/O capabilities of the OS. We're writing big, honkin' enterprise apps and we need that stuff".
So NIO was specified as a set of abstractions for accessing I/O services. Rather than basing everything on the simple, but very portable, metaphor of a stream NIO has the more general notion of a channel. Channels perform data transfer in chunks, or blocks, which are represented by a new family of objects known as buffers.
The new buffer and channel metaphors are closer to the way I/O works at the Operating System level. NIO also makes provision for I/O features which are widely available on most OSs (like file locking and memory mapped files) but which don't relate to streams at all.
NIO provides new channel-based abstractions for sockets. These socket channels may operate in non-blocking mode, which finally removes the onerous need for dedicating a thread to each socket when managing lots of them.
An entirely new object, a selector, is part of NIO. It provides readiness selection service for a group of non-blocking socket channels.
Selection is probably the most important new addition in NIO since it enables you to increase the number of active sockets without increasing the number of threads. It also delegates most of the grunt work to the OS which makes your apps more efficient and less complex.
NIO is a new way of doing I/O. It's not the way that I/O must be done now. The traditional java.io classes work fine for a broad ranges of uses. Both will be around for a while.
 
John Holme
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
both responses were very informative and helpful; thanks, guys!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!