- 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