Java New IO

Source: Internet
Author: User

The traditional IO

Stream-oriented input and output systems such as InputStream, OutputStream, Reader, and writer in Java are considered traditional IO. Traditional IO is a blocking input and output, and is handled by byte movement, that is, traditional IO can only process one byte at a time and is inefficient.

New IO

Both the new IO and the traditional IO have the same purpose and are used for input and output functions. However, the new IO uses a memory-mapped file to process the input and output, and the new IO maps a section of the file or file into memory so that the file can be accessed like memory, which is much faster than the traditional IO.

The new IO-related packages in Java are as follows:

    • Java.nio Package: Mainly provides some classes related to buffer;
    • Java.nio.channel package: mainly includes channel and selector related classes;
    • Java.nio.charset Package: mainly contains the class related to the character set;
    • JAVA.NIO.CHANNEL.SPI Package: Mainly includes the class that provides the channel service;
    • JAVA.NIO.CHARSET.SPI Package: Primarily contains classes that provide character set services.

The main concerns are four types of channel, Buffer, Charset, Selector

Channel and buffer are the two core objects in the new IO, the channel is a simulation of the traditional input and output, and all the data in the new IO system needs to be transmitted through the channel. The biggest difference between the channel and the traditional InputStream and OutputStream is that it provides a map method that maps the "piece of data" directly into memory. If traditional IO is a stream-oriented process, the new IO is a block-oriented process.

Buffer can be understood as a container whose essence is an array, and all objects sent to the channel must first be placed in buffer, and the objects read from the channel must first be read into buffer. As its name, buffer plays the role of Buffering.

The charset is used to map Unicode strings into byte sequences and inverse map operations.

The selector class is used to support non-blocking input and output operations.

Use of buffer

The internal structure of buffer is similar to an array, and can hold more than one type of data. Buffer is an abstract class with corresponding buffer classes for each base type (except for the Boolean), such as Bytebuffer, Shortbuffer, Charbufferr, Intbuffer, Longbuffer, Floatbuffer, DoubleBuffer and so on. These buffer classes, in addition to Bytebuffer, use the same or similar methods to manage data. None of these buffer constructors, creating a buffer object with a capacity of capacity by using a static Xxxbuffer allocate (int capacity).

The Bytebuffer and Charbuffer are used more in the buffer class, and the other buffer subclasses are less used. Bytebuffer also has a subclass mappedbytebuffer, which is used to represent the result of the channel mapping part or all of a disk file to memory, usually Mappedbytebuffer objects are returned by the channel's Map method.

There are 3 important concepts in buffer: capacity (capacity), limit (limit), position (position).

    • Capacity: Identifies the maximum capacity of buffer that can store up to how much data. Capacity cannot be negative and cannot be changed after it is created. (This is similar to the array)
    • Limit: The first buffer position index that cannot be read and cannot be written to. In other words, the data after limit is neither readable nor writable.
    • Position: Used to indicate the next buffer position index (similar to a record pointer in traditional IO) that can be read out or written to. When you use buffer to read data from the channel, the value of the position position is exactly equal to how much data has been blocked. When a new buffer object is created, its position is 0.
    • There is also an optional marker mark,mark that allows the position to be positioned directly to mark.

Mark, position, limit, and capacity meet the following relationships:

Mark<=position<=limit<=capacity.

The primary function of buffer is to mount the data and then output the data, and the position and limit are changed after the write to read the data:

    1. When the buffer object was first created, its position is 0,limit to capacity;
    2. The program calls the put method to fill the buffer with data continuously, each fill some data, buffer's position moves backward some position;
    3. When buffer fills the data, it calls the Flip method, sets the limit to the location where position is located, and then sets the position to 0;
    4. This allows you to read the data from buffer, and each time you read some data, the position will move back some positions;
    5. After the data has been read, the clear method needs to be called, the clear method does not empty the data in buffer, but instead resets the position to 0, resets the limit to capacity, waiting for the data to be written again.

Based on the above steps, you can find that the flip method of buffer is ready to read the data, and the clear method is ready to write the data again.

Put and get are methods for writing and reading data in buffer. Buffer supports both reading and writing of individual data, as well as bulk read and write (array parameters).

When you use put and get to read and write data in buffer, it is divided into absolute and relative two ways:

    • Relative: Reads or writes data from the current position of the buffer, and then increments the value of position by the number of elements processed.
    • Absolute: Reads and writes data directly to buffer according to the index, and does not change the location of the position.

Java New IO

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.