Java NIO Learning Series II: Channel

  Buffer summarizes the relevant knowledge Java NIO earlier in this article, we summarize its brothers: Channel. There comes above, Java NIO are paired and Channel Buffer general, in all IO NIO originates at a Channel, a stream is equivalent to a Channel ,, data may be read from the Channel Buffer, or write Channel the data.

Channel Profile

FileChannel

SocketChannel

ServerSocketChannel

DatagramChannel

 

1. Channel Profile

  Java NIO similar flow in the Channel, but there are some differences:

  • Channel can support both write also support read, and the flow is one-way, can only support write or read;
  • Channel support asynchronous read and write;
  • Channel Buffer general and supporting the use of the read data from the Channel Buffer in Buffer from or written to the Channel;

  Channel main implementation class are the following:

  • FileChannel, can file read / write data;
  • DatagramChannel, reading from the network UDP / write data;
  • SocketChannel, read from the network via TCP / write data;
  • ServerSocketChannel, allowing you to monitor TCP connections for each TCP connection creates a SocketChannel;

 

2. FileChannel

  Java NIO FileChannel channel is connected to a class file, through which data can be read from a file, or write data to the file. FileChannel class is used instead of the standard Java IO API to read and write files provided by Java NIO libraries. FileChannel not be set to non-blocking mode, only works in blocking mode.

2.1 Open FileChannel

  Before using FileChannel first to open it, I / O library has been changed three classes, for generating FileChannel, these three classes are: InputStream, OutputStream, RandomAccessFile, the following is how to obtain FileChannel from RandomAccessFile:

RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
FileChannel inChannel = aFile.getChannel();

2.2 Data read from FileChannel

  First, you need to assign a Buffer, the data read from FileChannel will read the Buffer (is not it a bit around). Then call read FileChannel () method to read the data, this method will read the data in the Buffer, the representative return int number of bytes read, return -1 for the end of file.

ByteBuffer buf = ByteBuffer.allocate(48);
int bytesRead = inChannel.read(buf);

2.3 to write data FileChannel

  To which data can be written by calling FileChannel the write () method, parameter is a Buffer:

String newData = "New String to write to file..." + System.currentTimeMillis();
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
while(buf.hasRemaining()) {
    channel.write(buf);
}

  Here write () method there is no guarantee how much data is written once, we just repeat until finished.

2.4 Close FileChannel

  Remember exhausted after FileChannel turn it off:

channel.close();

2.5 FileChannel position

  Call FileChannel object position () method can get its position, you can also call position (long pos) set its position:

// 获取position
long pos channel.position();
// 设置position
channel.position(pos +123);

  If the position is set to the rear end of the file, and then try to read the file, it will return -1;

  If the position is set to the back end of the file, and then try to write data to a file, the file will be automatically extended, and began to write the data from the position set at the position, which will lead to "file hole", that is, the physical file will be gaps.

2.6 FileChannel size

  size () method returns the size of file filechannel connected:

long fileSize = channel.size(); 

2.7 truncated FileChannel

  truncate () method can truncate the file:

channel.truncate(1024);

  As the file interception length is 1024 bytes.

2.8 FileChannel Force

  FileChannel.force () method refreshes all the data is not written to disk to disk. Because the operating system will first data cached in memory, and then write-once disk, we can not guarantee that data written to the channel is written to disk, so you can call flush () method forces the data to disk.

  force () method has a boolean parameter, whether on behalf of the metadata (such as permissions) written to the file:

channel.force(true);

 

3. SocketChannel

  Java NIO SocketChannel connected network socket for TCP and is equivalent to Java network packet Socket. You can create a SocketChannel in two ways:

  • And it opens a SocketChannel and a server connected on the network;
  • When there is a connection to reach a ServerSocketChannel automatically creates a SocketChannel;

3.1 Open Channel Socket

  The following example illustrates how to open a SocketChannel:

SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("http://jenkov.com", 80));

3.2 channel Close Socket

  For the use of this resource class, it is to remember to shut down:

socketChannel.close();

3.3 Reading from a SocketChannel

  Call the read () method can read the data:

 

4. ServerSocketChannel

5. DatagramChannel

 

Guess you like

Origin www.cnblogs.com/volcano-liu/p/11066883.html