NIO组件Channel
基本介绍
- NIO的通道类似于流, 但有些区别:
- 通道可以同时进行读写, 而流只能读或者只能写
- 通道可以实现异步读写数据
- 通道可以从缓冲区(Buffer)读数据, 也可以写数据到缓冲区
- BIO中的stream是单向的, 例如 FileInputStream 对象只能进行读取数据的操作, 而NIO中的通道(Channel)是双向的, 可以读操作, 也可以写操作。
- Channel在NIO中是一个接口: public interface Channel extends Closeable{}
- 常用的 Channel类有: FileChannel、DatagramChannel、ServerSocketChannel 和 SocketChannel。
- FileChannel 用于文件的数据读写, DatagramChannel 用于UDP的数据读写, ServerSocketChannel 和 SocketChannel 用于 TCP 的数据读写。
FileChannel类
FileChannel主要用于对本地文件进行IO操作, 常见方法有:
- public int read(ByteBuffer dst): 从通道读取数据并放到缓冲区中
- public int write(ByteBuffer src): 把缓冲区的数据写到通道中
- public long transferFrom(ReadableByteChannel src, long position, long count), 从目标通道中复制数据到当前通道
- public long transferTo(long position, long count, WritableByteChannel target), 把数据从当前通道复制给目标通道
一个简单demo
示意图

- 需要注意的是在将数据存入ByteBuffer再将数据写入到Channel中时需要对ByteBuffer进行flip转换改变模式。
写入案例代码
package com.ronnie.nio; import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; public class NIOFileChannel01 {
public static void main(String[] args) throws IOException {
String str = "hello, the world of code"; // 创建一个输出流 -> channel
FileOutputStream fileOutputStream = new FileOutputStream("E:/fun/nio/doc/text.txt"); // 通过 fileOutputStream获取对应的 FileChannel
// 需要注意的是: 此fileChannel真实类型是 FileChannelImpl
FileChannel fileChannel = fileOutputStream.getChannel(); // 创建缓冲区 ByteBuffer
ByteBuffer byteBuffer = ByteBuffer.allocate(1024); // 将 str 放入 byteBuffer
byteBuffer.put(str.getBytes()); // 对byteBuffer进行反转(flip)
byteBuffer.flip(); // 将byteBuffer 数据写入到channel
fileChannel.write(byteBuffer);
fileOutputStream.close();
}
}
细节
fileOutputStream.getChannel()获取的 fileChannel 真实类型是 FileChannelImpl, 是FileChannel的实现类, FileChannel本身只是一个抽象类。

getChannel()方法:
/**
* Returns the unique {@link java.nio.channels.FileChannel \
* FileChannel} object associated with this file output stream.
* 返回与此文件输出流相关的唯一FileChannel对象
*
* <p> The initial {@link java.nio.channels.FileChannel#position()
* position} of the returned channel will be equal to the
* number of bytes written to the file so far unless this stream is
* in append mode, in which case it will be equal to the size of the
* file.
* 返回的channel的初始位置与写入到此文件此文件的byte数一致(除非这个流式可
* 添加模式, 该模式下channel的初始位置会与该文件的大小一致)
* Writing bytes to this stream will increment the channel's position
* accordingly.
* 将字节码写入到该流会增加该channel的位置
* Changing the channel's position, either explicitly or by
* writing, will change this stream's file position.
* 改变管道的位置, 无论是显示的修改还是通过写入修改, 都会改变此流的文件位置
*
* @return the file channel associated with this file output stream
*
* @since 1.4
* @spec JSR-51
*/
public FileChannel getChannel() {
// 同步锁锁了当前输出流对象
synchronized (this) {
// 如果管道为空
if (channel == null) {
// 就创建一个新的FileChannelImpl对象赋给channel
channel = FileChannelImpl.open(fd, path, false, true, append, this);
}
return channel;
}
}
FileChannelImpl.open()方法:
public static FileChannel open(FileDescriptor var0, String var1, boolean var2, boolean var3, boolean var4, Object var5) {
return new FileChannelImpl(var0, var1, var2, var3, var4, var5);
}
读取案例代码
package com.ronnie.nio; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; public class NIOFileChannel02 {
public static void main(String[] args) throws IOException { // 创建文件输入流
File file = new File("E:/fun/nio/doc/text.txt"); FileInputStream fileInputStream = new FileInputStream(file); // 通过fileInputStream 获取对应的fileChannel -> 实际类型 FileChannelImpl
FileChannel fileChannel = fileInputStream.getChannel(); // 创建缓冲区
ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length()); // 将通道的数据读入到buffer中
fileChannel.read(byteBuffer); // 将字节数据转成String
System.out.println(new String(byteBuffer.array()));
fileInputStream.close();
}
}- byteBuffer.array()返回的是底层的那个字节数组, 然后再将它转换成String类型。
一个Buffer完成文件读取案例
示意图

