上一篇讲到的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发送,输出结果如下图:

最新文章

  1. Kindeditor在ThinkPHP框架下的使用
  2. CodeIgniter 3 源码学习笔记《一》
  3. iOS runtime的理解和应用
  4. Java项目相关监控与调优
  5. winform 控件(1)
  6. POj3104 Drying(二分)
  7. Core Data 概述
  8. 机器人与机器人仿真技术(zz)
  9. 小扩展大用处,自己扩展一个ForeachRead吧
  10. [物理学与PDEs]第4章 反应流体力学
  11. [HDU 3535] AreYouBusy (动态规划 混合背包 值得做很多遍)
  12. 解决OOM小记
  13. ActionScript接收socket服务器发送来的数据
  14. 智能电视TV开发---如何实现程序省电
  15. LPC1768外部中断与GPIO中断
  16. Javascript中变量作用域
  17. 轻量级文本编辑器,Notepad最佳替代品:Notepad++
  18. Elastic 技术栈之 Logstash 基础
  19. python的学习笔记01_2变量 常量 注释 用户交互 格式化输出
  20. Java————迷宫问题

热门文章

  1. Springboot jpa多数据源
  2. PHP应用如何对接微信公众号JSAPI支付
  3. Flink Time深度解析(转)
  4. z = z*z + c的分型图如何画
  5. 【转】STM32生成的文件大小探索
  6. SaltStack--项目实战
  7. which had no Root Element. This likely means the XML is malformed or missing
  8. javax.persistence.PersistenceException: Unable to build entity manager factory
  9. 认识Activiti
  10. 强大的接口调试工具-Postman图文详解