java 比较InputStream,BufferedInputStream,RandomAccessFile,FileChannel的读取速度。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 比较InputStream,BufferedInputStream,RandomAccessFile,FileChannel的读取速度。相关的知识,希望对你有一定的参考价值。

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.CRC32;

/**
 * @version 1.0.0
 * @since on 2017-01-01.
 */
public class MemoryMapDemo {

  public static long checksumInputStream(Path filename) throws IOException {
    try (InputStream inputStream = Files.newInputStream(filename)) {
      CRC32 crc = new CRC32();

      int c;
      while ((c = inputStream.read()) != -1)
        crc.update(c);
      return crc.getValue();
    }
  }

  public static long checksumBufferedInputStream(Path filename) throws IOException {
    try (BufferedInputStream in = new BufferedInputStream(Files.newInputStream(filename))) {
      CRC32 crc = new CRC32();

      int c;
      while ((c = in.read()) != -1)
        crc.update(c);
      return crc.getValue();
    }
  }

  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 (int p = 0; p < length; p++) {
        file.seek(p);
        int c = file.readByte();
        crc.update(c);
      }

      return crc.getValue();
    }
  }

  public static long checksumMappedFile(Path filename) throws IOException {
    try (FileChannel channel = FileChannel.open(filename)) {
      CRC32 crc = new CRC32();

      long size = channel.size();
      MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);

      for (int p = 0; p < size; p++) {
        int c = buffer.get(p);
        crc.update(c);
      }

      return crc.getValue();
    }
  }

  public static void main(String[] args) throws IOException {
    Path filename = Paths.get(args[0]);

    System.out.println("InputStream:");
    long start = System.currentTimeMillis();
    long crcValue = checksumInputStream(filename);
    long end = System.currentTimeMillis();
    System.out.println(Long.toHexString(crcValue));
    System.out.println((end - start) + " milliseconds.");

    System.out.println("BufferedInputStream:");
    start = System.currentTimeMillis();
    crcValue = checksumBufferedInputStream(filename);
    end = System.currentTimeMillis();
    System.out.println(Long.toHexString(crcValue));
    System.out.println((end - start) + " milliseconds.");

    System.out.println("RandomAccessFile:");
    start = System.currentTimeMillis();
    crcValue = checksumRandomAccessFile(filename);
    end = System.currentTimeMillis();
    System.out.println(Long.toHexString(crcValue));
    System.out.println((end - start) + " milliseconds.");

    System.out.println("MappedFile:");
    start = System.currentTimeMillis();
    crcValue = checksumMappedFile(filename);
    end = System.currentTimeMillis();
    System.out.println(Long.toHexString(crcValue));
    System.out.println((end - start) + " milliseconds.");
  }
}

以上是关于java 比较InputStream,BufferedInputStream,RandomAccessFile,FileChannel的读取速度。的主要内容,如果未能解决你的问题,请参考以下文章

NIO中Buffer的重要属性关系解析

java 比较InputStream,BufferedInputStream,RandomAccessFile,FileChannel的读取速度。

java io流缓冲理解

Java IO使用入门

Java NIO 缓冲区

java怎么获取inputstream的大小