nio : Java Glossary

nio
Introduced in Java 1.4, the java.nio NIO (New Input/Output) 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 (Random Access Memory) 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 (Java Virtual Machine), 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 (Garbage Collection) 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
by:Ron Hitchens 978-0-596-00288-6paperback
publisher:O’Reilly recommended
published:2002-08-15
Covers NIO (new I/O) that arrived with JDK 1.4.
Australian flag abe books anz abe books.co.uk UK flag
German flag abe books.de amazon.co.uk UK flag
German flag amazon.de abe books.ca Canadian flag
Spanish flag iberlibro.com amazon.ca Canadian flag
Spanish flag amazon.es Chapters Indigo Canadian flag
French flag abe books.fr abe books.com American flag
French flag amazon.fr amazon.com American flag
Italian flag abe books.it Barnes & Noble American flag
Italian flag amazon.it powells.com American flag
American flag download o’reilly safari sony e-books American flag
Greyed out stores probably do not have the item in stock

Learning More

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

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/nio.html J:\mindprod\jgloss\nio.html
logofeedback Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email If you want your message kept confidential, not considered for posting, please explicitly specify that.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.210]
You are visitor number 18,719.