通道(Channel)的原理获取
通道表示打开到 IO 设备(例如:文件、套接字)的连接。若需要使用 NIO 系统,需要获取用于连接 IO 设备的通道以及用于容纳数据的缓冲区。然后操作缓冲区,对数据进行处理。Channel 负责传输, Buffer 负责存储。通道是由 java.nio.channels 包定义的。 Channel 表示 IO 源与目标打开的连接。Channel 类似于传统的“流”。只不过 Channel本身不能直接访问数据, Channel 只能与Buffer 进行交互。
java.nio.channels.Channel 接口:
|--FileChannel 本地文件的通道
|--SocketChannel
|--ServerSocketChannel
|--DatagramChannel
网络的关于通道
获取通道
1. Java 针对支持通道的类提供了 getChannel() 方法
本地 IO:
FileInputStream/FileOutputStream
RandomAccessFile
网络IO:
Socket
ServerSocket
DatagramSocket
2. 在 JDK 1.7 中的 NIO.2 针对各个通道提供了静态方法 open()
3. 在 JDK 1.7 中的 NIO.2 的 Files 工具类的 newByteChannel()
package com.toov5.Nio; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import org.junit.jupiter.api.Test; public class BUfferTest02 {
// 非直接缓冲区 读写操作
@Test
public void BUfferTest() throws IOException {
// 读入流
FileInputStream fileInputStream = new FileInputStream("aa.jpg");
// 写入流
FileOutputStream fileOutputStream = new FileOutputStream("bb.jpg");
// 创建通道
FileChannel iChannel = fileInputStream.getChannel(); // 读入流的通道
FileChannel oChannel = fileOutputStream.getChannel(); // 写入流的通道
//分配制定缓冲区大小
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
while (iChannel.read(byteBuffer) != -1) {
//开启读取模式
byteBuffer.flip();
//将数据写入到通道中
oChannel.write(byteBuffer);
byteBuffer.clear();
}
//关闭通道、关闭连接
iChannel.close();
oChannel.close();
fileInputStream.close();
fileOutputStream.close(); } }
运行结果:
直接缓冲区:
package com.toov5.Nio; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption; import org.junit.jupiter.api.Test; public class BUfferTest02 {
@Test
public void testZhiJie() throws IOException {
//创建管道
FileChannel inChannel =FileChannel.open(Paths.get("aa.jpg"), StandardOpenOption.READ);
FileChannel outChannel =FileChannel.open(Paths.get("bb.jpg"),StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE );
//定义映射文件
MappedByteBuffer inMappedByteBuffer =inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
MappedByteBuffer outMappedByteBuffer =outChannel.map(MapMode.READ_WRITE, 0, inChannel.size());
//直接对缓冲区操作
byte[] bytes = new byte[inMappedByteBuffer.limit()];
inMappedByteBuffer.get(bytes);
outMappedByteBuffer.put(bytes);
inChannel.close();
outChannel.close();
System.out.println("操作直接缓冲区完毕");
} // 非直接缓冲区 读写操作
@Test
public void BUfferTest() throws IOException {
// 读入流
FileInputStream fileInputStream = new FileInputStream("aa.jpg");
// 写入流
FileOutputStream fileOutputStream = new FileOutputStream("bb.jpg");
// 创建通道
FileChannel iChannel = fileInputStream.getChannel(); // 读入流的通道
FileChannel oChannel = fileOutputStream.getChannel(); // 写入流的通道
//分配制定缓冲区大小
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
while (iChannel.read(byteBuffer) != -1) {
//开启读取模式
byteBuffer.flip();
//将数据写入到通道中
oChannel.write(byteBuffer);
byteBuffer.clear();
}
//关闭通道、关闭连接
iChannel.close();
oChannel.close();
fileInputStream.close();
fileOutputStream.close(); } }
通道(Channel)的原理获取的更多相关文章
- NIO之通道(Channel)的原理与获取以及数据传输与内存映射文件
通道(Channel) 由java.nio.channels包定义的,Channel表示IO源与目标打开的连接,Channel类似于传统的“流”,只不过Channel本身不能直接访问数据,Channe ...
- Java-NIO(四):通道(Channel)的原理与获取
通道(Channel): 由java.nio.channels包定义的,Channel表示IO源与目标打开的连接,Channel类似于传统的“流”,只不过Channel本身不能直接访问数据,Chann ...
- 通道(Channel)的原理与获取
通道(Channel):由 java.nio.channels 包定义 的.Channel 表示 IO 源与目标打开的连接. Channel 类似于传统的“流”.只不过 Channel 本身不能直接访 ...
- Java NIO中的通道Channel(一)通道基础
什么是通道Channel 这个说实话挺难定义的,有点抽象,不过我们可以根据它的用途来理解: 通道主要用于传输数据,从缓冲区的一侧传到另一侧的实体(如文件.套接字...),反之亦然: 通道是访问IO服务 ...
- go中的数据结构通道-channel
1. channel的使用 很多文章介绍channel的时候都和并发揉在一起,这里我想把它当做一种数据结构来单独介绍它的实现原理. channel,通道.golang中用于数据传递的一种数据结构.是g ...
- 详解 通道 (Channel 接口)
在本篇博文中,本人主要讲解NIO 的两个核心点 -- 缓冲区(Buffer) 和 通道 (Channel)之一的 缓冲区(Buffer), 有关NIO流的其他知识点请观看本人博文<详解 NIO流 ...
- nio再学习之通道channel
通道(Channel):用于在数据传输过程中,进行输入输出的通道,其与(流)Stream不一样,流是单向的,在BIO中我们分为输入流,输出流,但是在通道中其又具有读的功能也具有写的功能或者两者同时进行 ...
- 理解CNN中的通道 channel
在深度学习的算法学习中,都会提到 channels 这个概念.在一般的深度学习框架的 conv2d 中,如 tensorflow .mxnet ,channels 都是必填的一个参数. channel ...
- 33、[源码]-AOP原理-获取拦截器链-MethodInterceptor
33.[源码]-AOP原理-获取拦截器链-MethodInterceptor
随机推荐
- DevExpress控件之LookupEdit,ComboBoxEdit
ComboBoxEdit 1. ComBoxEdit没有DisplayMember 和 ValueMember 属性,只能获取显示的值2.当前选定值comboBoxEdit1.Propertie ...
- MAT使用及OOM分析
知识及工具推荐 1.Android资源监控工具 2.Android内存管理机制 http://blog.csdn.net/hexieshangwang/article/details/47188987
- eletron 播放rtmp flash 播放器问题
1 安装 flash https://www.flash.cn/ 2 man.js 配置 参考 https://newsn.net/say/electron-flash-win.html 3 播放器 ...
- Android---63---Android中的动画效果
Android中有四种动画效果: AlphaAnimation:透明度动画效果 ScaleAnimation:缩放动画效果 TranslateAnimation:位移动画效果 RotateAnimat ...
- javascript if(条件)------------条件中可以使用的值
1.布尔变量true/false2.数字非0,非NaN/ ( 或NaN) NaN--------Not a Number 3.对象非null/(null或undefined) 4.字符串非空串(&qu ...
- js:Razor视图下服务器代码给Javascript变量赋值
namespace Razor.Controllers { public class JSController : Controller { public ActionResult Index() { ...
- C#各种导入Excel文件的数据的方法总结
在导入前都需要将上传的文件保存到服务器,所以避免重复的写这些代码,先贴出上传文件并保存到服务器指定路径的代码 protected void btnImport_Click(object sender, ...
- hdu1316
链接:pid=1316" target="_blank">点击打开链接 题意:问区间[a,b]中有多少斐波那契数 代码: #include <iostream ...
- 织梦在广告(myad)中使用css样式
使用单引号,以及只有style这一个属性
- java中url正则regex匹配
String regex = "^(?:https?://)?[\\w]{1,}(?:\\.?[\\w]{1,})+[\\w-_/?&=#%:]*$"; 解释说明: ^ : ...