代码
package com.ronnie.nio; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; public class NIOFileChannel03 {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("1.txt");
FileOutputStream fileOutputStream = new FileOutputStream("2.txt"); FileChannel channel01 = fileInputStream.getChannel();
FileChannel channel02 = fileOutputStream.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(512); // 循环读取
while (true){
// 非常重要的操作, 复位(重置标志位)
// 如果没写, 当position与limit相等时, read永远为0, 进入死循环
byteBuffer.clear(); int read = channel01.read(byteBuffer);
if (read == -1){
break;
}
// 将buffer 中的数据写入到 channel02
byteBuffer.flip();
channel02.write(byteBuffer);
} // 关闭流
fileInputStream.close();
fileOutputStream.close();
}
}
使用transferFrom()方法拷贝文件案例
package com.ronnie.nio; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel; public class NIOFileChannel04 {
public static void main(String[] args) throws IOException { // 创建线管的流
FileInputStream fileInputStream = new FileInputStream("E:/fun/nio/doc/hadoop.jpg");
FileOutputStream fileOutputStream = new FileOutputStream("E:/fun/nio/doc/hadoop2.jpg"); // 获取各个流对应的fileChannel
FileChannel sourceChannel = fileInputStream.getChannel();
FileChannel destinationChannel = fileOutputStream.getChannel(); // 使用transferFrom完成拷贝
destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); // 关闭相关通道和流
sourceChannel.close();
destinationChannel.close();
fileInputStream.close();
fileOutputStream.close(); }
}
Buffer 和 Channel的注意事项和细节
ByteBuffer支持类型化的put和get, put放入的是什么数据类型, get就应该使用相应的数据类型来取出, 否则可能有 BufferUnderflowException 异常
package com.ronnie.nio; import java.nio.ByteBuffer; public class NIOByteBufferPutGet {
public static void main(String[] args) { // 创建一个Buffer
ByteBuffer buffer = ByteBuffer.allocate(64); // 类型化放入数据
buffer.putInt(100);
buffer.putLong(9);
buffer.putChar('w');
buffer.putShort((short) 4); // 取出
buffer.flip(); System.out.println(); System.out.println(buffer.getInt());
System.out.println(buffer.getLong());
System.out.println(buffer.getChar());
System.out.println(buffer.getShort());
// 加个没的, 让它报错
System.out.println(buffer.getLong());
}
}
100
9
w
4
Exception in thread "main" java.nio.BufferUnderflowException
at java.nio.Buffer.nextGetIndex(Buffer.java:506)
at java.nio.HeapByteBuffer.getLong(HeapByteBuffer.java:412)
at com.ronnie.nio.NIOByteBufferPutGet.main(NIOByteBufferPutGet.java:26)
可以将一个普通Buffer转成只读Buffer, 转换后不能再写入
package com.ronnie.nio; import java.nio.ByteBuffer; public class ReadOnlyBuffer {
public static void main(String[] args) { // 创建一个buffer
ByteBuffer buffer = ByteBuffer.allocate(64); for (int i = 0; i < 64; i++){
buffer.put((byte) i);
} // 读取
buffer.flip(); // 得到一个只读的Buffer
ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
System.out.println(readOnlyBuffer.getClass()); // 读取
while (readOnlyBuffer.hasRemaining()){
System.out.println(readOnlyBuffer.get());
} // 会抛出ReadOnlyBufferException
readOnlyBuffer.put((byte) 100);
}
}
NIO还提供了 MappedByteBuffer, 可以让文件直接在内存(堆外的内存)中进行修改。
package com.ronnie.nio; import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel; /**
* MappedByteBuffer 可让文件直接在堆外内存修改, 操作系统无需将数据拷贝到用户态内存中,
* 即零拷贝, kafka底层就是依靠netty实现了零拷贝
*/ public class MappedByteBufferTest {
public static void main(String[] args) throws IOException {
RandomAccessFile randomAccessFile = new RandomAccessFile("1.txt", "rw"); // 获取对应的通道
FileChannel channel = randomAccessFile.getChannel(); /**
* 参数1: FileChannel.MapMode.READ_WRITE 使用的读写模式
* 参数2: 0: 可以直接修改的起始位置
* 参数3: 5: 是映射到内存的大小(不是索引位置), 即将 1.txt 的多少个字节映射到
* 内存
* 可以直接修改的范围就是0~5, 不到5
*/
MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5); mappedByteBuffer.put(0, (byte) 'S');
mappedByteBuffer.put(3, (byte) '8');
// mappedByteBuffer.put(5, (byte) 'G'); 会抛出数组越界异常 randomAccessFile.close();
System.out.println("修改成功");
}
}之前所说的读写操作都是通过一个Buffer完成的, NIO还支持通过多个Buffer(即Buffer数组) 完成读写操作, 即Scattering(分散) 和 Gathering(聚合)
案例代码:
package com.ronnie.nio; import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Arrays; /**
* Scattering: 将数据写入到buffer时, 可以采用buffer数组, 依次写入 [分散]
* Gathering: 从buffer读取数据时, 可以采用buffer数组, 依次读
*/
public class ScatteringAndGatheringTest {
public static void main(String[] args) throws IOException { // 使用 ServerSocketChannel 和 SocketChannel 网络 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
InetSocketAddress inetSocketAddress = new InetSocketAddress(7000); // 绑定端口到socket, 并启动
serverSocketChannel.socket().bind(inetSocketAddress); // 创建Buffer数组
ByteBuffer[] byteBuffers = new ByteBuffer[2]; byteBuffers[0] = ByteBuffer.allocate(5);
byteBuffers[1] = ByteBuffer.allocate(3); // 等待客户端连接(telnet)
SocketChannel socketChannel = serverSocketChannel.accept();
// 假定从客户端接收8个字节
int messageLength = 8; // 循环的读取
while (true){
int byteRead = 0;
while (byteRead < messageLength){
long l = socketChannel.read(byteBuffers);
// 累计读取的字节数
byteRead += 1;
System.out.println("byteRead = " + byteRead);
// 使用流打印, 查看当前buffer的position和limit
Arrays.stream(byteBuffers).map(buffer -> "position = " + buffer.position() + ", limit = " + buffer.limit()).forEach(System.out::println); }
// 将所有的buffer进行反转(flip)
Arrays.asList(byteBuffers).forEach(Buffer::flip); // 将数据读出显示到客户端
long byteWrite = 0;
while(byteWrite < messageLength){
long l = socketChannel.write(byteBuffers);// 回写
byteWrite += 1;
}
// 将所有的 buffer 进行 clear操作
Arrays.asList(byteBuffers).forEach(Buffer::clear); System.out.println("byteRead := " + byteRead + " byteWrite = " + byteWrite + ", messageLength = " + messageLength);
} }
}windows的话打开win + R输入cmd, telnet 127.0.0.1 7000, 就就可以发送数据, 没有开启的请: 控制面板 -> 程序 -> 启用或关闭 Windows功能 -> 开启Telnet Client
NIO组件Channel的更多相关文章
- Java基础知识强化之IO流笔记73:NIO之 Channel
1. Java NIO的Channel(通道)类似 Stream(流),但又有些不同: 既可以从通道中读取数据,又可以写数据到通道.但流的读写通常是单向的. 通道可以异步地读写. 通道中的数据总是要先 ...
- NIO组件Selector调用实例
*对于nio的非阻塞I/O操作,使用Selector获取哪些I/O准备就绪,注册的SelectionKey集合记录关联的Channel这些信息.SelectionKey记录Channel对buffer ...
- NIO组件之channel
Java NIO指的是new IO ,相对OIO,也称non-blocking IO,对应四种基本IO类型中的IO多路复用,主要有有三大核心组件,Channel(管道),Buffer(缓冲区),sel ...
- NIO组件Selector详解
Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程可以管理多个channel,从而管理多个网络连接. 下面是 ...
- 《精通并发与Netty》学习笔记(10 - 详解NIO (一) Channel、Buffer )
一.Java NIO 概述 Java NIO 由以下几个核心部分组成:ChannelsBuffersSelectors虽然Java NIO 中除此之外还有很多类和组件,但在我看来,Channel,Bu ...
- JAVA基础知识之NIO——Buffer.Channel,Charset,Channel文件锁
NIO机制 NIO即NEW IO的意思,是JDK1.4提供的针对旧IO体系进行改进之后的IO,新增了许多新类,放在java.nio包下,并对java.io下许多类进行了修改,以便使用与nio. 在ja ...
- Java NIO教程 Channel
Channel是一个连接到数据源的通道.程序不能直接用Channel中的数据,必须让Channel与BtyeBuffer交互数据,才能使用Buffer中的数据. 我们用FileChannel作为引子, ...
- 《JAVA NIO》Channel
3.通道 Channle主要分为两类:File操作对应的FIleChannel和Stream操作对应的socket的3个channe. 1.这3个channel都是抽象类.其具体实现在SPI里面. 2 ...
- Java基础知识强化之IO流笔记76:NIO之 Channel(通道)之间的数据传输
1. 在Java NIO中,如果两个通道中有一个是FileChannel,那你可以直接将数据从一个channel(译者注:channel中文常译作通道)传输到另外一个channel. (1)trans ...
随机推荐
- base64和图片的相互转换
package czc.superzig.modular.utils; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; im ...
- 吴裕雄--天生自然 JAVA开发学习:java使用Eclipel连接数据库
1:jar可到Mysql官网下载:地址Mysql 连接jar包.:https://dev.mysql.com/downloads/connector/j/如图,在下拉列表框中选择Platform In ...
- Atom 必装插件
Atom 必装插件 转载请注明出处. https://blog.csdn.net/Nick_php/article/details/54020956 主题 atom-material-ui 字体配色 ...
- mac下删除不需要的应用程序
一般的应用程序删除: 1)可以在 前往--应用程序 中直接删除 2)直接在启动台中按住出现X直接删除. 问题: mac下不出现在应用程序中,启动台中按住也不出现X,也不可以直接拖到废纸篓中删除的应用如 ...
- C++中函数访问数组的方式
在书写C++代码时,往往为了令代码更加简洁高效.提高代码可读性,会对定义的函数有一些特殊的要求:比如不传递不必要的参数,以此来让函数的参数列表尽可能简短. 当一个函数需要访问一个数组元素时,出于上述原 ...
- Java-读取.properties配置文件空指针异常java.lang.NullPointerException解决方法
异常显示: 问题所在: 加载.properties配置文件路径多了一个斜杠 "/" : 解决方法: 去掉斜杠,重新启动程序即可. 总结: 遇到问题还是要认真阅读异常信息!
- yii 框架 – 安全站点和非安全站点的 URL 管理
在本文中我将描述怎么管理安全站点和非安全站点的 URL 管理. 安全站点的内容使用httpsSSL (安全套接字层) 协议发送,而非安全站点使用http协议.为了描述简单,我们称前者https内容/页 ...
- 「SCOI2010」连续攻击游戏
传送门 Luogu 解题思路 二分图匹配,关键是建图. 如果我们naive地直接把每个武器的两个属性分为两部建图的话,显然是跑不了的. 我们考虑把每一个武器的属性向它连边:\(a_i \rightar ...
- 【Game学习随笔01】挑战任务01
今天是2020年2月6日,时间过得好快,以至于我在写到时间会下意识写成2019年…… 看来全国肺炎情况进一步升温了,以至于我家所在的小区进行了命令封锁通知,所以出行不再像以前那么自由了,不管怎样,给战 ...
- 范数(norm)
[范数定义] 非负实值函数(非线性) 1)非负性: || a || >= 0 2)齐次性: || ka || = |k| ||a|| 3)三角不等式: || a + b || <= || ...