NIO - MappedByteBuffer

  *MappedByteBuffer的创建

  在FileChannel上调用map方法 返回一个MappedByteBuffer对象  

public MappedByteBuffer map(MapMode mode, long position, long size)
 
  MapMode  映射模式(MapMode 是FileChannel中的一个内部类) 有三个可选值

   1.READ_ONLY    只读映射模式

   2.READ_WRITE  读/写映射模式

   3.PRIVATE           通过put方法对MappedByteBuffer的修改   不会修改到磁盘文件  只是虚拟内存的修改

*MappedByteBuffer在父类ByteBuffer的基础上 新增的几个方法

   1.fore缓冲区在READ_WRITE模式下,此方法对缓冲区所做的内容更改强制写入文件
   2.load:将缓冲区的内容载入物理内存,并返回该缓冲区的引用
   3.isLoaded:判断缓冲区的内容是否在物理内存,如果在则返回true,否则返回false

private final static Charset charset = Charset.forName("GBK");  
	
	/**
	 * 读文件
	 * <br>------------------------------<br>
	 * @param path
	 * @return
	 * @throws IOException
	 */
	private static String read(String path) throws IOException {
		if (path == null || path.length() == 0) return null;
		FileInputStream fileInputStream =  new FileInputStream(path);
		FileChannel fileChannel =  fileInputStream.getChannel();
		MappedByteBuffer mappedByteBuffer = fileChannel.map(MapMode.READ_ONLY, 0, fileChannel.size());
		fileInputStream.close();
		fileChannel.close();
		String str = charset.decode(mappedByteBuffer).toString();
		mappedByteBuffer = null;
		return str;
	}
	
	/**
	 * 追加内容
	 * <br>------------------------------<br>
	 * @param path
	 * @param str
	 * @return
	 * @throws IOException
	 */
	private static MappedByteBuffer append(String path, String str) throws IOException {
		if (str == null || str.length() == 0) return null;
		RandomAccessFile randomAccessFile = new RandomAccessFile(path, "rw");
		FileChannel fileChannel = randomAccessFile.getChannel();
		byte[] bytes = str.getBytes();
		long size = fileChannel.size() + bytes.length;
		MappedByteBuffer mappedByteBuffer = fileChannel.map(MapMode.READ_WRITE, 0, size);
		fileChannel.close();
		randomAccessFile.close();
		
		int position = mappedByteBuffer.limit() - bytes.length;
		mappedByteBuffer.position(position);
		mappedByteBuffer.put(bytes);
		mappedByteBuffer.force();
		mappedByteBuffer.flip();
		return mappedByteBuffer;
	}
	
	/**
	 * 文件复制
	 * <br>------------------------------<br>
	 * @param srcfilePath
	 * @param targetPath
	 * @throws IOException 
	 */
	private static void copy(String srcfilePath, String targetPath) throws IOException { 
		File file = new File(targetPath);  
		if (!file.getParentFile().exists()) {  
			file.mkdirs();  
		}  
		RandomAccessFile inRandomAccessFile = new RandomAccessFile(srcfilePath, "r");
		FileChannel inFileChannel = inRandomAccessFile.getChannel();
		MappedByteBuffer inMappedByteBuffer = inFileChannel.map(MapMode.READ_ONLY, 0, inFileChannel.size());
		inRandomAccessFile.close();
		inFileChannel.close();
		
		RandomAccessFile outRandomAccessFile = new RandomAccessFile(targetPath, "rw");
		FileChannel outFileChannel = outRandomAccessFile.getChannel();
		MappedByteBuffer outMappedByteBuffer = outFileChannel.map(MapMode.READ_WRITE, 0, inMappedByteBuffer.capacity());
		outMappedByteBuffer.put(inMappedByteBuffer);
		outMappedByteBuffer.force();
		outRandomAccessFile.close();
		outFileChannel.close();
		outMappedByteBuffer.flip();
	}
	
	/**
	 * 不会更新到文件 只会更新虚拟内存 
	 * <br>------------------------------<br>
	 * @param path
	 * @param str
	 * @return
	 * @throws IOException
	 */
	private static MappedByteBuffer doTestPrivateMode(String path, String str) throws IOException {
		if (str == null || str.length() == 0) return null;
		RandomAccessFile randomAccessFile = new RandomAccessFile(path, "rw");
		FileChannel fileChannel = randomAccessFile.getChannel();
		byte[] bytes = str.getBytes();
		long size = fileChannel.size() + bytes.length;
		MappedByteBuffer mappedByteBuffer = fileChannel.map(MapMode.PRIVATE, 0, size);
		mappedByteBuffer.put(bytes);
		fileChannel.close();
		randomAccessFile.close();
		
		mappedByteBuffer.flip();
		System.out.println(charset.decode(mappedByteBuffer));
		return mappedByteBuffer;
	}


 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值