返回顶部

收藏

Java 内存映射文件 MappedByteBuffer

更多

MapFile.java

package com.ronsoft.books.nio.channels;

import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.io.File;
import java.io.RandomAccessFile;

/**
 * Test behavior of Memory mapped buffer types.  Create a file, write
 * some data to it, then create three different types of mappings
 * to it.  Observe the effects of changes through the buffer APIs
 * and updating the file directly.  The data spans page boundaries
 * to illustrate the page-oriented nature of Copy-On-Write mappings.
 *
 * Created: April 2002
 * @author Ron Hitchens (ron@ronsoft.com)
 * @version $Id: MapFile.java,v 1.3 2002/05/20 07:24:29 ron Exp $
 */
public class MapFile
{
   public static void main (String [] argv)
      throws Exception
   {
      // create a temp file and get a channel connected to it
      File tempFile = File.createTempFile ("mmaptest", null);
      RandomAccessFile file = new RandomAccessFile (tempFile, "rw");
      FileChannel channel = file.getChannel();
      ByteBuffer temp = ByteBuffer.allocate (100);

      // put something in the file, starting at location 0
      temp.put ("This is the file content".getBytes());
      temp.flip();
      channel.write (temp, 0);

      // Put something else in the file, starting at location 8192
      // 8192 is 8k, almost certainly a different memory/FS page.
      // This may cause a file hole, depending on the
      // filesystem page size.
      temp.clear();
      temp.put ("This is more file content".getBytes());
      temp.flip();
      channel.write (temp, 8192);

      // create three types of mappings to the same file
      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());

      // The buffer states before any modifications
      System.out.println ("Begin");
      showBuffers (ro, rw, cow);

      // modify the Copy-On-Write buffer
      cow.position (8);
      cow.put ("COW".getBytes());

      System.out.println ("Change to COW buffer");
      showBuffers (ro, rw, cow);

      // Modify the Read/Write buffer
      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);

      // Write to the file through the channel, hit both pages
      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);

      // modify the Copy-On-Write buffer again
      cow.position (8207);
      cow.put (" COW2 ".getBytes());

      System.out.println ("Second change to COW buffer");
      showBuffers (ro, rw, cow);

      // Modify the Read/Write buffer
      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);

      // cleanup
      channel.close();
      file.close();
      tempFile.delete();
   }

   // show the current content of the three buffers
   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 ("");
   }

   // dump buffer content, counting and skipping nulls
   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 ("'");
   }
}

标签:FileChannel,RandomAccessFile,MappedByteBuffer,JDK

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 刘志军 发表 2013-03-13 07:11:38 Oracle停止维护Java6,Rad hat接手OpenJDK6
  2. bluedavy 发表 2014-04-28 12:24:42 阿里版JVM
  3. 衔山 发表 2015-02-09 15:55:15 HTTPS和SNI
  4. root 发表 2015-04-25 05:24:03 Java系列学习(一)-JDK下载与安装
  5. 6david9 发表 2015-07-13 07:38:00 Mac OS X 上查看 JDK 位置
  6. 高彬 (noreply@blogger. 发表 2014-03-28 12:39:13 ubuntu 12.04 安装jdk和Oracle Sql Developer
  7. liuchi1993 发表 2017-01-15 23:06:27 JDK7与JDK8中HashMap的实现
  8. TiuVe2 发表 2018-07-25 02:54:58 JDK 11 已处于特性冻结状态,看看 Java 11 API 变更提案
  9. angel 发表 2013-03-16 11:57:17 Android 应用包 apk 的内部结构
  10. admin 发表 2014-04-13 05:05:19 在Ubuntu 13.04 安装JDK的步骤
  11. hailiang 发表 2014-12-09 11:18:09 linux配置tomcat
  12. icycandy 发表 2012-04-26 01:45:01 在Fedora 17上安装Sun JDK 7

发表评论