Processing stream of IO stream: buffer stream and conversion stream

Posted by biannucci on Mon, 17 Jan 2022 05:31:12 +0100

Processing flow

Buffer stream

The buffer stream is an enhancement of the node stream. The buffer stream has an additional buffer. The read or write data is read or written to a buffer first, and then transmitted to the data destination when the buffer is full. This can reduce the time of back and forth reading and writing and improve efficiency. The buffer stream is also called efficient stream.

The buffer stream provides an 8KB (8 times 1024) buffer.

  • BufferedInputStream
  • BufferedOutputStream
  • BufferedReader
  • BufferedWriter

When using a processing flow to close flow resources, generally speaking, in addition to the processing flow, the node flow also needs to be closed. However, when closing the processing flow, the node flow will also be closed automatically. Therefore, we only need to close the outer flow, and the inner flow will be closed automatically.

Test: compare the rates of the two, use buffer streams (BufferedInputStream and BufferedOutputStream) to copy the video, and use node streams (FileInputStream and FileOutputStream) to copy the video.

  1. Based on the node stream, the buffer stream is used to copy the video.
@Test
public void test1(){
    long start = System.currentTimeMillis();
    String scrPath = "E:\\mv\\Fast & Furious.mp4";
    String destPath = "E:\\mv\\Speed and passion copy 1.mp4";
    copyFile(scrPath,destPath);
    long end = System.currentTimeMillis();
    System.out.println("Based on node flow, buffer flow is used to copy speed and passion.mp4 Time spent:" + (end - start) + "millisecond.");
}


// File copy
public static void copyFile(String scrPath,String destPath){
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        // Create file objects, node stream objects, and buffer stream objects
        File file1 = new File(scrPath);
        File file2 = new File(destPath);
        FileInputStream fis = new FileInputStream(file1);
        FileOutputStream fos = new FileOutputStream(file2);
        bis = new BufferedInputStream(fis);
        bos = new BufferedOutputStream(fos);
        // Copy files
        byte[] buffer = new byte[1024];
        int len;
        while ((len = bis.read(buffer)) != -1){
            bos.write(buffer,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bis != null){
            // Close stream resource
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bos != null){
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

  1. Node stream is used to copy video.
@Test
public void test2(){
    long start = System.currentTimeMillis();
    String scrPath = "E:\\mv\\Fast & Furious.mp4";
    String destPath = "E:\\mv\\Speed and passion copy 2.mp4";
    copy(scrPath,destPath);
    long end = System.currentTimeMillis();
    System.out.println("Replication speed and passion using only node streams.mp4 Time spent:" + (end - start) + "millisecond.");
}

public static void copy(String scrPath,String destPath){
    FileInputStream fis = null;
    FileOutputStream fos = null;

    try {
        // Create file objects, node stream objects, and buffer stream objects
        File file1 = new File(scrPath);
        File file2 = new File(destPath);
        fis = new FileInputStream(file1);
        fos = new FileOutputStream(file2);

        // Copy files
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fis != null){
            // Close stream resource
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fos != null){
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}




The video here is not large, but it can still be seen that the efficiency of using buffer stream is much higher than that of using node stream alone. Therefore, using buffer stream to process node stream has higher efficiency in reading and writing files.

Conversion flow

Look at the suffix, which belongs to the character stream.

  • InputStreamReader: converts a byte input stream into a character input stream.
  • OutputStreamWriter: converts a character output stream to a byte output stream.
    Illustration example:

Precautions for use:

  1. It is required that the encoding table in the InputStreamReader constructor must be consistent with the encoding table of file encoding, otherwise garbled code will appear. That is, if the file is encoded with utf-8, you need to use "utf-8" in the constructor.
  2. It doesn't matter what type the encoding table in the OutputStream constructor is. It only affects the encoding format of the file. If the corresponding encoding set is used during decoding, there will be no garbled code.

Example:
① Read the file and write it out in another encoding format through the conversion stream.

public class InputStreamReaderTest {
    @Test
    public void test1(){
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        try {
            // Ajax.txt this file is encoded in UTF-8, and the default encoding format of IDEA is also set to UTF-8
            isr = new InputStreamReader(new FileInputStream(new File("Ajax.txt")));
            osw = new OutputStreamWriter(new FileOutputStream(new File("Ajax_copy.txt")),"gbk");

            // Read in Ajax Txt file and write it in gbk encoding format
            char[] cbuffer = new char[30];
            int len;
            while ((len = isr.read(cbuffer)) != -1){
                osw.write(cbuffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isr != null){
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (osw != null){
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}



The encoding format is different, so it is garbled after opening for viewing.

② Use InputStreamReader to read the Ajax modified above in gbk encoding format_ copy. txt.
The first is to use the default encoding set, UTF-8.

public void test2(){
    InputStreamReader isr = null;
    try {
        isr = new InputStreamReader(new FileInputStream("Ajax_copy.txt"));
        // Convert the underlying byte input stream into character input stream, so it is received when reading with char [].
        char[] cbuffer = new char[5];
        int len;
        while (-1 != (len = isr.read(cbuffer))){
            // Convert the char type array into String type, and then pass it into the String constructor
            String stringBuffer = new String(String.valueOf(cbuffer));
            System.out.print(string);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (isr != null){
            try {
                isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


Because there is no inconsistency between the encoding and decoding formats, it leads to garbled code when reading to the console.
Set the encoding format in the InputStreamReader constructor to "gbk", and then read the input to the console.

isr = new InputStreamReader(new FileInputStream("Ajax_copy.txt"),"gbk")


Garbled code resolution. The transmission at the bottom of the file is binary stream, so I think the reason for garbled code is basically the inconsistency between the encoding set and the decoding set.

Topics: Java