okio of Android jar / library source code analysis

catalog: Source code analysis of andorid jar / Library 

Okio:

Function:

To put it bluntly, it is an IO Library Based on java Native io. To operate, internal optimization, concise, efficient. So it is liked and used by some people

Chestnut:

Read and write files.

    private void ReadFile() {
        try {
            InputStream in = new FileInputStream(new File("/sdcard/a.txt")); // new ByteArrayInputStream(("adasfdsaf").getBytes());

            //2.Buffer source
            Source source = Okio.source(in);
            //3.buffer
            Buffer sink = new Buffer();
            source.read(sink, in.read());
            //4.Read data in buffer
            System.out.print(sink.readUtf8());
        }catch (Exception e){
            System.out.print("error " + e.getMessage());
            e.printStackTrace();
        }
    }

    private void WriteFile(){
        BufferedSink bSink = null;
        try {
            boolean isCreate = false;
            File file = new File("/sdcard/a.txt");
            if (!file.exists()) {
                isCreate = file.createNewFile();
            } else {
                isCreate = true;
            }

            //Write operation
            if (isCreate) {
                Sink sink = Okio.sink(file);
                bSink = Okio.buffer(sink);
                bSink.writeUtf8("1");
                bSink.writeUtf8("\n");
                bSink.writeUtf8("this is new file!");
                bSink.writeUtf8("\n");
                bSink.writeString("I am every two", Charset.forName("utf-8"));
                bSink.flush();
            }

            System.out.print("success");
        }catch (Exception e){
            e.printStackTrace();
            System.out.print("error " + e.getMessage());
        }finally {
            try {
                if (null != bSink) {
                    bSink.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

Source code interpretation:

File file = new File("/sdcard/a.txt");
Sink sink = Okio.sink(file);

1. Definition document,

2. In the sink, create a file to write to the new FileOutputStream(file)

3. Pass the stream object to Okio's Sink method. Returns an anonymous object of Sink's interface. The methods provided in the object can access the incoming flow and operate on the flow. (write,flush,close)

BufferedSink bSink = Okio.buffer(sink);
bSink.writeUtf8("testtest");

1. Construct a RealBufferedSink object and pass it into the interface object of the interface Sink.

2. Call the write method of RealBufferedSink object to write data.

3. In the RealBufferedSink object, there is an okio.Buffer object maintained. Write method, first write into the Buffer. Then the write method of sink is invoked to write to the stream.

4. The okio.Buffer here is used for efficient replication.


InputStream in = new FileInputStream(new File("/sdcard/a.txt")); 
//2.Buffer source
Source source = Okio.source(in);
//3.buffer
Buffer sink = new Buffer();
source.read(sink, in.read());
//4.Read data in buffer
System.out.print(sink.readUtf8());

1. The incoming file stream returns a Source interface object. The interface method uses the incoming stream for operation. (read,close)

2. Construct a buffer object. It is used to operate the Source interface object. Buffer contains more methods.

3. Call the read method of source, first create a Segment, then read the data from the stream and write it to the Segment.

4. readUtf8, read data from data segment, which involves a judgment (according to the current data reading position and the length of data to be read, if the current data segment has been read, the next data segment needs to be released for next reading.)

Source code: https://github.com/square/okio

Introduction:

implementation 'com.squareup.okio:okio:1.9.0'

Tags: Android Java github

Posted on Sat, 25 Apr 2020 18:42:01 -0700 by leoric1928