- 浏览: 778282 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
萨琳娜啊:
Java读源码之Netty深入剖析网盘地址:https://p ...
Netty源码学习-FileRegion -
飞天奔月:
写得有趣 ^_^
那一年你定义了一个接口 -
GoldRoger:
第二个方法很好
java-判断一个自然数是否是某个数的平方。当然不能使用开方运算 -
bylijinnan:
<script>alert("close ...
自己动手实现Java Validation -
paul920531:
39行有个bug:"int j=new Random ...
java-蓄水池抽样-要求从N个元素中随机的抽取k个元素,其中N无法确定
今天看org.jboss.netty.example.http.file.HttpStaticFileServerHandler.java
可以直接往channel里面写入一个FileRegion对象,而不需要相应的encoder:
//pipeline(没有诸如“FileRegionEncoder”的handler): public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = pipeline(); pipeline.addLast("decoder", new HttpRequestDecoder()); pipeline.addLast("aggregator", new HttpChunkAggregator(65536)); pipeline.addLast("encoder", new HttpResponseEncoder()); pipeline.addLast("chunkedWriter", new ChunkedWriteHandler()); pipeline.addLast("handler", new HttpStaticFileServerHandler()); return pipeline; } public class HttpStaticFileServerHandler extends SimpleChannelUpstreamHandler { public void messageReceived...{ RandomAccessFile raf = new RandomAccessFile(file, "r"); long fileLength = raf.length(); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); setContentLength(response, fileLength); setContentTypeHeader(response, file); setDateAndCacheHeaders(response, file); Channel ch = e.getChannel(); // Write the initial line and the header. ch.write(response); // Write the content. // No encryption - use zero-copy. final FileRegion region = new DefaultFileRegion(raf.getChannel(), 0, fileLength); //直接写入FileRegion ch.write(region); } }
这是为什么?往channel里面写的数据最后不是都要转成ChannelBuffer吗?
我们一步步的分析:
ch.write(region)会触发downstream事件(把region“装入”MessageEvent),
会一路经过各个handler,最后去到“sink”:
//我们以NioServerSocketPipelineSink为例: private static void handleAcceptedSocket(ChannelEvent e) { if (e instanceof MessageEvent) { MessageEvent event = (MessageEvent) e; NioSocketChannel channel = (NioSocketChannel) event.getChannel(); boolean offered = channel.writeBufferQueue.offer(event); assert offered; channel.worker.writeFromUserCode(channel); } } //最终的写操作在AbstractNioWorker(只保留关键代码): protected void write0(AbstractNioChannel<?> channel) { final WritableByteChannel ch = channel.channel; final Queue<MessageEvent> writeBuffer = channel.writeBufferQueue; channel.currentWriteEvent = evt = writeBuffer.poll(); /*关键在这里:把FileRegion封装成一个SendBuffer, SendBuffer的transferTo调用的是FileRegion的transferTo方法, 而这个方法调用的是FileChannel的transferTo方法: This method is potentially much more efficient than a simple loop that reads from this channel and writes to the target channel. Many operating systems can transfer bytes directly from the filesystem cache to the target channel without actually copying them. 大体意思就是“Java NIO Channel to Channel Transfers”不需要内存复制,速度更快 */ channel.currentWriteBuffer = buf = sendBufferPool.acquire(evt.getMessage()); buf.transferTo(ch); } //这里证明了,往channel写入的数据,类型既可以是ChannelBuffer,也可以是FileRegion SendBuffer acquire(Object message) { if (message instanceof ChannelBuffer) { return acquire((ChannelBuffer) message); } else if (message instanceof FileRegion) { return acquire((FileRegion) message); } throw new IllegalArgumentException( "unsupported message type: " + message.getClass()); } private SendBuffer acquire(FileRegion src) { if (src.getCount() == 0) { return EMPTY_BUFFER; } return new FileSendBuffer(src); } class FileSendBuffer { private final FileRegion file; public long transferTo(WritableByteChannel ch) throws IOException { long localWrittenBytes = file.transferTo(ch, writtenBytes); writtenBytes += localWrittenBytes; return localWrittenBytes; } } class DefaultFileRegion...{ private final FileChannel file; public long transferTo(WritableByteChannel target, long position) throws IOException { return file.transferTo(this.position + position, count, target); } }
最后,记录一下java NIO对大文件的读写方法:
java.nio.channels.FileChannel的map方法可以把FileChannel“包装”成MappedByteBuffer:
public abstract MappedByteBuffer map(FileChannel.MapMode mode, long position, long size)
对于大文件,转成MappedByteBuffer再读写,速度更快
举例:
public class ReadingHugeFilesUsingMemoryMappedBuffer { /** * use a MappedByteBuffer to wrap a huge file. Using a MappedByteBuffer does * not load the file in JVM but reads it directly off the file system * memory. The file can be opened in read, write or private mode. */ // to test you can use any video movie file if you dont have any other large // file for testing. private static String hugeFile = "A Huge File"; public static void main(String[] args) throws IOException { File file = new File(hugeFile); FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel(); MappedByteBuffer buffer = fileChannel.map( FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); // the buffer now reads the file as if it were loaded in memory. note // that for smaller files it would be faster // to just load the file in memory // lets see if this buffer is loaded fully into memory System.out.println(buffer.isLoaded()); // the mappedbytebuffer can be used as a normal buffer to do read and/or // write operations // read the size System.out.println(buffer.capacity()); } }
评论
1 楼
萨琳娜啊
2018-07-10
Java读源码之Netty深入剖析
网盘地址:https://pan.baidu.com/s/1pdLNtJGkOSd1fGBT_chAqA 密码: 2kfw
备用地址(腾讯微云):https://share.weiyun.com/5Bs3HcR 密码:uu95be
JavaCoder如果没有研究过Netty,那么你对Java语言的使用和理解仅仅停留在表面水平,如果你要进阶,想了解Java服务器的深层高阶知识,Netty绝对是一个必须要过的门槛。
本课程带你从一个Socket例子入手,一步步深入探究Netty各个模块的源码,深入剖析Netty的工作流程和源码设计,让你不但“真懂”也要“会用”
网盘地址:https://pan.baidu.com/s/1pdLNtJGkOSd1fGBT_chAqA 密码: 2kfw
备用地址(腾讯微云):https://share.weiyun.com/5Bs3HcR 密码:uu95be
JavaCoder如果没有研究过Netty,那么你对Java语言的使用和理解仅仅停留在表面水平,如果你要进阶,想了解Java服务器的深层高阶知识,Netty绝对是一个必须要过的门槛。
本课程带你从一个Socket例子入手,一步步深入探究Netty各个模块的源码,深入剖析Netty的工作流程和源码设计,让你不但“真懂”也要“会用”
发表评论
-
TCP的TIME-WAIT
2014-04-23 16:35 1168原文连接:http://vincent.bernat.im/e ... -
《TCPIP详解卷1》学习-拥塞避免
2014-01-15 15:16 159拥塞避免算法、 ... -
Netty源码学习-HTTP-tunnel
2014-01-14 18:19 4252Netty关于HTTP tunnel的说明: http://d ... -
Netty源码学习-HttpChunkAggregator-HttpRequestEncoder-HttpResponseDecoder
2013-12-27 16:10 4004今天看Netty如何实现一个Http Server org.j ... -
Netty源码学习-ReadTimeoutHandler
2013-12-26 17:53 3769ReadTimeoutHandler的实现思 ... -
Netty学习笔记
2013-12-25 18:39 1457本文是阅读以下两篇文章时: http://seeallhear ... -
Netty源码学习-ChannelHandler
2013-12-25 18:12 1593一般来说,“有状态”的ChannelHandler不应 ... -
Netty源码学习-ServerBootstrap启动及事件处理过程
2013-12-19 20:11 10695Netty是采用了Reactor模式的多线程版本,建议先看下面 ... -
Netty源码学习-Java-NIO-Reactor
2013-12-19 18:21 4852Netty里面采用了NIO-based Reactor Pat ... -
Netty源码学习-ReplayingDecoder
2013-12-13 20:21 4225ReplayingDecoder是FrameDecoder的子 ... -
Netty源码学习-DefaultChannelPipeline2
2013-12-11 15:47 1248Netty3的API http://docs.jboss.or ... -
Netty源码学习-CompositeChannelBuffer
2013-12-06 15:54 2727CompositeChannelBuffer体现了Netty的 ... -
Netty源码学习-DelimiterBasedFrameDecoder
2013-12-05 18:36 9485看DelimiterBasedFrameDecoder的AP ... -
Netty源码学习-ObjectEncoder和ObjectDecoder
2013-12-05 16:06 4952Netty中传递对象的思路很直观: Netty中数据的传递是基 ... -
Netty源码学习-LengthFieldBasedFrameDecoder
2013-12-05 15:20 7241先看看LengthFieldBasedFrameDecoder ... -
Netty源码学习-FrameDecoder
2013-11-28 18:38 3869Netty 3.x的user guide里FrameDecod ... -
Netty源码学习-DefaultChannelPipeline
2013-11-27 17:00 2185package com.ljn.channel; /** ...
相关推荐
netty-buffer-4.1.32.Final-sources.jar netty-buffer-4.1.32.Final.jar netty-build-22-sources.jar netty-build-22.jar netty-codec-4.1.32.Final-sources.jar netty-codec-4.1.32.Final.jar netty-codec-...
赠送jar包:netty-codec-mqtt-4.1.73.Final.jar; 赠送原API文档:netty-codec-mqtt-4.1.73.Final-javadoc.jar; 赠送源代码:netty-codec-mqtt-4.1.73.Final-sources.jar; 赠送Maven依赖信息文件:netty-codec-...
赠送jar包:netty-transport-native-unix-common-4.1.73.Final.jar; 赠送原API文档:netty-transport-native-unix-common-4.1.73.Final-javadoc.jar; 赠送源代码:netty-transport-native-unix-common-4.1.73....
赠送jar包:netty-codec-dns-4.1.65.Final.jar; 赠送原API文档:netty-codec-dns-4.1.65.Final-javadoc.jar; 赠送源代码:netty-codec-dns-4.1.65.Final-sources.jar; 赠送Maven依赖信息文件:netty-codec-dns-...
赠送jar包:netty-codec-haproxy-4.1.73.Final.jar; 赠送原API文档:netty-codec-haproxy-4.1.73.Final-javadoc.jar; 赠送源代码:netty-codec-haproxy-4.1.73.Final-sources.jar; 赠送Maven依赖信息文件:netty-...
赠送jar包:netty-transport-rxtx-4.1.74.Final.jar; 赠送原API文档:netty-transport-rxtx-4.1.74.Final-javadoc.jar; 赠送源代码:netty-transport-rxtx-4.1.74.Final-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:netty-resolver-dns-4.1.65.Final.jar; 赠送原API文档:netty-resolver-dns-4.1.65.Final-javadoc.jar; 赠送源代码:netty-resolver-dns-4.1.65.Final-sources.jar; 赠送Maven依赖信息文件:netty-...
赠送jar包:netty-transport-classes-epoll-4.1.73.Final.jar; 赠送原API文档:netty-transport-classes-epoll-4.1.73.Final-javadoc.jar; 赠送源代码:netty-transport-classes-epoll-4.1.73.Final-sources.jar;...
赠送jar包:netty-codec-http-4.1.27.Final.jar; 赠送原API文档:netty-codec-http-4.1.27.Final-javadoc.jar; 赠送源代码:netty-codec-http-4.1.27.Final-sources.jar; 赠送Maven依赖信息文件:netty-codec-...
赠送jar包:netty-tcnative-classes-2.0.46.Final.jar; 赠送原API文档:netty-tcnative-classes-2.0.46.Final-javadoc.jar; 赠送源代码:netty-tcnative-classes-2.0.46.Final-sources.jar; 赠送Maven依赖信息...
赠送jar包:netty-codec-http-4.1.11.Final.jar; 赠送原API文档:netty-codec-http-4.1.11.Final-javadoc.jar; 赠送源代码:netty-codec-http-4.1.11.Final-sources.jar; 赠送Maven依赖信息文件:netty-codec-...
赠送jar包:netty-codec-http2-4.1.74.Final.jar; 赠送原API文档:netty-codec-http2-4.1.74.Final-javadoc.jar; 赠送源代码:netty-codec-http2-4.1.74.Final-sources.jar; 赠送Maven依赖信息文件:netty-codec-...
赠送jar包:netty-transport-classes-epoll-4.1.74.Final.jar; 赠送原API文档:netty-transport-classes-epoll-4.1.74.Final-javadoc.jar; 赠送源代码:netty-transport-classes-epoll-4.1.74.Final-sources.jar;...
赠送jar包:netty-transport-native-unix-common-4.1.73.Final.jar; 赠送原API文档:netty-transport-native-unix-common-4.1.73.Final-javadoc.jar; 赠送源代码:netty-transport-native-unix-common-4.1.73....
赠送jar包:netty-buffer-4.1.68.Final.jar; 赠送原API文档:netty-buffer-4.1.68.Final-javadoc.jar; 赠送源代码:netty-buffer-4.1.68.Final-sources.jar; 赠送Maven依赖信息文件:netty-buffer-4.1.68.Final....
赠送jar包:netty-common-4.1.65.Final.jar; 赠送原API文档:netty-common-4.1.65.Final-javadoc.jar; 赠送源代码:netty-common-4.1.65.Final-sources.jar; 赠送Maven依赖信息文件:netty-common-4.1.65.Final....
赠送jar包:reactor-netty-core-1.0.15.jar; 赠送原API文档:reactor-netty-core-1.0.15-javadoc.jar; 赠送源代码:reactor-netty-core-1.0.15-sources.jar; 赠送Maven依赖信息文件:reactor-netty-core-1.0.15....
赠送jar包:netty-codec-stomp-4.1.73.Final.jar; 赠送原API文档:netty-codec-stomp-4.1.73.Final-javadoc.jar; 赠送源代码:netty-codec-stomp-4.1.73.Final-sources.jar; 赠送Maven依赖信息文件:netty-codec-...
赠送jar包:netty-codec-http-4.1.73.Final.jar; 赠送原API文档:netty-codec-http-4.1.73.Final-javadoc.jar; 赠送源代码:netty-codec-http-4.1.73.Final-sources.jar; 赠送Maven依赖信息文件:netty-codec-...
赠送jar包:netty-resolver-dns-4.1.74.Final.jar; 赠送原API文档:netty-resolver-dns-4.1.74.Final-javadoc.jar; 赠送源代码:netty-resolver-dns-4.1.74.Final-sources.jar; 赠送Maven依赖信息文件:netty-...