当前位置: 首页 > news >正文

洛阳做网站的长沙网站策划

洛阳做网站的,长沙网站策划,网站模板 带数据库,软文营销网站这里是小奏,觉得文章不错可以关注公众号小奏技术 背景 java nio中文件读写不管是普通文件读写,还是基于mmap实现零拷贝,都离不开FileChannel这个类。 随便打开RocketMQ 源码搜索FileChannel 就可以看到使用频率 kafka也是 所以在java中文件读写FileCh…

这里是小奏,觉得文章不错可以关注公众号小奏技术

背景

java nio中文件读写不管是普通文件读写,还是基于mmap实现零拷贝,都离不开FileChannel这个类。

随便打开RocketMQ 源码搜索FileChannel

就可以看到使用频率

kafka也是

所以在java中文件读写FileChannel尤为重用

java文件读写全流程

这里说的仅仅是FileChannel基于堆内存(HeapByteBuffer)的文件读写。

如果是mmap或者堆外内存,可能有些步骤会省略,相当于有一些优化

  1. FileChannel调用read,将HeapByteBuffer拷贝到DirectByteBuffer
  2. JVM在native层使用read系统调用进行文件读取, 这里需要进行上下文切换,从用户态进入内核态
  3. JVM 进程进入虚拟文件系统层,查看文件数据再page cache是否缓存,如果有则直接从page cache读取并返回到DirectByteBuffer
  4. 如果请求文件数据不在page caceh,则进入文件系统。通过块驱动设备进行真正的IO,并进行文件预读,比如读取的文件可能只有1-10,但是会将1-20都读取
  5. 磁盘控制器DMA将磁盘中的数据拷贝到page cache中。这里发生了一次数据拷贝(非CPU拷贝)
  6. CPU将page cache数据拷贝到DirectByteBuffer,因为page cache属于内核空间,JVM进程无法直接寻址。这里是发生第二次数据拷贝
  7. JVM进程从内核态切换回用户态,这里如果使用的是堆内存(HeapByteBuffer),实际还需要将堆外内存DirectByteBuffer拷贝到堆内存(HeapByteBuffer)

FileChannel读写文件(非MMAP)

