Java 2 Platform Standard Edition (Java 2 Platform Standard edition,j2se) 1.4 has made significant changes to the I/O processing capabilities of the Java platform. Not only does it continue to support streaming I/O operations on previous J2SE distributions with streaming links, but Merlin also adds new features-called New I/O classes (NIO)-that are now in the Java.nio package.
I/O performs input and output operations to transfer data from the file or system console to or from the application. (For additional information on Java I/O, see Resources).
Buffer base
The abstract buffer class is the basis of the Java.nio packet support buffer. The Buffer works like a randomaccessfile in memory for reading and writing basic data types. Like Randomaccessfile, using Buffer, the next operation (read/write) takes place at a current location. Either of these operations will change that position, so reading after the write operation does not read what you just wrote, but reads the data after what you just wrote. Buffer provides four indication methods for accessing the linear structure (from highest to lowest):
Capacity (): Indicates the size of the buffer
Limit (): Tell you how many bytes have been filled in the buffer so far, or let you use: limit (int newlimit) to change this limit
Position (): tells you the current location to perform the next read/write operation
Mark (): Remember a location to reset it later with reset ()
The basic operation of the buffer is get () and put (), however, these methods are specific to each data type in the child class. To illustrate this, let's look at a simple example that demonstrates reading and writing a character from the same buffer. In Listing 1, the flip () method swaps the limits and positions, then places the position to 0 and discards the tag so you can read the data that you just wrote:
Listing 1. Read/write examples
import java.nio.*;
...
CharBuffer buff = ...;
buff.put('A');
buff.flip();
char c = buff.get();
System.out.println("An A: " + c);
Now let's look at some specific Buffer subclasses.
Buffer type
Merlin has 7 specific types of Buffer, each of which corresponds to a basic data type (excluding Boolean):
Bytebuffer
Charbuffer
DoubleBuffer
Floatbuffer
Intbuffer
Longbuffer
Shortbuffer