java计算文件的CRC校验值

package com;


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.zip.CRC32;


public class Crc
{
  public static long checksumInputStream(String filepath) throws IOException {
    InputStream inputStreamn = new FileInputStream(filepath);
    CRC32 crc = new CRC32();


    int cnt;


    while ((cnt = inputStreamn.read()) != -1) {
        crc.update(cnt);
    }
    return crc.getValue();
}


public static long checksumBufferedInputStream(String filepath) throws IOException {
    InputStream inputStream = new BufferedInputStream(new FileInputStream(filepath));


    CRC32 crc = new CRC32();


    int cnt;


    while ((cnt = inputStream.read()) != -1) {
        crc.update(cnt);
    }
    return crc.getValue();
}
public static long checksumRandomAccessFile(String filepath) throws IOException {
  RandomAccessFile randAccfile = new RandomAccessFile(filepath, "r");
  long length = randAccfile.length();
  CRC32 crc = new CRC32();


  for (long i = 0; i < length; i++) {
      randAccfile.seek(i);
      int cnt = randAccfile.readByte();
      crc.update(cnt);
  }
  return crc.getValue();
}


public static long checksumMappedFile(String filepath) throws IOException {
  FileInputStream inputStream = new FileInputStream(filepath);
  FileChannel fileChannel = inputStream.getChannel();


  int len = (int) fileChannel.size();


  MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, len);


  CRC32 crc = new CRC32();


  for (int cnt = 0; cnt < len; cnt++) {


      int i = buffer.get(cnt);
      crc.update(i);
  }
  return crc.getValue();
}




public static void main(String[] args)
{
  String filepath ="E:\\wangjiansheng\\wangjiansheng.jpg";
  try
  {
    System.out.println("Input Strea method:");


    long start_timer = System.currentTimeMillis();
    long crc = checksumInputStream(filepath);
    long end_timer = System.currentTimeMillis();
    System.out.println(Long.toHexString(crc));
    System.out.println((end_timer - start_timer) + " ms");
    System.out.println("///");
    
    
    
    System.out.println("Buffered Input Stream method:");
    start_timer = System.currentTimeMillis();
    crc = checksumBufferedInputStream(filepath);
    end_timer = System.currentTimeMillis();
    System.out.println(Long.toHexString(crc));
    System.out.println((end_timer - start_timer) + " ms");


    System.out.println("///");
    
    System.out.println("Random Access File method:");
    start_timer = System.currentTimeMillis();
    crc = checksumRandomAccessFile(filepath);
    end_timer = System.currentTimeMillis();
    System.out.println(Long.toHexString(crc));
    System.out.println((end_timer - start_timer) + " ms");


    System.out.println("///");


    System.out.println("Mapped File method:");
    start_timer = System.currentTimeMillis();
    crc = checksumMappedFile(filepath);
    end_timer = System.currentTimeMillis();
    System.out.println(Long.toHexString(crc));
    System.out.println((end_timer - start_timer) + " ms");
    
    
    
  }
  catch (IOException e)
  {
    e.printStackTrace();
  }
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值