Buffer and channel in Java NIO

Source: Internet
Author: User

Buffer:
one, which is responsible for data access in Java NIO. The buffer is an array. Used to store data of different data types

depending on the data type (except for the Boolean), a buffer of the appropriate type is provided:
Bytebuffer
Charbuffer
Shortbuffer
Intbuffer
Longbuffer
Floatbuffer
DoubleBuffer

the above buffers are managed in almost the same way that the buffers are obtained through allocate ()

Ii. Two core methods of buffer Access data:
put (): Deposit data into buffer
get (): Gets the data in the buffer

iii. four core properties in the buffer:
capacity: capacity, which represents the maximum amount of data stored in the buffer. Once the declaration cannot be changed.
limit: Bounds that indicates the size of the data that can be manipulated in the buffer. (data cannot be read or written after limit)
Position: Position that represents the position in the buffer where the data is being manipulated.

Mark : Mark, indicating the location of the current position. Can be restored to mark's location via Reset ()

0 <= Mark <= position <= limit <= capacity

iv. Direct Buffer vs. non-direct buffers:
non-direct buffers: allocates buffers through the allocate () method, setting buffers in the JVM's memory
Direct buffer: Allocates a direct buffer through the Allocatedirect () method, establishing the buffer in physical memory. can improve efficiency

Channels (channel):
One, the connection for the source node and the target node. In Java NIO, the transfer of data in the buffer is responsible. The Channel itself does not store data, so it needs to be transported in conjunction with a buffer. 

Second, the main realization class of the channel
Java.nio.channels.Channel Interface:
|--filechannel
|--socketchannel
|--serversocketchannel
|--datagramchannel

third, access to the channel
1. Java provides a Getchannel () method for a class that supports channels
Local IO:
Fileinputstream/fileoutputstream
Randomaccessfile

Network io:
Socket
ServerSocket
Datagramsocket

2. The nio.2 in JDK 1.7 provides a static method for each channel open ()
3. Newbytechannel () of the Files tool class of nio.2 in JDK 1.7

iv. data transfer between channels
Transferfrom ()
TransferTo ()

v. Dispersion (scatter) and aggregation (Gather)
Scatter read (scattering Reads): Spread the data in the channel across multiple buffers
Aggregate Write (gathering writes): Aggregates data from multiple buffers into the channel

VI. Character Set: Charset
encoding: Byte array, string
decoding: Byte array-string


Buffer and channel in Java NIO

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.