Java Memory Map File Mappedbytebuffer

Source: Internet
Author: User
Tags crc32

    • Mappedbytebuffer is a File memory mapping scheme introduced by Java NIO, which is very high in read and write performance. In NiO, the primary input stream is used, with buffered input streams, randomaccessfile and Mappedbytebuffer.

Now let's take a look at the efficiency of these four streams, and talk less directly on the code.

We use CRC32 to cycle the redundancy check. CRC32 in the Java.util.zip package.

1. Normal input stream.

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. Buffered input stream

   public static long Checksumbufferedinputstream (Path filename) throws IOException   {      try (inputstream in = new Buff Eredinputstream (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 ();      }   }

Each stream corresponds to the method of writing, and then we'll start testing.

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"); }

To ensure that each stream has no effect on the results, we can comment out the code for the other streams in the main method separately.

Finally, you can see that Mappedbytebuffer is the most efficient and consumes the least amount of time.

Java Memory Map File Mappedbytebuffer

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.