|Memory Mapped Files||Books|
|Direct Buffers||Learning More|
Even though I have written some code that at least functions using NIO (New Input/Output) I can’t say I understand it. I primarily just glue together methods based on the types of parameters and return types. I don’t have an overall picture of how it works or why it works, or what it is for, as I do for ordinary I/O. I am far from a reliable guide.
Introduced in Java 1.4, the java.nio NIO 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 asymmetric 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 Oracle’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 in-lined and it will run slowly.
NIO is for use when you have a lot of data to process and you don’t have to fish all of it out of the buffer. In other times it can be slower, such as this example where we read a file an once the fish all the bytes out of the buffer to convert them to a String, not a good use of NIO.
|recommend book⇒Java NIO|
|Covers NIO (new I/O) that arrived with JDK 1.4.|
|Greyed out stores probably do not have the item in stock. Try looking for it with a bookfinder.|
This page is posted
Optional Replicator mirror
Your face IP:[126.96.36.199]
You are visitor number|