ByteBuffer

 

ByteBuffer buf = ByteBuffer.allocate(256);    

byte[] input = new byte[15];

System.in.read(input);

buf.put(input);

 

 

// 调用flip()使limit变为当前的position的值,position变为0,

 // 为接下来从ByteBuffer读取做准备

 buf.flip();

 byte[] content = new byte[buf.limit()];  

 buf.get(content);  

 System.out.print(new String(content));

 // 调用clear()使position变为0,limit变为capacity的值,

 // 为接下来写入数据到ByteBuffer中做准备

 buf.clear();

 

 

 

 

CRC32 crc = new CRC32();

  InputStream in = new FileInputStream(fileName);

  //InputStream in = new BufferedInputStream(new FileInputStream(fileName));

  //RandomAccessFile in= new RandomAccessFile(fileName,"r");

  try {  

      int c;

      while((c=in.read())!=-1){

       crc.update(c);

      }

  }catch(IOException e){

      e.printStackTrace();   

  }

  return crc.getValue();

 

 

 

   FileInputStream in = new FileInputStream(fileName);

   FileChannel channel = in.getChannel();   

   int length = (int) channel.size();   

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

   buffer.load();//调用load()ByteBuffer才不为空

   byte[] byte1=new byte[buffer.limit()];   

   buffer.get(byte1);

   crc.update(byte1);

 

 

 

FileInputStream :耗时312ms

BufferedInputStream:耗时16ms

RandomAccessFile:耗时312ms

Mapped-FileInputStream:耗时16ms或0ms

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值