"JAVA Core technology" new I/o__java

Source: Internet
Author: User

The following list is used to compute the 32-bit cyclic redundancy checksum (CRC32) of a file that is often used to determine whether a file is corrupted, because file corruption can lead to checksum changes. Java.util.zip Bag

Contains a CRC32 class that can use loops to compute the checksum of a sequence of bytes.

Package cn.ls;
Import java.io.*;
Import java.nio.*;
Import java.nio.channels.*;

Import java.util.zip.*; /** * This program computes the CRC checksum of a file. 
   <br> * Usage:java niotest filename * @version 1.01 2004-05-11 * @author Cay Horstmann * * public class Niotest { public static long Checksuminputstream (String filename) throws IOException {InputStream in = new FILEINPUTST
      Ream (filename);

      CRC32 CRC = New CRC32 ();
      int C;
      while ((c = in.read ())!=-1) crc.update (c);
   return Crc.getvalue (); public static long Checksumbufferedinputstream (String filename) throws IOException {InputStream in = new B
      Ufferedinputstream (filename) (new FileInputStream);

      CRC32 CRC = New CRC32 ();
      int C;
      while ((c = in.read ())!=-1) crc.update (c);
   return Crc.getvalue (); public static long Checksumrandomaccessfile (String filename) throws IOException {Randomaccessfile file = n EW RandoMaccessfile (filename, "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 (); public static long Checksummappedfile (String filename) throws IOException {FileInputStream in = new Filein
      Putstream (filename);

      FileChannel channel = In.getchannel ();
      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 {System.out.println ("Input Stream:");
      Long start = System.currenttimemillis ();
      Long Crcvalue = Checksuminputstream ("f:/x.html"); Long 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 ("f:/x.html");
      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 ("f:/x.html");
      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 ("f:/x.html");
      End = System.currenttimemillis ();
      System.out.println (long.tohexstring (Crcvalue)); System.out.println ((End-start) + "milliseconds");
 }
}

The experimental results are as follows:

Input Stream:
599af5f2
2303 milliseconds
Buffered Input Stream:
599af5f2
Milliseconds
Random Access File:
599af5f2
3038 milliseconds
Mapped File:
599af5f2
Milliseconds

Memory mappings are faster than sequential reads with Buffering.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.