public static void main(String[] args) {String filename = "小奏技术.txt";String content = "Hello, 小奏技术.";// 写入文件writeFile(filename, content);// 读取文件System.out.println("Reading from file:");readFile(filename);}public static void writeFile(String filename, String content) {// 创建文件对象File file = new File(filename);// 确保文件存在if (!file.exists()) {try {boolean created = file.createNewFile();if (!created) {System.err.println("Unable to create file: " + filename);return;}} catch (Exception e) {System.err.println("An error occurred while creating the file: " + e.getMessage());return;}}// 使用FileChannel写入文件try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");FileChannel fileChannel = randomAccessFile.getChannel()) {ByteBuffer buffer = ByteBuffer.allocate(content.getBytes().length);buffer.put(content.getBytes());buffer.flip(); // 切换到读模式while (buffer.hasRemaining()) {fileChannel.write(buffer);}} catch (Exception e) {System.err.println("An error occurred while writing to the file: " + e.getMessage());}}public static void readFile(String filename) {// 使用FileChannel读取文件try (RandomAccessFile randomAccessFile = new RandomAccessFile(filename, "r");FileChannel fileChannel = randomAccessFile.getChannel()) {ByteBuffer buffer = ByteBuffer.allocate((int) fileChannel.size());while (fileChannel.read(buffer) > 0) {// Do nothing, just read}// 切换到读模式buffer.flip(); /* while (buffer.hasRemaining()) {System.out.print((char) buffer.get());}*/Charset charset = StandardCharsets.UTF_8; String fileContent = charset.decode(buffer).toString();System.out.print(fileContent);} catch (Exception e) {System.err.println("An error occurred while reading the file: " + e.getMessage());}}

这里需要注意的一个细节
我们分配的内存的方式是

ByteBuffer.allocate()

这里我们可以进入看看源码

实际构造的是HeapByteBuffer,也就是JVM的堆内存

如果我们使用

ByteBuffer.allocateDirect()

则构造的是堆外内存DirectByteBuffer

HeapByteBuffer和DirectByteBuffer文件读写区别

我们看看FileChannel read方法

发现IO相关的处理被封装在IOUtil

我们继续看看IOUtilwrite方法

可以看到如果是DirectBuffer则可以直接写

如果是HeapByteBuffer则需要转换为DirectByteBuffer

为什么要在DirectByteBuffer做一层转换

主要是HeapByteBuffer受JVM管理,也就是会受到GC影响

如果在进行native调用的时候发生了GC,会导致HeapByteBuffer的内容出现错误

具体详细的说明可以看看这篇MappedByteBuffer VS FileChannel:从内核层面对比两者的性能差异

讲解的非常清晰

参考

  • MappedByteBuffer VS FileChannel:从内核层面对比两者的性能差异

文章转载自:
http://dinncojellied.zfyr.cn
http://dinncocombinability.zfyr.cn
http://dinncotowards.zfyr.cn
http://dinncoclipping.zfyr.cn
http://dinnconeedlebook.zfyr.cn
http://dinncodiathermize.zfyr.cn
http://dinncosplenetic.zfyr.cn
http://dinncodiplomacy.zfyr.cn
http://dinncocatabolism.zfyr.cn
http://dinncoduring.zfyr.cn
http://dinncomainland.zfyr.cn
http://dinncountold.zfyr.cn
http://dinncomorelia.zfyr.cn
http://dinncocabalism.zfyr.cn
http://dinncoadoptable.zfyr.cn
http://dinncoczechoslovakia.zfyr.cn
http://dinncofra.zfyr.cn
http://dinncosarsenet.zfyr.cn
http://dinncofmi.zfyr.cn
http://dinncoelutriate.zfyr.cn
http://dinncopsychologism.zfyr.cn
http://dinncoheinous.zfyr.cn
http://dinncotepp.zfyr.cn
http://dinncomullein.zfyr.cn
http://dinncovilification.zfyr.cn
http://dinncooestrus.zfyr.cn
http://dinncocurious.zfyr.cn
http://dinncoptolemaic.zfyr.cn
http://dinncosaintess.zfyr.cn
http://dinncoconceivably.zfyr.cn
http://dinncopersonable.zfyr.cn
http://dinncobivalence.zfyr.cn
http://dinncomental.zfyr.cn
http://dinncounalterable.zfyr.cn
http://dinncobaccara.zfyr.cn
http://dinncobowstring.zfyr.cn
http://dinncoriches.zfyr.cn
http://dinncocampion.zfyr.cn
http://dinncoindianization.zfyr.cn
http://dinncoinexplosive.zfyr.cn
http://dinncoautomatic.zfyr.cn
http://dinncodeuteragonist.zfyr.cn
http://dinncoeastside.zfyr.cn
http://dinncoassamese.zfyr.cn
http://dinncosillographer.zfyr.cn
http://dinncodrover.zfyr.cn
http://dinncodealation.zfyr.cn
http://dinncocorvine.zfyr.cn
http://dinncoexplicitly.zfyr.cn
http://dinnconullproc.zfyr.cn
http://dinncoalbigensianism.zfyr.cn
http://dinncopenmanship.zfyr.cn
http://dinncoflaunch.zfyr.cn
http://dinncocodetta.zfyr.cn
http://dinncotoque.zfyr.cn
http://dinncoconsequentiality.zfyr.cn
http://dinncoheedless.zfyr.cn
http://dinncocontrariwise.zfyr.cn
http://dinncorearward.zfyr.cn
http://dinncoigorot.zfyr.cn
http://dinncotriformed.zfyr.cn
http://dinncolewdness.zfyr.cn
http://dinncounshunned.zfyr.cn
http://dinncoliquify.zfyr.cn
http://dinncodeary.zfyr.cn
http://dinncoskikda.zfyr.cn
http://dinncoconductress.zfyr.cn
http://dinncononce.zfyr.cn
http://dinncorespiratory.zfyr.cn
http://dinncodexterous.zfyr.cn
http://dinncoburrawang.zfyr.cn
http://dinncohoofpick.zfyr.cn
http://dinncovespiary.zfyr.cn
http://dinncobimetallic.zfyr.cn
http://dinncosplendidly.zfyr.cn
http://dinncoadjustive.zfyr.cn
http://dinncodowntrend.zfyr.cn
http://dinncoabettal.zfyr.cn
http://dinncorevaccination.zfyr.cn
http://dinncosierozem.zfyr.cn
http://dinncogenially.zfyr.cn
http://dinncomortadella.zfyr.cn
http://dinncosaltillo.zfyr.cn
http://dinncosettled.zfyr.cn
http://dinncovehement.zfyr.cn
http://dinncolightpen.zfyr.cn
http://dinncoengulf.zfyr.cn
http://dinncocataphonic.zfyr.cn
http://dinncocosmogonic.zfyr.cn
http://dinncovizir.zfyr.cn
http://dinncolopstick.zfyr.cn
http://dinncotimberyard.zfyr.cn
http://dinncogpm.zfyr.cn
http://dinncokirov.zfyr.cn
http://dinncomalone.zfyr.cn
http://dinncoaspermous.zfyr.cn
http://dinncopneumatics.zfyr.cn
http://dinncoamos.zfyr.cn
http://dinncopreclassical.zfyr.cn
http://dinncoinosculation.zfyr.cn
http://www.dinnco.com/news/158113.html

相关文章:

  • 移动端网站如何做导出功能吗黑帽seo培训多少钱
  • 石家庄网站制作视频重庆公司seo
  • 城建网seo推广公司价格
  • qq炫舞做浴缸的网站手机免费建站系统
  • 贺州 网站建设公司有哪些大数据精准客户
  • 生态旅游网站的建设东莞企业网站排名
  • 南阳网站建设公司二级分销小程序
  • 做网站的基础架构四川餐饮培训学校排名
  • dw个人网站建立教学郑州百度推广外包
  • 网站建设费用明细表网站权重等级
  • 平面设计师服务平台seo排名工具有哪些
  • 网站装修的代码怎么做的优化设计方案
  • 公司企业网站源码百度霸屏推广靠谱吗
  • 深圳seo网站优化公司关键词在线播放免费
  • 中装建设集团网站湛江今日头条新闻
  • gvm网站是什么类的网站软件推广平台有哪些
  • 工程公司名字大全广州百度seo排名优化
  • 做美食直播哪个网站最好seo课程培训机构
  • 九天智能建站软件有人看片吗免费观看视频
  • 蛋糕方案网站建设怎样申请自己的电商平台
  • wordpress站外连接郑州网站建设推广优化
  • 做网站的用多少钱软文发布软件
  • 做地产网站哪家好做seo有什么好处
  • 怎么把自己笔记本做服务器做个网站买卖网站
  • 住建局领导班子成员分工搜索引擎优化规则
  • 厚街镇网站仿做网络营销推广公司有哪些
  • 公司微网站怎么做的好深圳全网营销型网站
  • js显示其他网站页面今日国内新闻重大事件
  • 工信部做网站认证吗购物网站排名
  • 做视频必须知道的一些网站百度广告上的商家可靠吗