通道表示打开到 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)的原理获取的更多相关文章

  1. NIO之通道(Channel)的原理与获取以及数据传输与内存映射文件

    通道(Channel) 由java.nio.channels包定义的,Channel表示IO源与目标打开的连接,Channel类似于传统的“流”,只不过Channel本身不能直接访问数据,Channe ...

  2. Java-NIO(四):通道(Channel)的原理与获取

    通道(Channel): 由java.nio.channels包定义的,Channel表示IO源与目标打开的连接,Channel类似于传统的“流”,只不过Channel本身不能直接访问数据,Chann ...

  3. 通道(Channel)的原理与获取

    通道(Channel):由 java.nio.channels 包定义 的.Channel 表示 IO 源与目标打开的连接. Channel 类似于传统的“流”.只不过 Channel 本身不能直接访 ...

  4. Java NIO中的通道Channel(一)通道基础

    什么是通道Channel 这个说实话挺难定义的,有点抽象,不过我们可以根据它的用途来理解: 通道主要用于传输数据,从缓冲区的一侧传到另一侧的实体(如文件.套接字...),反之亦然: 通道是访问IO服务 ...

  5. go中的数据结构通道-channel

    1. channel的使用 很多文章介绍channel的时候都和并发揉在一起,这里我想把它当做一种数据结构来单独介绍它的实现原理. channel,通道.golang中用于数据传递的一种数据结构.是g ...

  6. 详解 通道 (Channel 接口)

    在本篇博文中,本人主要讲解NIO 的两个核心点 -- 缓冲区(Buffer) 和 通道 (Channel)之一的 缓冲区(Buffer), 有关NIO流的其他知识点请观看本人博文<详解 NIO流 ...

  7. nio再学习之通道channel

    通道(Channel):用于在数据传输过程中,进行输入输出的通道,其与(流)Stream不一样,流是单向的,在BIO中我们分为输入流,输出流,但是在通道中其又具有读的功能也具有写的功能或者两者同时进行 ...

  8. 理解CNN中的通道 channel

    在深度学习的算法学习中,都会提到 channels 这个概念.在一般的深度学习框架的 conv2d 中,如 tensorflow .mxnet ,channels 都是必填的一个参数. channel ...

  9. 33、[源码]-AOP原理-获取拦截器链-MethodInterceptor

    33.[源码]-AOP原理-获取拦截器链-MethodInterceptor

随机推荐

  1. Linux下快速安装Mysql及使用

    1.安装 查看有没有安装过: yum list installed mysql* rpm -qa | grep mysql* 查看有没有安装包: yum list mysql* 安装mysql客户端: ...

  2. java把一个文件的内容复制到另外一个文件

    /** * java把一个文件的内容复制到另外一个文件 */import java.io.File;import java.io.FileInputStream;import java.io.File ...

  3. Vue避免 v-if 和 v-for 用在一起

    永远不要把 v-if 和 v-for 同时用在同一个元素上. 一般我们在两种常见的情况下会倾向于这样做: 为了过滤一个列表中的项目 (比如 v-for="user in users" ...

  4. mysql主从只同步部分库或表

    同步部分数据有两个思路,1.master只发送需要的:2.slave只接收想要的. master端: binlog-do-db      二进制日志记录的数据库(多数据库用逗号,隔开)binlog-i ...

  5. MySQL -进阶

    一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用 SELECT * FROM(SELE ...

  6. 字符串== equals

    经常碰到比较字符串的题, eg: public class StringDemo{ private static final String MESSAGE = "taobao"; ...

  7. slam cartographer 学习

    https://github.com/slam4code                   感谢大牛的分享

  8. IOS中公布应用程序,进度条一直不走怎么处理

    在IOS中公布应用程序非常是喜闻乐见. 近期1周.我更新了6次版本号.可是时不时的会卡住,进度条不走. 最后总结了几个原因. 1.在公布前你要确认自己的证书是否配置正确 2.DNS域名server有没 ...

  9. php减少损耗的方法之一 缓存对象

    即把实例后的对象缓存起来(存入变量),当需要再次实例化时,先去缓存里查看是否存在.存在则返回.否则实例化.

  10. 深入Asyncio(十)异步解析式

    Async Comprehensions 目前已经学会了如何在Python中进行异步迭代,接下来的问题是这是否适用于解析式?答案是OJBK!该支持在PEP 530中提及,建议去读一下. >> ...