关于ByteBuffer、MappedByteBuffer类学习
来源:互联网 发布:罗马共和国知乎 编辑:程序博客网 时间:2024/05/22 05:13
最近公司有个小项目要放在springboot里让我改造下,里面有MappedByteBuffer所以百度+自己看代码学习了一下,记录下,防止忘了
MappedByteBuffer继承ByteBuffer,ByteBuffer是个缓冲区方便自己的IO操作而MappedByteBuffer是java nio引入的文件内存映射方案,读写性能极高。大文件用缓存区的话用这个,具体的还没深入研究,先学习下用法。
ByteBuff学习:
简单事例:实现文件的复制
public class MappedByteBufferTest { public static void main(String[] args) { ByteBuffer buff = ByteBuffer.allocate(128); FileChannel fin = null; FileChannel fout = null; try{ fin = new FileInputStream("E:"+File.separator+"test"+File.separator+"redis-4.0.1.tar.gz").getChannel(); fout = new FileOutputStream("E:"+File.separator+"test1"+File.separator+"redis-4.0.1.tar.gz").getChannel(); while(fin.read(buff) != -1) { buff.flip(); fout.write(buff); buff.clear(); } }catch (Exception e) { e.printStackTrace(); }finally { try { if(fin != null) { fin.close(); } if(fout != null) { fout.close(); } } catch(IOException e) { e.printStackTrace(); } } }}
ByteBuffer buff = ByteBuffer.allocate(128);开辟一个缓冲区,然后我们可以在这个缓冲区里进行一些操作。
ByteBuffer 有4个索引mark,position,limit,capacity
mark用于对当前position的标记(allocate后-1)
position表示当前可读写的指针,就是你要读写的位置,读写玩都往后加1
limit是可以读写的边界,相当于记录用到了缓存的哪个位置,ByteBuffer.allocate(128)后在128这个位置,如果用了100调用buff.flip()后那么他变到100这个位置。然后你就可以读到这个位置就知道读完了。
capacity是这个ByteBuffer的容量,如上就是128
ByteBuffer buff = ByteBuffer.allocate(128);
开辟缓存区时: mark : -1 ,position :0,limit :128,capacity:128
开始写入数据position随着写移动位置
写完
buff.flip();方法:
public final Buffer flip() { limit = position; position = 0; mark = -1; return this; }
这里向缓存区写入字节后,你要开始使用缓存区时调用,如上将limit放到读到的位置,position置0;
使用一次后你要重复使用这个缓存区就要掉 buff.clear()方法:
public final Buffer clear() { position = 0; limit = capacity; mark = -1; return this; }
一切回到初始化状态然后使用
MappedByteBuffer 学习:
MappedByteBuffer 继承 ByteBuffer
简单事例:
1读取文件:
package com.zzm.test;import java.io.File;import java.io.FileInputStream;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;import java.nio.charset.Charset;public class MappedByteBufferTest2 { private final static Charset charset = Charset.forName("GBK"); public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("E:"+File.separator+"test"+File.separator+"123.txt"); FileChannel fc = fis.getChannel(); MappedByteBuffer mbb = fis.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); fis.close(); fc.close(); String str = charset.decode(mbb).toString(); System.out.println(str); } catch (Exception e) { e.printStackTrace(); } }}
2实现文件的复制
package com.zzm.test;import java.io.File;import java.io.FileInputStream;import java.nio.ByteBuffer;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;public class MappedByteBufferTest3 { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("E:"+File.separator+"test"+File.separator+"123.txt"); FileChannel fc = fis.getChannel(); MappedByteBuffer mbb = fis.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); fis.close(); fc.close(); System.out.println(mbb.capacity()); System.out.println(mbb.limit()); System.out.println(mbb.position()); //mbb.flip(); System.out.println(mbb.hasArray()); System.out.println(mbb.capacity()); System.out.println(mbb.limit()); System.out.println(mbb.position()); byte[] bytes = new byte[mbb.limit()]; ByteBuffer bb = mbb.get(bytes); System.out.println(new String(bytes,"GBK")); } catch (Exception e) { e.printStackTrace(); } }}
MappedByteBuffer mbb = fis.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, fc.size());相当于开辟缓存区并将文件放进去,然后我们就可以进行一些列的操作。
- 关于ByteBuffer、MappedByteBuffer类学习
- 使用ByteBuffer和MappedByteBuffer的简单例子
- Difference between MappedByteBuffer and ByteBuffer.allocateDirect()
- 关于ByteBuffer
- MappedByteBuffer的学习
- 关于MappedByteBuffer资源释放问题
- ByteBuffer 类
- MappedByteBuffer
- MappedByteBuffer
- 关于ByteBuffer使用解释
- java byteBuffer 的学习
- Bytebuffer学习笔记二
- Java 学习系列:ByteBuffer
- java内存映射类MappedByteBuffer
- 关于MappedByteBuffer使用后无法删除问题
- 关于MappedByteBuffer占用内存和文件关闭
- 关于ByteBuffer的一些知识
- C#实现ByteBuffer类
- thread 线程执行顺序
- 电磁波中的波段划分:L波段、S波段、C波段、X波段、Ku波段、K波段、Ka波段
- 1 redis在spring中的配置及java代码实现 2 redis在java项目中的使用
- prototype
- SSH公共action集成easyui分页+模型驱动
- 关于ByteBuffer、MappedByteBuffer类学习
- 从年会看声明式编程(Declarative Programming)
- JAVA课程学习四:遍历与复制数组
- 使用CSDN-markdown编辑器
- PDF转换器使用方法分享:Word文件怎么转换成PDF格式
- C++调用外部程序
- 模拟实现strcpy函数
- 为什么他们都用UWA GOT?
- Effective Java中文第四章第17节(个人渣翻)