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'



目录
相关文章
|
5天前
|
存储 安全 Java
Java面试题:请解释Java内存模型(JMM)是什么,它如何保证线程安全?
Java面试题:请解释Java内存模型(JMM)是什么,它如何保证线程安全?
37 13
|
5天前
|
存储 Java 程序员
Java面试题:方法区在JVM中存储什么内容?它与堆内存有何不同?
Java面试题:方法区在JVM中存储什么内容?它与堆内存有何不同?
26 10
|
5天前
|
存储 运维 Java
Java面试题:JVM的内存结构有哪些主要部分?请简述每个部分的作用
Java面试题:JVM的内存结构有哪些主要部分?请简述每个部分的作用
26 9
|
4天前
|
缓存 监控 算法
Java内存怎么优化
【7月更文挑战第11天】Java内存怎么优化
11 3
|
5天前
|
缓存 安全 Java
Java面试题:解释volatile关键字的作用,以及它如何保证内存的可见性
Java面试题:解释volatile关键字的作用,以及它如何保证内存的可见性
19 4
|
5天前
|
Java 程序员 编译器
Java面试题:解释Java内存模型(JMM)是什么,它为何重要?
Java面试题:解释Java内存模型(JMM)是什么,它为何重要?
20 2
|
5天前
|
缓存 Prometheus 监控
Java面试题:如何监控和优化JVM的内存使用?详细讲解内存调优的几种方法
Java面试题:如何监控和优化JVM的内存使用?详细讲解内存调优的几种方法
25 3
|
5天前
|
监控 Java 数据库连接
Java面试题:如何诊断和解决Java应用的内存泄漏问题?
Java面试题:如何诊断和解决Java应用的内存泄漏问题?
12 2
|
5天前
|
安全 Java
Java面试题:解释synchronized关键字在Java内存模型中的语义
Java面试题:解释synchronized关键字在Java内存模型中的语义
9 1
|
3天前
|
存储 算法 Java
JAVA内存模型与JVM内存模型的区别
JAVA内存模型与JVM内存模型的区别