Java Reference
In-Depth Information
Summary
New Input/Ouput (NIO) provides faster I/O compared to the stream-based input/output. NIO uses buffers and
channels for I/O operations. A channel represents a connection between a data source/sink and a Java program for
data transfer. A buffer contains data to be written to a file or data that is read from a file. Buffers for holding different
types of primitive values are supported as instances of separate classes. You can use only a ByteBuffer for file I/O
operations. NIO also supports memory-mapped file I/O that is the fastest way to read/write files.
A buffer maintains several properties that are affected by reading its data or writing data to it. The position
property of a buffer is the index in the buffer that is the starting position to be read or written in the next read/write
operation. The limit property of a buffer is the index in the buffer that is the starting index indicating the invalid
read/write position. The buffer's position may change as you read from the buffer or write to the buffer.
Buffer related classes contain methods to manipulate those properties directly as well. A buffer supports absolute
as well as relative read/write. In absolute read/write, the buffer's position is unaffected. In a relative read/write, the
position property of the buffer is automatically advanced.
Byte buffers support different views. You can use a view of a buffer to access the data buffer's data as different
primitive type values or to see only part of the buffer's data.
A FileChannel along with buffers are used to read/write files. You can obtain a FileChannel from an
InputStream , an OutputStream , or using the factory method of the FileChannel class. You can also lock a file in
exclusive or shared mode using the lock() method of the FileChannel class.
The byte order is the order in which bytes of a multi-byte value are stored. A byte order is called big endian if
the bytes of a multi-bytes value are stored from the most significant byte to the least significant byte. If the bytes of a
multi-byte value are stored from the least significant byte to the most significant byte, it is known as little endian. You
need to deal with the byte order of a byte buffer if the buffer represents multi-byte data. The java.nio.ByteOrder
class represents the byte order. It contains two constants, BIG_ENDIAN and LITTLE_ENDIAN , to represent big-endian
and little-endian byte orders, respectively.
 
Search WWH ::




Custom Search