IO Noriyuki NIO

What is IO streams

I is Input (Input), O refers to the Output (output).

In Java, the input and output files is achieved through the flow (Stream), the concept of flow from the duct concept Unix (pipe) a. In Unix systems, the pipe is a continuous stream of bytes, used to communicate between programs or processes, or read-write peripherals, external files.

A stream, there must be a source and destination, they may be certain areas of the computer's memory, it can be a disk file, or even a URL on the Internet. For flow, we do not care how the data is transmitted only need to enter the data source, you can get the data to the destination.

According to the data stream processing unit it can be divided into a byte stream and a stream of characters; in accordance with the flow into the input and output streams (Note: the input and output streams are standing procedural point of reference).

NIO (immediately New IO)

JDK1.4 version starts, JDK IO operation provides a new API, NIO to provide multiple (non-blocking) a highly scalable non-blocking network I / O, thereby increasing the efficiency, NIO there are three core components: Channel, Buffer and Selector, where the first two focus on learning, Selector will be detailed in network programming.

Buffer

Buffer is an abstract class, the type of a variable corresponding to the Buffer object represents a buffer, ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer and ShortBuffer Buffer classes are subclasses of abstract classes, wherein the most common ByteBuffer.

  • ①static ByteBuffer allocate (int capacity): assign a new byte buffer, capacity buffer sizes for this capacity, capacity is also the limit of the current buffer
public class Test {
 	public static void main(String[] args) {
   		ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
 	}
}
  • ②int capacity (): Returns the size of this buffer is, in fact, directly to the capacity member property values ​​returned.
public class Test {
 	public static void main(String[] args) {
   		ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
   		System.out.println(bytebuffer.capacity());
 	}
}
  • ③ByteBuffer put (byte b): the type of data bytes written to the buffer current position, and position + 1, 0 from the start position
public class Test {
 	public static void main(String[] args) {
   		ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
   		byte a=3;
   		bytebuffer.put(a);
 	}
}
  • ④byte [] array (): ByteBuffer type data into the 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 buffer's current position, in fact, directly to the position member property values ​​returned.
public class Test {
 	public static void main(String[] args) {
   		ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
   		System.out.println(bytebuffer.position());//0
   		byte a=3;
   		bytebuffer.put(a);
   		System.out.println(bytebuffer.position());//1
 	}
}
  • ⑥Buffer flip (): flip buffer, assigning a value to limit the current position, and the position zero; this method is mainly to prevent the output once in the end, if the buffer is not full, then the position is the value assigned to the limit the current amount of data in the buffer, with the general method hasRemaining. If the attribute is not used directly limit capacity, redundant data is output.
    Here Insert Picture Description
public class Test {
 	public static void main(String[] args) {
   		ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
   		bytebuffer.position();//0
  	 	byte a=3;
   		bytebuffer.put(a);
   		bytebuffer.position();//1
   		bytebuffer.flip();
   		bytebuffer.position();//0
 	}
}
  • ⑦byte get () reads the current position of the byte buffer, and then the current position +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();//1
   		bytebuffer. flip();
   		bytebuffer.position();//0
   		bytebuffer.get();
   		bytebuffer.position();//1
 	}
}
  • ⑧boolean hasRemaining (): whether to tell you when to release the buffer has been reached on border buffer
public class Test {
 	public static void main(String[] args) {
  		ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
  		System.out.println(bytebuffer.position());//0
  		byte a=3;
  		bytebuffer.put(a);
  		System.out.println(bytebuffer.position());//1
  		System.out.println(bytebuffer.hasRemaining());//true
  		bytebuffer.flip();
  		bytebuffer.get();
  		System.out.println(bytebuffer.position());//1
  		System.out.println(bytebuffer.hasRemaining());//false
 	}
}

Cnnl

Channel is an interface, which type of variable to point object represents a data transmission channel, a Channel Buffer Object oriented: data is always read from the channel to the buffer (Buffer type object), or from a buffer (Buffer Object Type ) is written into the channel

The main implementation class Channel interfaces as follows:

①FileChannel: read and write data from the file.
②DatagramChannel: read and write data network through UDP.
③SocketChannel: write data network through TCP.
④ServerSocketChannel: you can listen for new incoming TCP connections, such as a Web server as a SocketChannel to create each new incoming connections will be.

public class Test{
	public static void main(String[] args) {
  		try {
   			FileInputStream fileInputStream = new FileInputStream("D:\\timg.jpg");
   			FileChannel inputChannal = fileInputStream.getChannel();
   
   			FileOutputStream fileOutputStream = new FileOutputStream("D:\\hh.jpg");
   			FileChannel outputChannal = fileOutputStream.getChannel();
   
   			ByteBuffer buffer = ByteBuffer.allocate(1024);
   			while((inputChannal.read(buffer))!=-1) {
    				buffer.flip();
    				outputChannal.write(buffer);
    				buffer.clear();
   			}
  		} catch (Exception e) {
   			e.printStackTrace();
  		}
 	}
}
Published 101 original articles · won praise 3 · Views 2219

Guess you like

Origin blog.csdn.net/S_Tian/article/details/104634525