NiO and IO in the Java file system

Source: Internet
Author: User

Java has introduced the Java NIO mechanism since jdk1.4:

The notable feature of NiO is the channel, buffer, selector (selector), which adds non-blocking calls that are not in the traditional I/O mechanism (this is useful for network communication and can be used efficiently by the CPU). However, this can only be used for network channels (Socketchannel), filechannel or blocking calls.

We are now specifically analyzing the file I/O mechanism in Java, regardless of the network socket communication mechanism.

The traditional file system I/O mechanism in Java is the inner class in Java, FileSystem and File,java, which do not provide external display characteristics, the file class contains the FileSystem object, thus for the operation of file, For example, rename, create etc all turn into Java in the internal class filesystem operation. The following is an abstract class of filesystem in Java:

Abstract class FileSystem {

/**
* Return the FileSystem object representing this platform ' s local
* FileSystem.
*/
public static native FileSystem Getfilesystem ();


/*--normalization and construction--* *

/**
* Return The local filesystem ' s name-separator character.
*/
public abstract char getseparator ();

/**
* Return The local filesystem ' s path-separator character.
*/
public abstract char getpathseparator ();

。。。。。。

}

Each operating system has a specific file system, while under Windows, through the FileSystem Getfilesystem () operation to obtain the local file system, Win32filesystem, and for the Linux system, The acquisition is Unixfilesystem;

In Java, the traditional I/O mechanism of the file object, through the inclusion of filesystem object, to the file system under the management of files, create, delete, rename and so on. The I/O data streams, inputs and outputs for the file are FileInputStream and FileOutputStream. The following is an example of FileInputStream, in which the file operation functions in FileInputStream include the following:

Private native void open (String name) throws FileNotFoundException;

/**
* Reads a byte of data from the this input stream. This method blocks
* If no input is yet available.
*
* @return The next byte of data, or <code>-1</code> if the end of the
* file is reached.
* @exception IOException If an I/O error occurs.
*/
public int read () throws IOException {
Object TraceContext = iotrace.filereadbegin (path);
int b = 0;
try {
b = read0 ();
} finally {
Iotrace.filereadend (TraceContext, b = =-1? 0:1);
}
return b;
}

Private native int read0 () throws IOException;

/**
* Reads a subarray as a sequence of bytes.
* @param b The data to be written
* @param off the start offset in the data
* @param len the number of bytes that is written
* @exception IOException If an I/O error has occurred.
*/
Private native int Readbytes (byte b[], int off, int len) throws IOException;

Public native long Skip (long N) throws IOException;

And so on, above is a few important functions, each time the file read operation has a document read location, under the Linux file system is the file descriptor FileDescriptor, and the Windows system is handler, The read location is done by FileDescriptor or handler, and the file operation can only be read from the last position at a time.

However, FileChannel is introduced in the NIO (new I/O) in Java and has the following new features in FileChannel:

    1. Byte reads can be read in relative positions or in absolute locations
    2. The area of a file can be mapped directly into memory
    3. Bytes can be transferred from one file to another, through the Transferto method, the file transfer directly in the kernel space, without switching between the user state and the kernel state, effectively reducing the file transfer time (under Linux there is a corresponding function is Sendfile, Direct file transfer in the kernel without the need for data switching between the user state and the kernel state

The corresponding FileChannel is an abstract class:

Public abstract class FileChannel
Extends Abstractinterruptiblechannel
Implements Seekablebytechannel, Gatheringbytechannel, Scatteringbytechannel {... }

The newly added methods are:
/**
* Reads a sequence of bytes from this channel into the given buffer,
* Starting at the given file position.
*
* <p> This method works in the same manner as the {@link
* #read (Bytebuffer)} method, except that bytes is read starting at the
* Given file position rather than at the channel's current position. This
* Method does not modify this channel ' s position. If the given position
* is greater than the file's current size and no bytes is read. </p>
public Abstract int read (Bytebuffer DST, long position) throws IOException;
/**
* Writes a sequence of bytes to this channel from the given buffer,
* Starting at the given file position.
*
* <p> This method works in the same manner as the {@link
* #write (Bytebuffer)} method, except that bytes is written starting at
* The given file position rather than at the channel's current position.
* This method does the modify this channel ' s position. If the given
* position is greater than the file's current size then the file would be
* Grown to accommodate the new bytes; The values of any bytes between the
* Previous End-of-file and the Newly-written bytes are unspecified. </p>
public Abstract int write (Bytebuffer src, long position) throws IOException;
/**
* Acquires a lock on the given region of this channel ' s file.
Public Abstract Filelock Lock (long position, long size, Boolean shared)
throws IOException;
/**
* Forces any updates to this channel's file to being written to the storage
* device that contains it.
Public abstract void Force (Boolean metaData) throws IOException;
/**
* Transfers bytes from this channel ' s file to the given writable byte
* Channel.
* <p> This method was potentially much more efficient than a simple loop
* That reads from this channel and writes to the target channel. Many
* Operating systems can transfer bytes directly from the filesystem cache
* to the target channel without actually copying them. </p>
Public Abstract Long transferTo (long position, long count,
writablebytechannel target)
throws IOException;
The above is the new method of FileChannel.

The FileInputStream member variables in the I/O mechanism in traditional Java:

Private final FileDescriptor FD;
The traditional Java file system uses the form of file descriptors to remember where files are accessed

The NIO mechanism in Java also uses a similar mechanism:

Used to make native read and write calls
private static nativedispatcher nd;

Memory allocation size for mapping buffers
private static long allocationgranularity;

Cached field for Mappedbytebuffer.isamappedbuffer
private static Field Isamappedbufferfield;

File Descriptor
Private FileDescriptor FD;
Above is a specific FileChannel class, Filechannelimpl part of the member variable.

NiO and IO in the Java file system

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.