内存映射文件MappedByteBuffer和Buffer的Scattering与Gathering
2024-09-06 06:26:55
上一篇讲到的DirectByteBuffer继承自MappedByteBuffer
一、MappedByteBuffer
MappedByteBuffer的定义:
A direct byte buffer whose content is a memory-mapped region of a file.
直接缓存,内容是一个内存映射文件。
创建测试类
public class NioTest9 {
public static void main(String[] args) throws Exception {
RandomAccessFile randomAccessFile = new RandomAccessFile("NioTest9.txt","rw");
FileChannel fileChannel = randomAccessFile.getChannel();
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0,5);
mappedByteBuffer.put(0,(byte)'a');
mappedByteBuffer.put(3,(byte)'b'); randomAccessFile.close();
}
}
创建NioTest9.tex文件
运行程序,用记事本打开
操作的是堆外内存,堆外内存写入到文件由操作系统控制。
二、排他锁和共享锁
实际用的比较少
/**
*
* 共享锁: 所有程序都能多共享的部分进行读
* 排他锁: 只有一个程序对锁定的部分进行写操作
*
*/
public class NioTest10 { public static void main(String[] args) throws Exception {
RandomAccessFile randomAccessFile = new RandomAccessFile("NioTest10.txt", "rw"); FileChannel fileChannel = randomAccessFile.getChannel(); FileLock fileLock = fileChannel.lock(3,6,true); System.out.println("valid:" + fileLock.isValid()); System.out.println("lock type:" + fileLock.isShared()); fileLock.release(); randomAccessFile.close();
}
}
三、关于Buffer的Scattering(散开)与Gathering(收集)
/**
*
* 关于Buffer的Scattering(散开)与Gathering(收集)
*
*/
public class NioTest11 { public static void main(String[] args) throws Exception { ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); InetSocketAddress address = new InetSocketAddress(8899); serverSocketChannel.socket().bind(address); int messageLength = 2 + 3 + 4;
ByteBuffer[] buffers = new ByteBuffer[3];
buffers[0] = ByteBuffer.allocate(2);
buffers[1] = ByteBuffer.allocate(3);
buffers[2] = ByteBuffer.allocate(4); SocketChannel socketChannel = serverSocketChannel.accept(); while (true){
int byteRead = 0;
while (byteRead < messageLength){
long r = socketChannel.read(buffers);
byteRead += r; System.out.println("bytesRead:" + byteRead); Arrays.asList(buffers).stream().map(buffer -> "position:" + buffer.position() +",limit:" + buffer.limit() + " 值:" + buffer.toString()).forEach(System.out::println);
} Arrays.asList(buffers).forEach(buffer -> {
buffer.flip();
}); long byteWritten = 0;
while (byteWritten < messageLength){
long r = socketChannel.write(buffers);
byteWritten += r;
} Arrays.asList(buffers).forEach(bufffer -> {
bufffer.clear();
}); System.out.println("bytesRead:" + byteRead + ",byteWritten: " + byteWritten +", messageLength:" + messageLength);
} }
}
使用Telnet发送,输出结果如下图:
最新文章
- Kindeditor在ThinkPHP框架下的使用
- CodeIgniter 3 源码学习笔记《一》
- iOS runtime的理解和应用
- Java项目相关监控与调优
- winform 控件(1)
- POj3104 Drying(二分)
- Core Data 概述
- 机器人与机器人仿真技术(zz)
- 小扩展大用处,自己扩展一个ForeachRead吧
- [物理学与PDEs]第4章 反应流体力学
- [HDU 3535] AreYouBusy (动态规划 混合背包 值得做很多遍)
- 解决OOM小记
- ActionScript接收socket服务器发送来的数据
- 智能电视TV开发---如何实现程序省电
- LPC1768外部中断与GPIO中断
- Javascript中变量作用域
- 轻量级文本编辑器,Notepad最佳替代品:Notepad++
- Elastic 技术栈之 Logstash 基础
- python的学习笔记01_2变量 常量 注释 用户交互 格式化输出
- Java————迷宫问题
热门文章
- Springboot jpa多数据源
- PHP应用如何对接微信公众号JSAPI支付
- Flink Time深度解析(转)
- z = z*z + c的分型图如何画
- 【转】STM32生成的文件大小探索
- SaltStack--项目实战
- which had no Root Element. This likely means the XML is malformed or missing
- javax.persistence.PersistenceException: Unable to build entity manager factory
- 认识Activiti
- 强大的接口调试工具-Postman图文详解