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'



目录
相关文章
|
15天前
|
存储 Java 测试技术
滚雪球学Java(18):解密JavaSE中的堆栈:你真的了解Java内存吗?
【4月更文挑战第7天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
41 1
滚雪球学Java(18):解密JavaSE中的堆栈:你真的了解Java内存吗?
|
7天前
|
存储 Java
深入理解Java虚拟机:JVM内存模型
【4月更文挑战第30天】本文将详细解析Java虚拟机(JVM)的内存模型,包括堆、栈、方法区等部分,并探讨它们在Java程序运行过程中的作用。通过对JVM内存模型的深入理解,可以帮助我们更好地编写高效的Java代码,避免内存溢出等问题。
|
10天前
|
算法 Java Go
Go vs Java:内存管理与垃圾回收机制对比
对比了Go和Java的内存管理与垃圾回收机制。Java依赖JVM自动管理内存,使用堆栈内存并采用多种垃圾回收算法,如标记-清除和分代收集。Go则提供更多的手动控制,内存分配与释放由分配器和垃圾回收器协同完成,使用三色标记算法并发回收。示例展示了Java中对象自动创建和销毁,而Go中开发者需注意内存泄漏。选择语言应根据项目需求和技术栈来决定。
|
5天前
|
存储 缓存 监控
|
7天前
|
存储 算法 内存技术
深入理解操作系统内存管理:从虚拟内存到物理内存的映射
【4月更文挑战第30天】 在现代操作系统中,内存管理是一个复杂而关键的功能。它不仅确保了系统资源的有效利用,还为每个运行的程序提供了独立的地址空间,保障了程序之间的隔离性和安全性。本文将探讨操作系统如何通过分页机制和虚拟内存技术实现内存的抽象化,以及这些技术是如何影响应用程序性能的。我们将详细解析虚拟地址到物理地址的转换过程,并讨论操作系统在此过程中扮演的角色。文章的目的是为读者提供一个清晰的框架,以便更好地理解内存管理的工作原理及其对系统稳定性和效率的影响。
|
8天前
|
存储 机器学习/深度学习 Java
【Java探索之旅】数组使用 初探JVM内存布局
【Java探索之旅】数组使用 初探JVM内存布局
20 0
|
11天前
|
Java 开发者
【JAVA】Java内存模型中的happen-before
Happen-before关系帮助开发者理解多线程程序中操作的执行顺序和可见性,从而避免竞态条件和数据不一致性问题。它提供了一种可预测的规则来确保线程之间的正确交互。
22 0
|
12天前
|
存储 缓存 安全
深入理解Java内存模型及其作用
深入理解Java内存模型及其作用
|
13天前
|
运维 Kubernetes 算法
Java堆内存又溢出了!教你一招必杀技
Java堆内存又溢出了!教你一招必杀技
|
13天前
|
安全 Java 调度
[Java并发基础] 共享内存
[Java并发基础] 共享内存