nio : Java Glossary
home N words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2008-02-22 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
nio
Introduced in Java 1.4, the java.nio (new i/ o) package allows I/O more like that available in other lower level languages like C. You can memory map files. You can read and write blocks of data direct from disk, rather than byte by byte. It deals with the endian problem when you fish the data out of the buffer, not during the read. You can do non-blocking asynchronous I/O. You can lock files or parts of files. Regular I/O is based on stacking the methods you need in a rather verbose and highly assymetric way, in InputStream, Reader, BufferedReader. The nio design is cleaner, simpler and more efficient. You could do all your I/O with nio if you chose. Unfortunately, at this point the File I/O Amanuensis will not generate nio examples, however there some examples in the ByteBuffer entry.

Memory mapped files chew up huge amounts of virtual RAM and real RAM as well for the lookup tables and cached data, so they are not for routine use. They will mainly help random access especially when you have several threads accessing the same file. Don’t just assume they will improve performance. Further, they are limited by the address space. In a 32-bit JVM, you will be lucky if you can even handle a 1GB file window. Test both ways.

Direct buffers help primarily for bulk file operations, slewing through files doing little processing of the data or bulk copying. They are only for when you can allocate a mighty buffer at the start of the program and keep it till completion. Normal GC does not recover the unused direct buffers. Again, test both ways. Also be aware performance ratios will differ on different platforms.

Read and writing individual bytes goes through a lot of indirection, even though what you are trying to achieve is very simple. For Sun’s HotSpot you need to make sure that the method with your inner loop is very small. If it isn’t then the access code won’t be inlined and it will run very slowly.

Books

book_cover recommend book⇒Java NIO
ISBN10:0-596-00288-2
ISBN13:978-0-596-00288-6
publisher:O’Reilly recommended
published:2002-08-15
by:Ron Hitchens
Covers NIO (new I/O) that arrived with JDK 1.4.
Canadian flag amazon.ca. amazon.com. American flag
Canadian flag chapters.indigo.ca . powells.com American flag
French flag amazon.fr. barnesandnoble.com American flag
German flag amazon.de. download O’Reilly Safari American flag
UK flag amazon.co.uk.   

Learning More

Sun’s Javadoc on the nio overview class : available:
Sun’s JDK Technote Guide on nio examples : available:
Sun’s Javadoc on the CharSet class : available:
Sun’s Javadoc on the CharSetDecoder class : available:
Sun’s Javadoc on the ByteBuffer class : available:
Sun’s Javadoc on the MappedByteBuffer class : available:
Sun’s Javadoc on the CharBuffer class : available:
Sun’s Javadoc on the FileChannel class : available:
Sun’s Javadoc on the SocketChannel class : available:
Sun’s Javadoc on the Pipe class : available:

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.18] The information on this page is for non-military use only.
You are visitor number 10,720. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/nio.html J:\mindprod\jgloss\nio.html