Buffer and Channel in NIO

NIO (New IO) concept:

Since version 1.4 of JDK, JDK has provided a new IO operation API. NIO provides non-blocking, non-blocking, highly scalable network I/O, which improves efficiency. NIO has three main core components: Channel, Buffer and Selector (Select we'll talk about later).

Buffer

1. Buffer is an abstract class. The object corresponding to Buffer type variable represents a buffer. ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer and ShortBuffer are all subclasses of Buffer Abstract class, of which ByteBuffer is the most commonly used.

2. Common methods of ByteBuffer:

  • Static ByteBuffer Allocation (int capacity): Allocate a new byte buffer
public class Test {
	public static void main(String[] args) {
			ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
	}
}
  • (2) int capacity(): Returns the capacity of this buffer
public class Test {
	public static void main(String[] args) {
			ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
			System.out.println(byteBuffer.capacity());
	}
}
  • (3) ByteBuffer put(byte b): Write byte type data to the buffer of the current location, then the current location + 1, starting from 0.
public class Test {
	public static void main(String[] args) {
			ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
			byte a=3;
			bytebuffer.put(a);
	}
}
  • (4) byte[] array(): Convert data of type ByteBuffer to byte array
public class Test {
	public static void main(String[] args) {
			ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
			byte a=3;
			bytebuffer.put(a);
			byte[] bbuf=bytebuffer.array();
	}
}
  • int position(): Returns the current location of the buffer
public class Test {
	public static void main(String[] args) {
			ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
			bytebuffer.position();
			byte a=3;
			bytebuffer.put(a);
			bytebuffer.position();
	}
}
  • Buffer flip(): flip the buffer and set position to zero
public class Test {
	public static void main(String[] args) {
			ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
			bytebuffer.position();
			byte a=3;
			bytebuffer.put(a);
			bytebuffer.position();
			bytebuffer. flip();
			bytebuffer.position();
	}
}
  • boolean hasRemaining(): When releasing the buffer, tell you if you have reached the upper bound of the buffer
public class Test {
	public static void main(String[] args) {
			ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
			bytebuffer.position();
			byte a=3;
			bytebuffer.put(a);
			bytebuffer.position();
			bytebuffer. flip();
			bytebuffer.position();
	}
}
  • byte get() reads the bytes of the current location of the buffer, and then the current location + 1
public class Test {
	public static void main(String[] args) {
			ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
			bytebuffer.position();
			byte a=3;
			bytebuffer.put(a);
			bytebuffer.position();
			bytebuffer. flip();
			bytebuffer.position();
			bytebuffer.get();
	}
}
  • Buffer clear(): Reset the buffer to an empty state. It does not change any data elements in the buffer, but simply sets the upper bound to the value of capacity and the position back to 0.
public class Test {
	public static void main(String[] args) {
			ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
			bytebuffer.position();
			byte a=3;
			bytebuffer.put(a);
			bytebuffer.clear();
			bytebuffer.position();
			System.out.println(bytebuffer.hasRemaining());
	}
}

Channel

1. Channel is an interface. The object that the interface type variable points to represents a data transmission channel. The Channel object is buffer-oriented: data is always read from the channel to the buffer (Buffer-type object), or written from the buffer (Buffer-type object) to the channel.

2. The main implementation classes of Channel interface are as follows:

  • File Channel: Read and write data from a file.
  • Data gram Channel: Read and write data in the network through UDP.
  • (3) Socket Channel: Read and write data in the network through TCP.
  • Server Socket Channel: You can listen for new incoming TCP connections. Like Web servers, you create a Socket Channel for every new incoming connection.
public class Test {
	public static void main(String[] args) {
		try {
			FileInputStream fileInputStream=new FileInputStream("D:\\1.txt");
			FileChannel inChannel=fileInputStream.getChannel();
			
			FileOutputStream fileOutputStream=new FileOutputStream("D:\\2.txt");
			FileChannel ouChannel=fileOutputStream.getChannel();
			
			ByteBuffer byteBuffer= ByteBuffer.allocate(1024);
			while(inChannel.read(byteBuffer)!=-1) {
				byteBuffer.flip();
				ouChannel.write(byteBuffer);
				byteBuffer.clear();
			}	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Keywords: socket network JDK

Added by ginginca on Thu, 22 Aug 2019 13:58:03 +0300