zoukankan      html  css  js  c++  java
  • Java 内存映射文件MappedByteBuffer

    •  MappedByteBuffer是java nio引入的文件内存映射方案,读写性能极高。在NIO中主要用到普通的输入流,带缓冲的输入流,RandomAccessFile和MappedByteBuffer。

    现在我们来看看这四种流的效率,废话少说直接上代码。

    我们采用CRC32来循环冗余校验。CRC32在java.util.zip包中。

    1.普通的输入流。

     public static long checksumInputStream(Path filename) throws IOException
       {
          try (InputStream in = Files.newInputStream(filename))
          {
             CRC32 crc = new CRC32();
       
             int c;
             while ((c = in.read()) != -1)
                crc.update(c);
             return crc.getValue();
          }
       }
    

    2.带缓冲的输入流

       public static long checksumBufferedInputStream(Path filename) throws IOException
       {
          try (InputStream in = new BufferedInputStream(Files.newInputStream(filename)))
          {
             CRC32 crc = new CRC32();
       
             int c;
             while ((c = in.read()) != -1)
                crc.update(c);
             return crc.getValue();
          }
       }
    

    3.RandomAccessFile

     public static long checksumRandomAccessFile(Path filename) throws IOException
       {
          try (RandomAccessFile file = new RandomAccessFile(filename.toFile(), "r"))
          {
             long length = file.length();
             CRC32 crc = new CRC32();
       
             for (long p = 0; p < length; p++)
             {
                file.seek(p);
                int c = file.readByte();
                crc.update(c);
             }
             return crc.getValue();
          }
       }
    

    4.MappedByteBuffer

       public static long checksumMappedFile(Path filename) throws IOException
       {
          try (FileChannel channel = FileChannel.open(filename))
          {
             CRC32 crc = new CRC32();
             int length = (int) channel.size();
             MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
       
             for (int p = 0; p < length; p++)
             {
                int c = buffer.get(p);
                crc.update(c);
             }
             return crc.getValue();
          }
       }
    

    每个流对应的方法写好之后我们来开始测试

    public static void main(String[] args) throws IOException {
    
            String name = "txt.txt";
            Path filename = Paths.get(name);
            long start, crcValue, end;
    
            System.out.println("Input Stream:");
            start = System.currentTimeMillis();
    
            crcValue = checksumInputStream(filename);
            end = System.currentTimeMillis();
            System.out.println(Long.toHexString(crcValue));
            System.out.println((end - start) + " milliseconds");
    
            System.out.println("Buffered Input Stream:");
            start = System.currentTimeMillis();
            crcValue = checksumBufferedInputStream(filename);
            end = System.currentTimeMillis();
            System.out.println(Long.toHexString(crcValue));
            System.out.println((end - start) + " milliseconds");
    
            System.out.println("Random Access File:");
            start = System.currentTimeMillis();
            crcValue = checksumRandomAccessFile(filename);
            end = System.currentTimeMillis();
            System.out.println(Long.toHexString(crcValue));
            System.out.println((end - start) + " milliseconds");
    
            System.out.println("Mapped File:");
            start = System.currentTimeMillis();
            crcValue = checksumMappedFile(filename);
            end = System.currentTimeMillis();
            System.out.println(Long.toHexString(crcValue));
            System.out.println((end - start) + " milliseconds");
        }
    

      为了保证每种流相互之间对结果没有影响,我们可以分别把main方法中其他流的代码注释掉。

         最后可以看到MappedByteBuffer效率最高,所消耗的时间最少。

  • 相关阅读:
    Java Sping 第一章——初识 Spring
    C++设计模式——状态模式 State
    线性代数思维导图(3)——向量组
    基于Servlet实现简单系统登录
    优秀博客汇总
    整理一些开源项目
    Android UI性能优化详解
    (原创)如何在spannableString中使用自定义字体
    (原创)用讯飞语音实现人机交互的功能
    (原创)speex与wav格式音频文件的互相转换(二)
  • 原文地址:https://www.cnblogs.com/LeeHc/p/4838633.html
Copyright © 2011-2022 走看看