Mappedbytebuffer cache file, Randomaccessfile random access

Source: Internet
Author: User

When it comes to cache storage, processing read-write files, you have to say mappedbytebuffer.

Read a lot of articles later write their own summary.


Here first introduce the related classes and methods.

Let's first talk about the relationships between the buffer, Bytebuffer, and Mappedbytebuffer classes.


Public abstract class Buffer {    //Invariants:mark <= position <= limit <= capacity    private int mark =-1 ;    private int position = 0;    private int limit;    private int capacity;    Long address;    ......} Public abstract class Bytebuffer extends Buffer implements comparable {    //These fields is declared here rather than In Heap-x-buffer on order    to//reduce the number of virtual method invocations needed to access these    //values, Which is especially costly when coding small buffers.    final byte[] HB;   Non-null only for heap buffers    final int offset;    Boolean isreadonly;   Valid only for heap buffers    Boolean Bigendian;    Boolean nativebyteorder;    ......} Byte array final byte[] HB is the memory buffer that is referred to public abstract class Mappedbytebuffer extends Bytebuffer{private final filedescriptor FD, ...}


The public abstract class Mappedbytebuffer extends Bytebuffer direct byte buffer, whose contents are the memory-mapped area of the file.

The mapped byte buffers are created by the Filechannel.map method. This class extends the Bytebuffer class with operations specific to memory-mapped file regions.
The mapped byte buffer and the file-mapping relationship it represents remain valid until the buffer itself becomes a garbage-collected buffer.
The contents of a mapped byte buffer can be changed at any time, for example, in cases where the program or another program changes the contents of the corresponding map file area. Whether or not these changes occur (and when they occur) is not operating system-independent and therefore unspecified.
All or part of a mapped byte buffer may become inaccessible at any time, for example, if we intercept the mapped file. An inaccessible zone that attempts to access a mapped byte buffer will not change the contents of the buffer and cause an unspecified exception to be thrown at some point after access or access. It is therefore strongly recommended that appropriate precautions be taken to prevent this program or another program running concurrently from performing operations on mapped files (except for read and write file contents).

In addition, the mapped byte buffers are functionally identical to the normal direct byte buffers.


Public randomaccessfile (file file, String mode) throws FileNotFoundException
Creates a random-access file stream from which to read and write (optionally), which is specified by the file parameter. A new FileDescriptor object is created to represent the connection to this file.
R "opens in read-only mode. Any write method that invokes the result object will cause the IOException to be thrown.
"RW" opens for read and write. If the file does not already exist, try to create the file.
"RWS" opens for read and write, and for "RW", it also requires that each update to the contents or metadata of the file be synchronously written to the underlying storage device.
"RWD" is opened for read and write, and for "RW", each update to the file content is also required to be written synchronously to the underlying storage device.


Public final FileChannel Getchannel () returns the unique FileChannel object associated with this file.
The java.nio.channels.filechannel#position () position of the returned channel will always be equal to the file pointer offset of this object returned by the Getfilepointer method. Changing the file pointer offset of this object explicitly or by reading or writing bytes changes the location of the channel and vice versa. Changing the length of this file through this object will change the length seen through the file channel, and vice versa.

Public abstract Mappedbytebuffer Map (filechannel.mapmode mode,long position,long size) throws IOException maps the file area of this channel directly into memory.
Mode-maps files based on read-only, read/write, or private (copy-on-write), one of the read_only, read_write, or private defined in the Filechannel.mapmode class, respectively
Position-the location in the file where the map area starts; must be non-negative
Size-the area to be mapped; must be non-negative and not greater than integer.max_value


Here's an example of a run:

Package Com.tzx.ne;import Java.io.bufferedinputstream;import Java.io.bufferedoutputstream;import java.io.File; Import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.randomaccessfile;import Java.nio.buffer;import Java.nio.ByteBuffer;import Java.nio.mappedbytebuffer;import Java.nio.channels.filechannel;public class Mappedbytebufferdemo {public static void Main (string[] args) throws exception{/** * OUTPUT:0.001S (Read) * input:0.11s (Write) * */mappedbytebuffertest ();/** * size=1024* 8 * out:0.0s * input:0.014s * *//** * size=1024*1024*8 * output:0.01s * input:0.014s * *//** * size=80 * output:0.0s * input:0.546s * *///buffertest ();/** * time:0.585s * *///bufferedinputstreamtest (); /* * Test results with buffer size about *///1, using mappedbytebuffer:0.7spublic static void Mappedbytebuffertest () throws exception{string s Rcfile = "f:\\ebook\\ steal the day, txt"; String destfile = "F:\\ebook\\toutian.txt"; Randomaccessfile Rafi = new RandOmaccessfile (Srcfile, "R"); Randomaccessfile Rafo = new Randomaccessfile (destfile, "RW"); FileChannel FCI = Rafi.getchannel (); FileChannel FCO = Rafo.getchannel (); Long size = Fci.size (); byte b;long start = System.currenttimemillis (); Mappedbytebuffer Mbbi = Fci.map (FileChannel.MapMode.READ_ONLY, 0, size); System.out.println ("Output:" + (Double) (System.currenttimemillis ()-start)/+ + "s"); Mappedbytebuffer Mbbo = Fco.map (FileChannel.MapMode.READ_WRITE, 0, size); start = System.currenttimemillis (); for (int i = 0; i < size; i++) {b = Mbbi.get (i); Mbbo.put (i, b);} Fci.close (); Fco.close (); Rafi.close (); Rafo.close (); SYSTEM.OUT.PRINTLN ("Input:" + (Double) (System.currenttimemillis ()-start)/+ + "s");} 2, self-processing buffer (randomaccessfile): 0.13spublic static void Buffertest () throws exception{string Srcfile = "f:\\ebook\\ steal the Day . txt "; String destfile = "F:\\ebook\\toutian.txt"; Randomaccessfile Rafi = new Randomaccessfile (srcfile, "R"); Randomaccessfile Rafo = new Randomaccessfile (destfile, "RW");byte[] buf = new Byte[80];long start = System.currenttimemillis (); int c = Rafi.read (BUF); System.out.println ("Output:" + (Double) (System.currenttimemillis ()-start)/+ + "s"); start = System.currenttimemill Is (); while (C > 0) {if (c = = buf.length) {rafo.write (BUF);} else {rafo.write (buf, 0, c);} c = Rafi.read (BUF);} SYSTEM.OUT.PRINTLN ("Input:" + (Double) (System.currenttimemillis ()-start)/+ + "s"), Rafi.close (); Rafo.close ();} 3, bufferedinputstream&bufferedoutputstream:3.02spublic static void Bufferedinputstreamtest () throws Exception {String srcfile = "f:\\ebook\\. txt"; String destfile = "F:\\ebook\\toutian.txt"; FileInputStream Rafi = new FileInputStream (srcfile); FileOutputStream Rafo = new FileOutputStream (destfile); Bufferedinputstream bis = new Bufferedinputstream (Rafi, 8192); Bufferedoutputstream BOS = new Bufferedoutputstream (Rafo, 8192); Long size = Rafi.available (); Long start = System.currentt Imemillis (); for (int i = 0; i < size; i++) {byte b = (byte) bis.read(); Bos.write (b);} Rafi.close (); Rafo.close (); System.out.println ("Time:" + (Double) (System.currenttimemillis ()-start)/+ + "s");}}


Summarize:

1. Randomaccessfile is the most versatile file content access class in Java input and output stream system, he provides many methods to access the file, it can read the contents of the file, can also say to the file output data, itself without buffer read and write, and FileInputStream, FileOutputStream and so on, the performance is not acceptable when read and write directly by byte;


2, the use of Mappedbytebuffer read and write, is the performance will be greatly improved; in fact, as long as their own buffer, performance will have a very large increase, such as the following two ways in the first use of Mappedbytebuffer, the second self-buffering processing, for a few megabytes of the file , the latter efficiency is even higher than the former, can be seen from a few size size to run speed, when the size of a large time when the read speed is slower, but the overall efficiency is very high.


3, bufferedxxxx, such as buffer stream, if only using the default buffer size, performance is not necessarily optimal, to weigh the different circumstances of various factors set the size.



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.