NIO - MappedByteBuffer

转自:http://blog.csdn.net/java2000_wl/article/details/7625021

*MappedByteBuffer的创建

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

  1. public MappedByteBuffer map(MapMode mode, long position, long size)  
  2.    

  MapMode  映射模式(MapMode 是FileChannel中的一个内部类) 有三个可选值

   1.READ_ONLY    只读映射模式

   2.READ_WRITE  读/写映射模式

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

扫描二维码关注公众号,回复: 655854 查看本文章

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

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

  1. private final static Charset charset = Charset.forName("GBK");    
  2.       
  3.     /** 
  4.      * 读文件 
  5.      * <br>------------------------------<br> 
  6.      * @param path 
  7.      * @return 
  8.      * @throws IOException 
  9.      */  
  10.     private static String read(String path) throws IOException {  
  11.         if (path == null || path.length() == 0return null;  
  12.         FileInputStream fileInputStream =  new FileInputStream(path);  
  13.         FileChannel fileChannel =  fileInputStream.getChannel();  
  14.         MappedByteBuffer mappedByteBuffer = fileChannel.map(MapMode.READ_ONLY, 0, fileChannel.size());  
  15.         fileInputStream.close();  
  16.         fileChannel.close();  
  17.         String str = charset.decode(mappedByteBuffer).toString();  
  18.         mappedByteBuffer = null;  
  19.         return str;  
  20.     }  
  21.       
  22.     /** 
  23.      * 追加内容 
  24.      * <br>------------------------------<br> 
  25.      * @param path 
  26.      * @param str 
  27.      * @return 
  28.      * @throws IOException 
  29.      */  
  30.     private static MappedByteBuffer append(String path, String str) throws IOException {  
  31.         if (str == null || str.length() == 0return null;  
  32.         RandomAccessFile randomAccessFile = new RandomAccessFile(path, "rw");  
  33.         FileChannel fileChannel = randomAccessFile.getChannel();  
  34.         byte[] bytes = str.getBytes();  
  35.         long size = fileChannel.size() + bytes.length;  
  36.         MappedByteBuffer mappedByteBuffer = fileChannel.map(MapMode.READ_WRITE, 0, size);  
  37.         fileChannel.close();  
  38.         randomAccessFile.close();  
  39.           
  40.         int position = mappedByteBuffer.limit() - bytes.length;  
  41.         mappedByteBuffer.position(position);  
  42.         mappedByteBuffer.put(bytes);  
  43.         mappedByteBuffer.force();  
  44.         mappedByteBuffer.flip();  
  45.         return mappedByteBuffer;  
  46.     }  
  47.       
  48.     /** 
  49.      * 文件复制 
  50.      * <br>------------------------------<br> 
  51.      * @param srcfilePath 
  52.      * @param targetPath 
  53.      * @throws IOException  
  54.      */  
  55.     private static void copy(String srcfilePath, String targetPath) throws IOException {   
  56.         File file = new File(targetPath);    
  57.         if (!file.getParentFile().exists()) {    
  58.             file.mkdirs();    
  59.         }    
  60.         RandomAccessFile inRandomAccessFile = new RandomAccessFile(srcfilePath, "r");  
  61.         FileChannel inFileChannel = inRandomAccessFile.getChannel();  
  62.         MappedByteBuffer inMappedByteBuffer = inFileChannel.map(MapMode.READ_ONLY, 0, inFileChannel.size());  
  63.         inRandomAccessFile.close();  
  64.         inFileChannel.close();  
  65.           
  66.         RandomAccessFile outRandomAccessFile = new RandomAccessFile(targetPath, "rw");  
  67.         FileChannel outFileChannel = outRandomAccessFile.getChannel();  
  68.         MappedByteBuffer outMappedByteBuffer = outFileChannel.map(MapMode.READ_WRITE, 0, inMappedByteBuffer.capacity());  
  69.         outMappedByteBuffer.put(inMappedByteBuffer);  
  70.         outMappedByteBuffer.force();  
  71.         outRandomAccessFile.close();  
  72.         outFileChannel.close();  
  73.         outMappedByteBuffer.flip();  
  74.     }  
  75.       
  76.     /** 
  77.      * 不会更新到文件 只会更新虚拟内存  
  78.      * <br>------------------------------<br> 
  79.      * @param path 
  80.      * @param str 
  81.      * @return 
  82.      * @throws IOException 
  83.      */  
  84.     private static MappedByteBuffer doTestPrivateMode(String path, String str) throws IOException {  
  85.         if (str == null || str.length() == 0return null;  
  86.         RandomAccessFile randomAccessFile = new RandomAccessFile(path, "rw");  
  87.         FileChannel fileChannel = randomAccessFile.getChannel();  
  88.         byte[] bytes = str.getBytes();  
  89.         long size = fileChannel.size() + bytes.length;  
  90.         MappedByteBuffer mappedByteBuffer = fileChannel.map(MapMode.PRIVATE, 0, size);  
  91.         mappedByteBuffer.put(bytes);  
  92.         fileChannel.close();  
  93.         randomAccessFile.close();  
  94.           
  95.         mappedByteBuffer.flip();  
  96.         System.out.println(charset.decode(mappedByteBuffer));  
  97.         return mappedByteBuffer;  
  98.     }  

猜你喜欢

转载自ximeng1234.iteye.com/blog/2292008
NIO