java nio 内存映射_Java Nio中的三种内存映射缓冲区---MappedByteBuffer

该博客展示了Java NIO中文件映射(Memory-Mapped File)的使用,通过创建不同类型的缓冲区(ReadOnly, ReadWrite, Private)进行文件内容的读写和修改。在代码示例中,作者创建了一个临时文件,然后使用RandomAccessFile和FileChannel进行缓冲区操作,包括写入内容、修改内容,并展示了不同映射模式下缓冲区的变化。
摘要由CSDN通过智能技术生成

开始有点跟不上实际思路了,

今天暂停吧。

作个记录。

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'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值