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