首页 文章 精选 留言 我的

精选列表

搜索[内存压力],共10000篇文章
优秀的个人博客,低调大师

Java Nio中的三种内存映射缓冲区---MappedByteBuffer

开始有点跟不上实际思路了, 今天暂停吧。 作个记录。 package com.ronsoft.books.nio.channels; import java.io.File; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class MapFile { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub File tempFile = File.createTempFile("mmaptest", null); RandomAccessFile file = new RandomAccessFile(tempFile, "rw"); FileChannel channel = file.getChannel(); ByteBuffer temp = ByteBuffer.allocate(100); temp.put("This is the file content".getBytes()); temp.flip(); channel.write(temp, 0); temp.clear(); temp.put("This is more file content".getBytes()); temp.flip(); channel.write(temp, 8192); MappedByteBuffer ro = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); MappedByteBuffer rw = channel.map(FileChannel.MapMode.READ_WRITE, 0, channel.size()); MappedByteBuffer cow = channel.map(FileChannel.MapMode.PRIVATE, 0, channel.size()); System.out.println("Begin"); showBuffers(ro, rw, cow); cow.position(8); cow.put("COW".getBytes()); System.out.println("Change to COW buffer"); showBuffers(ro, rw, cow); rw.position(9); rw.put(" R/W".getBytes()); rw.position(8194); rw.put(" R/W".getBytes()); rw.force(); System.out.println("Change to R/W buffer"); showBuffers(ro, rw, cow); temp.clear(); temp.put("Channel write ".getBytes()); temp.flip(); channel.write(temp, 0); temp.rewind(); channel.write(temp, 8202); System.out.println("Write on channel"); showBuffers(ro, rw, cow); cow.position(8207); cow.put(" COW2".getBytes()); System.out.println("Second change to COW buffer"); showBuffers(ro, rw, cow); rw.position(0); rw.put(" R/W2 ".getBytes()); rw.position(8210); rw.put(" R/W2 ".getBytes()); rw.force(); System.out.println("Second change to R/W buffer"); showBuffers(ro, rw, cow); channel.close(); file.close(); tempFile.delete(); } public static void showBuffers(ByteBuffer ro, ByteBuffer rw, ByteBuffer cow) throws Exception { dumpBuffer("R/O", ro); dumpBuffer("R/W", rw); dumpBuffer("COW", cow); System.out.println(""); } public static void dumpBuffer(String prefix, ByteBuffer buffer) throws Exception { System.out.print(prefix + ": '"); int nulls = 0; int limit = buffer.limit(); for (int i = 0; i < limit; i++) { char c = (char)buffer.get(i); if (c == '\u0000') { nulls++; continue; } if (nulls != 0) { System.out.print("|[" + nulls + " nulls]|"); nulls = 0; } System.out.print(c); } System.out.println("'"); } } Begin R/O: 'This is the file content|[8168 nulls]|This is more file content' R/W: 'This is the file content|[8168 nulls]|This is more file content' COW: 'This is the file content|[8168 nulls]|This is more file content' Change to COW buffer R/O: 'This is the file content|[8168 nulls]|This is more file content' R/W: 'This is the file content|[8168 nulls]|This is more file content' COW: 'This is COW file content|[8168 nulls]|This is more file content' Change to R/W buffer R/O: 'This is t R/Wile content|[8168 nulls]|Th R/Ws more file content' R/W: 'This is t R/Wile content|[8168 nulls]|Th R/Ws more file content' COW: 'This is COW file content|[8168 nulls]|Th R/Ws more file content' Write on channel R/O: 'Channel write le content|[8168 nulls]|Th R/Ws moChannel write t' R/W: 'Channel write le content|[8168 nulls]|Th R/Ws moChannel write t' COW: 'This is COW file content|[8168 nulls]|Th R/Ws moChannel write t' Second change to COW buffer R/O: 'Channel write le content|[8168 nulls]|Th R/Ws moChannel write t' R/W: 'Channel write le content|[8168 nulls]|Th R/Ws moChannel write t' COW: 'This is COW file content|[8168 nulls]|Th R/Ws moChann COW2ite t' Second change to R/W buffer R/O: ' R/W2 l write le content|[8168 nulls]|Th R/Ws moChannel R/W2 t' R/W: ' R/W2 l write le content|[8168 nulls]|Th R/Ws moChannel R/W2 t' COW: 'This is COW file content|[8168 nulls]|Th R/Ws moChann COW2ite t'

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册