3. Buffers and channels
At the heart of the Java NIO system are channels and buffers. Channels represent open connections to IO devices (e.g., files, sockets). If you want to use a NIO system, you need to get the channel to connect to the IO device and the buffer to hold the data. The buffer is then manipulated to process the data
In short, a Channel is responsible for transmission and a Buffer is responsible for storage
3.1 Introduction to Buffers
-
Buffer: A container for a specific base data type. Defined by the java.nio package, all buffers are subclasses of the Abstract Buffer class.
-
Buffers in Java NIO are used primarily to interact with NIO channels, from which data is read into the Buffer and from which data is written into the channel.
-
A Buffer is like an array that can hold multiple pieces of the same type of data. Depending on the data type (except Boolean), there are the following common subclasses of Buffer:
- ByteBuffer
- CharBuffer
- ShortBuffer
- IntBuffer
- LongBuffer
- FloatBuffer
- DoubleBuffer
The above Buffer classes all take a similar approach to managing data, but separately
Different types of data are managed. A Buffer is obtained by using the following method
object
3.2 Basic properties of buffers
-
Capacity: indicates the maximum capacity of the Buffer. The capacity of the Buffer cannot be negative and cannot be changed after creation.
-
Limit: The index of the first data that should not be read or written, that is, data after the limit cannot be read or written. The buffer limit cannot be negative and cannot be larger than its capacity.
-
Position: The index of the next data to read or write. The position of the buffer cannot be negative and cannot exceed its limit
-
Mark and reset: The mark is an index that specifies a specific position in the Buffer by using the mark() method in the Buffer, which can be restored by calling the reset() method.
3.3 Common methods of Buffer
3.4 Buffer data manipulation
All subclasses of Buffer provide two methods for data manipulation: get() and PUT ()
- Get the data in Buffer
- Get () : Reads a single byte
- Get (byte[] DST) : reads multiple bytes to DST in batches
- Get (int index) : reads bytes at the specified index position (does not move position)
- Put data into Buffer
- Put (byte B) : Writes the given single byte to the current position of the buffer
- Put (byte[] SRC) : writes bytes from SRC to the current position of the buffer
- Put (int index, byte B) : writes the specified byte to the index position of the buffer (does not move position)
/** * Date: 2020/5/12 * Time: 14:53 * Content: ** 一 Buffers: Are responsible for data access in Java NIO. Buffers are arrays. Use to store data of different data types * * Depending on the data type (except Boolean), Buffers of the corresponding type * ByteBuffer * CharBuffer * ShortBuffer * IntBuffer * LongBuffer * FloatBuffer * DoubleBuffer * * are provided The above buffers are managed in almost the same way: allocate() gets buffer * * * 2. There are two core ways to access data in the buffer: * PUT () : puts data into the buffer * get() : gets the amount in the buffer * * four. The four core attributes of the buffer are as follows: * Capacity: indicates the capacity of the buffer for storing large amounts of data. * limit: indicates the size of data that can be manipulated in the buffer.(After limit, data cannot be read or written.) * position: indicates the position in the buffer where data is being manipulated. Mark to record the current position * * * position <= LIMIT <= capacty */ by reset()
public class TestBuffer {
@Test
public void test01(a) {
String str = "wangpeng";
//1. Allocate a buffer of the specified size
ByteBuffer buf = ByteBuffer.allocate(1024);
System.out.println("---------------allocate---------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
//2. Use put() to store data to the buffer
buf.put(str.getBytes());
System.out.println("---------------put---------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
//3. Switch the data reading mode
buf.flip();
System.out.println("---------------flip---------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
//4. Use get() to read data in the buffer
byte[] dst = new byte[buf.limit()];
buf.get(dst);
System.out.println(new String(dst, 0, dst.length));
System.out.println("---------------get---------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
//5, rewind() : repeatable
buf.rewind();
System.out.println("---------------rewind---------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
//6. clear() : Clears the buffer, but the data in the buffer is still in the "forgotten" state
buf.clear();
System.out.println("---------------clear---------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
System.out.println((char) buf.get());
}
@Test
public void test02(a) {
String str = "zhaopeng";
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.put(str.getBytes());
buf.flip();
byte[] dst = new byte[buf.limit()];
buf.get(dst, 0.2);
System.out.println(new String(dst, 0.2));
System.out.println(buf.position());
/ / mark () : the tag
buf.mark();
buf.get(dst, 2.2);
System.out.println(new String(dst, 2.2));
System.out.println(buf.position());
//reset() : Restores mark to his position
buf.reset();
System.out.println(buf.position());
// Determine whether there is any data left in the buffer
if (buf.hasRemaining()) {
// Get the number of operations that can be performed in the bufferSystem.out.println(buf.remaining()); }}}Copy the code