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'



目录
相关文章
|
10月前
|
Java 大数据
解析Java中的NIO与传统IO的区别与应用
解析Java中的NIO与传统IO的区别与应用
|
6月前
|
传感器 人工智能 物联网
C 语言在计算机科学中尤其在硬件交互方面占据重要地位。本文探讨了 C 语言与硬件交互的主要方法,包括直接访问硬件寄存器、中断处理、I/O 端口操作、内存映射 I/O 和设备驱动程序开发
C 语言在计算机科学中尤其在硬件交互方面占据重要地位。本文探讨了 C 语言与硬件交互的主要方法,包括直接访问硬件寄存器、中断处理、I/O 端口操作、内存映射 I/O 和设备驱动程序开发,以及面临的挑战和未来趋势,旨在帮助读者深入了解并掌握这些关键技术。
147 6
|
7月前
|
存储 缓存 固态存储
|
8月前
|
存储 缓存 Linux
用户态内存映射
【9月更文挑战第20天】内存映射不仅包括物理与虚拟内存间的映射,还涉及将文件内容映射至虚拟内存,使得访问内存即可获取文件数据。mmap 系统调用支持将文件或匿名内存映射到进程的虚拟内存空间,通过多级页表机制实现高效地址转换,并利用 TLB 加速映射过程。TLB 作为页表缓存,存储频繁访问的页表项,显著提升了地址转换速度。
|
7月前
|
Linux C++
Linux c/c++文件虚拟内存映射
这篇文章介绍了在Linux环境下,如何使用虚拟内存映射技术来提高文件读写的速度,并通过C/C++代码示例展示了文件映射的整个流程。
181 0
|
8月前
|
存储 安全 Linux
将文件映射到内存,像数组一样访问
将文件映射到内存,像数组一样访问
77 0
|
8月前
|
消息中间件 Linux 容器
共享内存的创建和映射过程
【9月更文挑战第1天】消息队列、共享内存及信号量在使用前需生成key并获取唯一ID,均通过`xxxget`函数实现。
|
10月前
|
Java
Java中的NIO编程详解
Java中的NIO编程详解
|
9月前
|
C++ 容器
curl使用小记(三)——获取远端数据到内存缓冲区
curl使用小记(三)——获取远端数据到内存缓冲区
111 0
|
10月前
|
Java 大数据
如何在Java中进行网络编程:Socket与NIO
如何在Java中进行网络编程:Socket与NIO

热门文章

最新文章