- 浏览: 1000291 次
-
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
ServerSocketChannel定义:http://donald-draper.iteye.com/blog/2369836
ServerSocketChannelImpl解析:http://donald-draper.iteye.com/blog/2370912
SocketChannelImpl 解析一(通道连接,发送数据):http://donald-draper.iteye.com/blog/2372364
SocketChannelImpl 解析二(发送数据后续):http://donald-draper.iteye.com/blog/2372548
SocketChannelImpl 解析三(接收数据):http://donald-draper.iteye.com/blog/2372590
SocketChannelImpl 解析四(关闭通道等) :http://donald-draper.iteye.com/blog/2372717
Pipe定义:http://donald-draper.iteye.com/blog/2373540
引言:
Pipe中包含一个可写通道SinkChannel和一个可读通道SourceChannel。sink向管道写字节序序列,
source从管道读取字节序列。
我们从Pipe的open方法开始:
这里为什么是SelectorProviderImpl,前面已经说过不在说,
//SelectorProviderImpl
下面来看通道的实现,PipeImpl
从上面可以看出PipeImpl,内部有一个Source通道SourceChannel,Sink通道SinkChannel,一个
随机数rnd(long),还有一个管道初始化Action,初始化时加载net和nio资源库,委托IOUtil产生8个字节,然后根据8个字节生成一个随机数rnd;在构造时,在与当前线程访问控制权限的情况下,执行Initializer,权限动作,执行Initializer的run方法,即通过ServerSocketChannle和SocketChannel建立一个通道连接;首先新建一个ServerSocketChannle和SocketChannel,分别绑定地址SocketChannel向ServerSocetChannel发送随机数rnd,ServerSocetChannel接受SocketChannel连接,产生一个SocketChannel1(server),SocketChannel1接受client(SocketChannel),检验与随机数rnd,相等则建立连接。然后根据SocketChannel1(server),构造Sink通道SinkChannelImpl,根据client(SocketChannel),构造Source通道SourceChannelImpl。
我们先来看SinkChannelImpl
从SinkChannelImpl,可以看出内部关联一个socket通道,SinkChannelImpl关闭通道,配置通道阻塞模式,写字节序列到管道都是委托给内部的SocketChannle。
再看SourceChannelImpl
从SourceChannelImpl,可以看出内部关联一个socket通道,SourceChannelImpl关闭通道,配置通道阻塞模式,从管道读取字节序列都是委托给内部的SocketChannle。
总结:
PipeImpl,内部有一个Source通道SourceChannel,Sink通道SinkChannel,一个随机数rnd(long),还有一个管道初始化Action,初始化时加载net和nio资源库,委托IOUtil产生8个字节,然后根据8个字节生成一个随机数rnd;在构造时,在与当前线程访问控制权限的情况下,执行Initializer,权限动作,执行Initializer的run方法,即通过ServerSocketChannle和SocketChannel建立一个通道连接;首先新建一个ServerSocketChannle和SocketChannel,分别绑定地址SocketChannel向ServerSocetChannel发送随机数rnd,ServerSocetChannel接受SocketChannel连接,产生一个SocketChannel1(server),SocketChannel1接受client(SocketChannel),检验与随机数rnd,相等则建立连接。然后根据SocketChannel1(server),构造Sink通道SinkChannelImpl,根据client(SocketChannel),构造Source通道SourceChannelImpl。
SinkChannelImpl,内部关联一个socket通道,SinkChannelImpl关闭通道,配置通道阻塞模式,写字节序列到管道都是委托给内部的SocketChannle。
SourceChannelImpl,内部关联一个socket通道,SourceChannelImpl关闭通道,配置通道阻塞模式,从管道读取字节序列都是委托给内部的SocketChannle。
ServerSocketChannelImpl解析:http://donald-draper.iteye.com/blog/2370912
SocketChannelImpl 解析一(通道连接,发送数据):http://donald-draper.iteye.com/blog/2372364
SocketChannelImpl 解析二(发送数据后续):http://donald-draper.iteye.com/blog/2372548
SocketChannelImpl 解析三(接收数据):http://donald-draper.iteye.com/blog/2372590
SocketChannelImpl 解析四(关闭通道等) :http://donald-draper.iteye.com/blog/2372717
Pipe定义:http://donald-draper.iteye.com/blog/2373540
引言:
Pipe中包含一个可写通道SinkChannel和一个可读通道SourceChannel。sink向管道写字节序序列,
source从管道读取字节序列。
我们从Pipe的open方法开始:
public static Pipe open() throws IOException { return SelectorProvider.provider().openPipe(); }
这里为什么是SelectorProviderImpl,前面已经说过不在说,
//SelectorProviderImpl
public Pipe openPipe() throws IOException { return new PipeImpl(this); }
下面来看通道的实现,PipeImpl
package sun.nio.ch; import java.io.IOException; import java.net.*; import java.nio.ByteBuffer; import java.nio.channels.*; import java.nio.channels.spi.SelectorProvider; import java.security.*; import java.util.Random; // Referenced classes of package sun.nio.ch: // IOUtil, Util, SinkChannelImpl, SourceChannelImpl class PipeImpl extends Pipe { private java.nio.channels.Pipe.SourceChannel source;//Source通道 private java.nio.channels.Pipe.SinkChannel sink;//Sink通道 private static final Random rnd;// static { //加载net和nio资源库 Util.load(); byte abyte0[] = new byte[8]; //委托IOUtil,获取8个字节序列,static native boolean randomBytes(byte abyte0[]); boolean flag = IOUtil.randomBytes(abyte0); if(flag) rnd = new Random(ByteBuffer.wrap(abyte0).getLong()); else rnd = new Random(); } PipeImpl(SelectorProvider selectorprovider) throws IOException { try { //在与当前线程访问控制权限的情况下,执行Initializer,权限动作,执行Initializer的run方法 AccessController.doPrivileged(new Initializer(selectorprovider)); } catch(PrivilegedActionException privilegedactionexception) { throw (IOException)privilegedactionexception.getCause(); } } //管道初始化Action private class Initializer implements PrivilegedExceptionAction { private final SelectorProvider sp; static final boolean $assertionsDisabled = !sun/nio/ch/PipeImpl.desiredAssertionStatus(); final PipeImpl this$0; private Initializer(SelectorProvider selectorprovider) { this$0 = PipeImpl.this; super(); sp = selectorprovider; } public volatile Object run() throws Exception { return run(); } public Void run() throws IOException { ServerSocketChannel serversocketchannel;//ServerSocket通道, SocketChannel socketchannel;//用于source通道 SocketChannel socketchannel1;//用于Sink通道 serversocketchannel = null; socketchannel = null; socketchannel1 = null; try { //获取本地地址 InetAddress inetaddress = InetAddress.getByName("127.0.0.1"); if(!$assertionsDisabled && !inetaddress.isLoopbackAddress()) throw new AssertionError(); //打开一个ServerSocket通道 serversocketchannel = ServerSocketChannel.open(); //ServerSocket通道绑定地址 serversocketchannel.socket().bind(new InetSocketAddress(inetaddress, 0)); InetSocketAddress inetsocketaddress = new InetSocketAddress(inetaddress, serversocketchannel.socket().getLocalPort()); //打开一个SocketChannel通道 socketchannel = SocketChannel.open(inetsocketaddress); ByteBuffer bytebuffer = ByteBuffer.allocate(8); //获取通道的随机long值 long l = PipeImpl.rnd.nextLong(); bytebuffer.putLong(l).flip(); //向serverSocket通道发送一个long值,即8个字节 socketchannel.write(bytebuffer); do { //serverSocket接受连接 socketchannel1 = serversocketchannel.accept(); bytebuffer.clear(); //接受client通道端发送过来的数据 socketchannel1.read(bytebuffer); bytebuffer.rewind(); if(bytebuffer.getLong() == l) break; socketchannel1.close(); } while(true); //根据client通道,构造SourceChannelImpl source = new SourceChannelImpl(sp, socketchannel); //根据ServerChannel接受连接产生的SocketChannel通道,构造SinkChannelImpl sink = new SinkChannelImpl(sp, socketchannel1); } catch(IOException ioexception1) { try { if(socketchannel != null) socketchannel.close(); if(socketchannel1 != null) socketchannel1.close(); } catch(IOException ioexception2) { } IOException ioexception3 = new IOException("Unable to establish loopback connection"); ioexception3.initCause(ioexception1); throw ioexception3; } try { //关闭serverSocketChannle,任务完成(建立一个SocketChannle连接) if(serversocketchannel != null) serversocketchannel.close(); } catch(IOException ioexception) { } break MISSING_BLOCK_LABEL_277; Exception exception; exception; try { if(serversocketchannel != null) serversocketchannel.close(); } catch(IOException ioexception4) { } throw exception; return null; } } //返回source通道 public java.nio.channels.Pipe.SourceChannel source() { return source; } //返回sink通道 public java.nio.channels.Pipe.SinkChannel sink() { return sink; } }
从上面可以看出PipeImpl,内部有一个Source通道SourceChannel,Sink通道SinkChannel,一个
随机数rnd(long),还有一个管道初始化Action,初始化时加载net和nio资源库,委托IOUtil产生8个字节,然后根据8个字节生成一个随机数rnd;在构造时,在与当前线程访问控制权限的情况下,执行Initializer,权限动作,执行Initializer的run方法,即通过ServerSocketChannle和SocketChannel建立一个通道连接;首先新建一个ServerSocketChannle和SocketChannel,分别绑定地址SocketChannel向ServerSocetChannel发送随机数rnd,ServerSocetChannel接受SocketChannel连接,产生一个SocketChannel1(server),SocketChannel1接受client(SocketChannel),检验与随机数rnd,相等则建立连接。然后根据SocketChannel1(server),构造Sink通道SinkChannelImpl,根据client(SocketChannel),构造Source通道SourceChannelImpl。
我们先来看SinkChannelImpl
package sun.nio.ch; import java.io.FileDescriptor; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.*; import java.nio.channels.spi.SelectorProvider; // Referenced classes of package sun.nio.ch: // SelChImpl, SelectionKeyImpl, SelectorImpl, SocketChannelImpl, // Util class SinkChannelImpl extends java.nio.channels.Pipe.SinkChannel implements SelChImpl { SocketChannel sc;//关联socket通道 public FileDescriptor getFD() { return ((SocketChannelImpl)sc).getFD(); } public int getFDVal() { return ((SocketChannelImpl)sc).getFDVal(); } SinkChannelImpl(SelectorProvider selectorprovider, SocketChannel socketchannel) { super(selectorprovider); sc = socketchannel; } //关闭通道 protected void implCloseSelectableChannel() throws IOException { //通道没有注册到任何选择器 if(!isRegistered()) kill(); } //关闭socket通道 public void kill() throws IOException { sc.close(); } //配置阻塞模式 protected void implConfigureBlocking(boolean flag) throws IOException { sc.configureBlocking(flag); } //写字节序列 public int write(ByteBuffer bytebuffer) throws IOException { return sc.write(bytebuffer); AsynchronousCloseException asynchronouscloseexception; asynchronouscloseexception; close(); throw asynchronouscloseexception; } public long write(ByteBuffer abytebuffer[]) throws IOException { return sc.write(abytebuffer); AsynchronousCloseException asynchronouscloseexception; asynchronouscloseexception; close(); throw asynchronouscloseexception; } public long write(ByteBuffer abytebuffer[], int i, int j) throws IOException { if(i < 0 || j < 0 || i > abytebuffer.length - j) throw new IndexOutOfBoundsException(); return write(Util.subsequence(abytebuffer, i, j)); AsynchronousCloseException asynchronouscloseexception; asynchronouscloseexception; close(); throw asynchronouscloseexception; } //设置就绪操作事件 public boolean translateAndSetReadyOps(int i, SelectionKeyImpl selectionkeyimpl) { return translateReadyOps(i, 0, selectionkeyimpl); } //更新就绪操作事件 public boolean translateAndUpdateReadyOps(int i, SelectionKeyImpl selectionkeyimpl) { return translateReadyOps(i, selectionkeyimpl.nioReadyOps(), selectionkeyimpl); } public boolean translateReadyOps(int i, int j, SelectionKeyImpl selectionkeyimpl) { int k = selectionkeyimpl.nioInterestOps(); int l = selectionkeyimpl.nioReadyOps(); int i1 = j; //就绪事件为读1写4连接8,接受连接事件16,不是这四种事件,则抛出Error if((i & 32) != 0) throw new Error("POLLNVAL detected"); //为8+16,接受连接,并建立连接,设置就绪事件k if((i & 24) != 0) { i1 = k; selectionkeyimpl.nioReadyOps(i1); return (i1 & ~l) != 0; } if((i & 4) != 0 && (k & 4) != 0) i1 |= 4;//写操作 selectionkeyimpl.nioReadyOps(i1); return (i1 & ~l) != 0; } //设置兴趣操作事件 public void translateAndSetInterestOps(int i, SelectionKeyImpl selectionkeyimpl) { if((i & 4) != 0) i = 4;//写事件 selectionkeyimpl.selector.putEventOps(selectionkeyimpl, i); } }
从SinkChannelImpl,可以看出内部关联一个socket通道,SinkChannelImpl关闭通道,配置通道阻塞模式,写字节序列到管道都是委托给内部的SocketChannle。
再看SourceChannelImpl
class SourceChannelImpl extends java.nio.channels.Pipe.SourceChannel implements SelChImpl { SocketChannel sc; public FileDescriptor getFD() { return ((SocketChannelImpl)sc).getFD(); } public int getFDVal() { return ((SocketChannelImpl)sc).getFDVal(); } SourceChannelImpl(SelectorProvider selectorprovider, SocketChannel socketchannel) { super(selectorprovider); sc = socketchannel; } //关闭通道 protected void implCloseSelectableChannel() throws IOException { //通道没有注册到任何选择器 if(!isRegistered()) kill(); } //关闭socket通道 public void kill() throws IOException { sc.close(); } //配置阻塞模式 protected void implConfigureBlocking(boolean flag) throws IOException { sc.configureBlocking(flag); } //读取字节序列 public int read(ByteBuffer bytebuffer) throws IOException { return sc.read(bytebuffer); AsynchronousCloseException asynchronouscloseexception; asynchronouscloseexception; close(); throw asynchronouscloseexception; } public long read(ByteBuffer abytebuffer[], int i, int j) throws IOException { if(i < 0 || j < 0 || i > abytebuffer.length - j) throw new IndexOutOfBoundsException(); return read(Util.subsequence(abytebuffer, i, j)); AsynchronousCloseException asynchronouscloseexception; asynchronouscloseexception; close(); throw asynchronouscloseexception; } public long read(ByteBuffer abytebuffer[]) throws IOException { return sc.read(abytebuffer); AsynchronousCloseException asynchronouscloseexception; asynchronouscloseexception; close(); throw asynchronouscloseexception; } //设置就绪操作事件 public boolean translateAndSetReadyOps(int i, SelectionKeyImpl selectionkeyimpl) { return translateReadyOps(i, 0, selectionkeyimpl); } //更新就绪操作事件 public boolean translateAndUpdateReadyOps(int i, SelectionKeyImpl selectionkeyimpl) { return translateReadyOps(i, selectionkeyimpl.nioReadyOps(), selectionkeyimpl); } public boolean translateReadyOps(int i, int j, SelectionKeyImpl selectionkeyimpl) { int k = selectionkeyimpl.nioInterestOps(); int l = selectionkeyimpl.nioReadyOps(); int i1 = j; //就绪事件为读1写4连接8,接受连接事件16,不是这四种事件,则抛出Error if((i & 32) != 0) throw new Error("POLLNVAL detected"); //为8+16,接受连接,并建立连接,设置就绪事件k if((i & 24) != 0) { i1 = k; selectionkeyimpl.nioReadyOps(i1); return (i1 & ~l) != 0; } if((i & 1) != 0 && (k & 1) != 0) i1 |= 1;//读事件 selectionkeyimpl.nioReadyOps(i1); return (i1 & ~l) != 0; } //设置兴趣操作事件 public void translateAndSetInterestOps(int i, SelectionKeyImpl selectionkeyimpl) { if((i & 1) != 0) i = 1;//读事件 selectionkeyimpl.selector.putEventOps(selectionkeyimpl, i); } }
从SourceChannelImpl,可以看出内部关联一个socket通道,SourceChannelImpl关闭通道,配置通道阻塞模式,从管道读取字节序列都是委托给内部的SocketChannle。
总结:
PipeImpl,内部有一个Source通道SourceChannel,Sink通道SinkChannel,一个随机数rnd(long),还有一个管道初始化Action,初始化时加载net和nio资源库,委托IOUtil产生8个字节,然后根据8个字节生成一个随机数rnd;在构造时,在与当前线程访问控制权限的情况下,执行Initializer,权限动作,执行Initializer的run方法,即通过ServerSocketChannle和SocketChannel建立一个通道连接;首先新建一个ServerSocketChannle和SocketChannel,分别绑定地址SocketChannel向ServerSocetChannel发送随机数rnd,ServerSocetChannel接受SocketChannel连接,产生一个SocketChannel1(server),SocketChannel1接受client(SocketChannel),检验与随机数rnd,相等则建立连接。然后根据SocketChannel1(server),构造Sink通道SinkChannelImpl,根据client(SocketChannel),构造Source通道SourceChannelImpl。
SinkChannelImpl,内部关联一个socket通道,SinkChannelImpl关闭通道,配置通道阻塞模式,写字节序列到管道都是委托给内部的SocketChannle。
SourceChannelImpl,内部关联一个socket通道,SourceChannelImpl关闭通道,配置通道阻塞模式,从管道读取字节序列都是委托给内部的SocketChannle。
发表评论
-
文件通道解析二(文件锁,关闭通道)
2017-05-16 23:17 1109文件通道解析一(读写操作,通道数据传输等):http://do ... -
文件通道解析一(读写操作,通道数据传输等)
2017-05-16 10:04 1206Reference定义(PhantomRefere ... -
文件通道创建方式综述
2017-05-15 17:39 1110Reference定义(PhantomReference,Cl ... -
文件读写方式简单综述后续(文件,流构造)
2017-05-14 23:04 1525Java Socket通信实例:http://donald-d ... -
文件读写方式简单综述
2017-05-14 11:13 1169Java Socket通信实例:http://donald-d ... -
FileChanne定义
2017-05-12 23:28 991文件读写方式简单综述:http://donald-draper ... -
SeekableByteChannel接口定义
2017-05-11 08:43 1282ByteChannel,分散聚集通道接口的定义(SocketC ... -
FileChannel示例
2017-05-11 08:37 1027前面我们看过socket通道,datagram通道,以管道Pi ... -
Pipe定义
2017-05-10 09:07 958Channel接口定义:http://donald-drape ... -
NIO-Pipe示例
2017-05-10 08:47 951PipeImpl解析:http://donald-draper ... -
DatagramChannelImpl 解析四(地址绑定,关闭通道等)
2017-05-10 08:27 832DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析三(多播)
2017-05-10 08:20 2003DatagramChannelImpl 解析一(初始化):ht ... -
NIO-UDP实例
2017-05-09 12:32 1628DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析二(报文发送与接收)
2017-05-09 09:03 1454DatagramChannelImpl 解析一(初始化):ht ... -
DatagramChannelImpl 解析一(初始化)
2017-05-08 21:52 1472Channel接口定义:http://donald-drape ... -
MembershipKeyImpl 简介
2017-05-08 09:11 965MembershipKey定义:http://donald-d ... -
DatagramChannel定义
2017-05-07 23:13 1268Channel接口定义:http://donald-drape ... -
MulticastChanne接口定义
2017-05-07 13:45 1195NetworkChannel接口定义:ht ... -
MembershipKey定义
2017-05-06 16:20 966package java.nio.channels; i ... -
SocketChannelImpl 解析四(关闭通道等)
2017-05-05 08:38 2599SocketChannelImpl 解析一(通道连接,发送数据 ...
相关推荐
标题中的"PipeImpl.rar"可能是一个包含Java编程中关于实现管道(Pipe)接口源代码的压缩文件,专门针对Unix和Linux操作系统。在这个场景下,我们主要讨论的是在这些类Unix系统中如何用Java来实现I/O管道。 Java编程在...
小姐姐炫酷唯美引导页.zip
scratch少儿编程逻辑思维游戏源码-让它们跳起来.zip
少儿编程scratch项目源代码文件案例素材-战斗塔防.zip
前端开发_Vue全家桶_vw适配_vux组件库_mescroll滚动_fastclick优化_mock模拟_less预处理器_移动端脚手架_基于Vue2和Webpack3构建的移动
数据可视化分析_微信小程序开发_JavaScript_Canvas图表渲染_数据助手工具_仿微信小程序数据助手_支持线性图圆环图柱状图条形图_动态效果适配_商业数据分析_移动端数据
少儿编程scratch项目源代码文件案例素材-章鱼怪.zip
音乐流媒体_API开发_网易云音乐接口封装与数据抓取_提供完整的网易云音乐API调用解决方案_包含歌曲搜索_MP3资源获取_歌词解析_播放列表管理_歌手专辑查询_专辑详情展示_MV
物联网与健康监测_基于RISC-V架构ESP32-C3芯片与MAX30102MAX30205传感器_LVGL图形界面与WiFiBLE双模通信的智能手表系统_实时监测心率血氧体温
wangtengfei-hn_EmployeesExample_23540_1745868671962
Android开发_自定义View绘制动画效果_模仿小米手环App首页计步数据展示_实现可自定义目标步数当前步数颜色字体大小圆点尺寸的动态步数统计图表组件_用于健康运动类App展示
少儿编程scratch项目源代码文件案例素材-钻机机器人.zip
scratch少儿编程逻辑思维游戏源码-狂暴坦克.zip
scratch少儿编程逻辑思维游戏源码-拳击.zip
scratch少儿编程逻辑思维游戏源码-魔方冲刺.zip
少儿编程scratch项目源代码文件案例素材-重返危机.zip
杰奇CMS小说网站系统_php53-73_mysql5-MariaDB10_utf8mb4编码_GBK自动转码_INNODB存储引擎_多模块支持_前后台分离_电脑手机双版适配_
linyuan620_StockAnalysisAssistant_17128_1745866117970
本系统采用的数据库是Mysql,使用SpringBoot框架开发,运行环境使用Tomcat服务器,ECLIPSE 是本系统的开发平台。在设计过程中,充分保证了系统代码的良好可读性、实用性、易扩展性、通用性、便于后期维护、操作方便以及页面简洁等特点。
少儿编程scratch项目源代码文件案例素材-足球踩踏者.zip