This is the 11th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.

Byte with cache []

Improve the read and write efficiency of FileInputStream. Therefore, we need to add a “bag”. Byte [] is used to act as a bag.

  • The buffer must be an integer power of 2, usually 1024:
byte[] buff = new byte[1024];
Copy the code
  • Fine tuning in while:
while((temp = file.read(buff))! = -1){
    System.out.print(temp+"");
    fos.write(buff,0,temp);
}
Copy the code

First of all, it is.read()Method more than a cache parameter, source code as shown below:

Next, it is.write()Method parameters need to be adjusted, source code as shown below:I tried it here, and both of the ways in the picture are ok, and they both work.

The teacher (the second half of the Java set 300 changed another teacher 😭) used a larger picture, and then with the naked eye to observe the copying process, the console of the red rectangle disappear time.

I thought, do I have to find a big picture to do this? No, I’m too lazy. And, of course, programmers have to have rational quantitative thinking to figure out how and how much better this approach is than the previous one.

So let’s do a simple timing comparison using the same method that Gao qi used to compare the efficiency of StringBuilder and StringBuffer.

Wrap the last code in noneBuffer and withBuffer respectively. And then remember the memory, time, and subtract.

In the code

public static void main(String[] args) {

    long num_start = Runtime.getRuntime().freeMemory(); // now memory
    long time_start = System.currentTimeMillis(); // now time
    noneBuffer();
    long num_end = Runtime.getRuntime().freeMemory();
    long time_end = System.currentTimeMillis();
    System.out.println("NoneBuffer occupies memory:" + (num_start-num_end));
    System.out.println("NoneBuffer occupied time:" + (time_end-time_start));

    System.out.println("-------------------------------\n");
    long num_start2 = Runtime.getRuntime().freeMemory(); // now memory
    long time_start2 = System.currentTimeMillis(); // now time
    withBuffer();
    long num_end2 = Runtime.getRuntime().freeMemory();
    long time_end2 = System.currentTimeMillis();
    System.out.println("WithBuffer occupies memory:" + (num_start2-num_end2));
    System.out.println("WithBuffer occupancy time:" + (time_end2-time_start2));

}
Copy the code

Running results:

I’m still printing temp in the while. Therefore, it can be clearly seen from the picture of the running result that the temp is different each time when two reads are made. The specific comparison is shown in the following table:

Comparison of object noneBuffer withBuffer
temp Each byte Pack of 1024
memory 2602560 0
Elapsed time 174 2

It can be found that withBuffer, compared with no buffer, the efficiency of the memory and time are in single digits!

Available ()

The previous method, ourbufferThe length is written dead. Now, try another way to read what this is relatively intelligently. That isfile.available(), the source code is as follows:As you can see, the return is a maximum length that can be read at one time without blocking, but this can also become blocked, such as when reading large files on a poor network.

I can’t wait to see which one is better than the last one. After slightly changing the code, you get the following result:

The core code of the withAvailble function:

file = new FileInputStream("/ Users/fang/Images/subject. PNG");
fos = new FileOutputStream("/ Users/fang/Images/title output3. PNG");
int temp = 0;
// Create a buffer to improve read and write efficiency
System.out.println("The file. The available () :" + file.available());
byte[] buff = new byte[file.available()];
file.read(buff);
fos.write(buff);

// Call from memory
fos.flush();
Copy the code

The following statement is commented out during timing. This is because I’m curious what it says:

Run with this statement:Find that this value is the same as the sum of the previous 1024! Yes, the image is the same image, so the total length of bytes is the same.

Try buffering next time