Java 1.4 came out with NIO package that introduced a lot of new classes that java programmers were longing for a long. Few of the important features have been discussed below.
Block IO
Until now java has been doing I/O using streams, InputStream and OutputStream to be precise. The problem with stream I/O is that the I/O is carried out internally using byte by byte and hence very slow. Java uses these steams to send and receive data to external resources like files and sockets. It is also used to serialize and de-serialize java objects.
NIO is different from Stream I/O; NIO performs I/O operations in blocks. This simulates I/O closer to how operating systems optimize I/O. And hence it is fast.
Channels
The older I/O model for reading and writing used two different classes, InputStream and OutputStream, single directional classes. Often you end up creating a lot of objects for what you want to do. Various specialization of these classed were used for various type streams.
Once again java pundits though thoroughly how operating system does the I/O and came out with a better deign for I/O called a Channel. Channels are bidirectional data transfer mechanisms that allows read and write. Channels are also synchronized and provide a lot more thread safety. Like streams, there are various implementations of channels for various purposes.
Buffers
A Buffer is essentially a container object. All data that is sent to a channel must first be placed in
a buffer; likewise, any data that is read from a channel is read into a buffer. Buffers compliment channels for faster I/O, various types of buffers are,
· ByteBuffer
· CharBuffer
· ShortBuffer
· IntBuffer
· LongBuffer
· FloatBuffer
· DoubleBuffer
Buffers come with a powerful set of functions to manipulate the content of the buffer.
Memory mapped files
Memory mapped file is another great introduction to java. These are files directly mapped to the system memory. Since the content is in system memory, reads and writes as fast as accessing system memory. In real life the common usage of memory mapping is for sharing libraries, as called as shared libraries.
Asynchronous or Non-blocking I/O
Non-blocking I/O is a method for reading and writing data without blocking. Normally, when your code makes a read, the code blocks until there is data to be read. Likewise, a write will block until the data can be completely written. Also in older model, when a server opens a socket, server usually assigns a thread exclusively to serve that socket.
On the other hand, Asynchronous I/O calls non-blocking, non thread binding, it lets you do I/O from a great many inputs and outputs at the same time. As stated synchronous I/O, systems often create one thread per connection; this creates a lot of threads for serving the connections. With asynchronous I/O, you can listen for I/O events on multiple number of channels, without polling and without extra threads.
Character-set encoding and decoding
Charsets class provides encoding and decoding between charset formats like Unicode and UTF-8. This will class will make a lot of internet programmers happy
References
Java NIO package.
http://java.sun.com/j2se/1.4.2/docs/api/java/nio/package-summary.html
Book, Java NIO.
www.macdevcenter.com/catalog/javanio/
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment