内存映射文件

MappedByteBuffer

内存映射文件允许我们修改和编辑那些因为太大不能放入内存的文件。
java.nio.channels.FileChannel.map(MapMode mode, long position, long size) throws IOException

package com.zachary.io.nio.mapped;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Zachary.Zheng
 * @version 1.0
 * @date 2020年6月1日 下午5:36:11
 */
public class LargeMappedFiles {
	static int length = 0x8FFFFFF; // 128M
	public static void main(String[] args) throws FileNotFoundException, IOException {
		MappedByteBuffer out = new RandomAccessFile("file/output/MappedByteBuffer/data.txt", "rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, length);
		for(int i=0; i<length; i++) {
			out.put((byte) 'x');
		}
		System.out.println("Finished writing");
		for(int i= length/2; i< length/2 + 6; i++) {
			System.out.println((char)out.get(i));
		}
		
		System.out.println("MappedFileTest writing");
		mappedFileTest();
	}
	
	private static void mappedFileTest() throws FileNotFoundException, IOException {
		MappedByteBuffer out = new RandomAccessFile("file/output/MappedByteBuffer/data.txt", "rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 10);
		int count = 0;
		while(out.hasRemaining()) {
			System.out.println((char) out.get());
			count++;
		}
		System.out.println(count);
	}
}

Output:
Finished writing
x
x
x
x
x
x
MappedFileTest writing
x
x
x
x
x
x
x
x
x
x
10

性能

尽管“旧”I/O在行的nio实现后性能有所提高,但“映射文件访问”往往能更加显著的提高速度。

package com.zachary.io.nio.mapped;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Zachary.Zheng
 * @version 1.0
 * @date 2020年6月1日 下午6:06:36
 */
public class MappedIO {
	private static int numOfInts = 4000000;
	private static int numUbuffInts = 200000;

	private abstract static class Tester {
		private String name;

		public Tester(String name) {
			this.name = name;
		}

		public void runTest() {
			System.out.println(name + ": ");
			try {
				long start = System.nanoTime();
				test();
				double duration = System.nanoTime() - start;
				System.out.format("%.2f\n", duration/1.0e9);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}

		public abstract void test() throws IOException;
	}

	public static Tester[] tests = { 
		new Tester("Stream Write") {
			@Override
			public void test() throws IOException {
				DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("file/output/MappedByteBuffer/Write.tmp")));
				for (int i = 0; i < numOfInts; i++) {
					out.writeInt(i);
				}
				out.close();
			}
		},
		new Tester("Mappend Write") {
			@Override
			public void test() throws IOException {
				FileChannel fc = new RandomAccessFile("file/output/MappedByteBuffer/Write.tmp","rw").getChannel();
				IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
				for (int i = 0; i < numOfInts; i++) {
					ib.put(i);
				}
				fc.close();
			}
		},
		new Tester("Stream Read") {
			@Override
			public void test() throws IOException {
				DataInputStream out = new DataInputStream(new BufferedInputStream(new FileInputStream("file/output/MappedByteBuffer/Write.tmp")));
				for (int i = 0; i < numOfInts; i++) {
					out.readInt();
				}
				out.close();
			}
		},
		new Tester("Mappend Read") {
			@Override
			public void test() throws IOException {
				FileChannel fc = new RandomAccessFile("file/output/MappedByteBuffer/Write.tmp","rw").getChannel();
				IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
				while(ib.hasRemaining()) {
					ib.get();
				}
				fc.close();
			}
		},
		new Tester("Stream Read/Write") {
			@Override
			public void test() throws IOException {
				RandomAccessFile raf = new RandomAccessFile("file/output/MappedByteBuffer/Write.tmp", "rw");
				raf.writeInt(1);
				for(int i = 0; i < numUbuffInts; i++) {
					raf.seek(raf.length() - 4);
					raf.writeInt(raf.read());
				}
				raf.close();
			}
		},
		new Tester("Mappend Read/Write") {
			@Override
			public void test() throws IOException {
				FileChannel fc = new RandomAccessFile("file/output/MappedByteBuffer/Write.tmp","rw").getChannel();
				IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
				ib.put(0);
				for(int i = 1; i < numUbuffInts; i++) {
					ib.put(ib.get(i-1));
				}
				fc.close();
			}
		}
	};
	public static void main(String[] args) {
		for (Tester tester : tests) {
			tester.runTest();
		}
	}
}

Output:
Stream Write:
0.21
Mappend Write:
0.02
Stream Read:
0.31
Mappend Read:
0.00
Stream Read/Write:
2.44
Mappend Read/Write:
0.00

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值