目录:andorid jar/库源码解析

Okio:

  作用:

    说白了,就是一个IO库,基于java原生io。来进行操作,内部做了优化,简洁,高效。所以受到了一部分人的喜欢和使用

  栗子:

  读写文件。

    private void ReadFile() {
try {
InputStream in = new FileInputStream(new File("/sdcard/a.txt")); // new ByteArrayInputStream(("adasfdsaf").getBytes()); //2.缓冲源
Source source = Okio.source(in);
//3.buffer
Buffer sink = new Buffer();
source.read(sink, in.read());
//4.将数据读入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;
} //写入操作
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("我是每二条", 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();
}
}
}

  源码解读:

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

  1、定义文件,

  2、传入文件,sink内部,创建一个文件写入流  new FileOutputStream(file)的

  3、传递流对象给Okio的sink方法。返回一个Sink 的接口的匿名对象,对象中提供的方法,可以访问到传入的流,对流进行操作。(write,flush,close)

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

  1、构造一个 RealBufferedSink 对象,并传入接口Sink的接口对象。

  2、调用 RealBufferedSink 对象的,write方法,写数据。

  3、在RealBufferedSink对象内部,维护有一个 okio.Buffer 对象,写入方法,首先写入Buffer内部。然后调用 sink的write方法进行写入到流中。

  4、这里的okio.Buffer,用于高效复制的时候使用。


InputStream in = new FileInputStream(new File("/sdcard/a.txt")); 
//2.缓冲源
Source source = Okio.source(in);
//3.buffer
Buffer sink = new Buffer();
source.read(sink, in.read());
//4.将数据读入buffer
System.out.print(sink.readUtf8());

  1、传入文件流,返回一个 Source接口对象,接口方法中使用了 传入的流进行操作。(read,close)

  2、构造一个Buffer对象。用于对Source接口对象,进行操作,Buffer中包含更多方法。

  3、调用 source的read方法,先创建一个数据段(Segment),然后从流中读取数据,写入到数据段中。

  4、readUtf8,从数据段中读取数据,这里涉及到了一个判断,(根据当前数据读取位置和需要读取的数据的长度,进行判定,如果当前数据段已经读完,就需要释放下一个数据段,供下次读取。

  源码:https://github.com/square/okio

  引入:

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

andorid jar/库源码解析之okio的更多相关文章

  1. andorid jar/库源码解析之okhttp3

    目录:andorid jar/库源码解析 Okhttp3: 作用: 用于网络编程(http,https)的快速开发. 栗子: // okHttpClient定义成全局静态,或者单例,不然重复new可能 ...

  2. andorid jar/库源码解析之Bolts

    目录:andorid jar/库源码解析 Bolts: 作用: 用于链式执行跨线程代码,且传递数据 栗子: Task.call(new Callable<Boolean>() { @Ove ...

  3. andorid jar/库源码解析之EventBus

    目录:andorid jar/库源码解析 EventBus: 作用: 用于不同Activity,Service等之间传递消息(数据). 栗子: A页面:onCreate定义   EventBus.ge ...

  4. andorid jar/库源码解析之Dagger/Dagger2

    目录:andorid jar/库源码解析 Dagger.Dagger2: 作用: 1.用于解耦Activity和业务逻辑 2.在使用业务的时候,不需要重复编写new代码. 3.当业务变化的时候,不需要 ...

  5. andorid jar/库源码解析之retrofit2

    目录:andorid jar/库源码解析 Retrofit2: 作用: 通过封装okhttp库,来进行web通讯,并且使用动态代理的方式,来调用接口地址,通过回调赋值结果. 栗子: 定义一个接口,用于 ...

  6. andorid jar/库源码解析之Butterknife

    目录:andorid jar/库源码解析 Butterknife: 作用: 用于初始化界面控件,控件方法,通过注释进行绑定控件和控件方法 栗子: public class MainActivity e ...

  7. andorid jar/库源码解析之zxing

    目录:andorid jar/库源码解析 Zxing: 作用: 生成和识别,二维码,条形码. 栗子: 生成二维码,赋值到ImageView上 QRCodeWriter qrCodeWriter = n ...

  8. andorid jar/库源码解析之错误提示

    目录:andorid jar/库源码解析 错误: 错误1: Error: Static interface methods are only supported starting with Andro ...

  9. andorid jar/库源码解析

    前言 本篇作为开篇,会大体上说明,需要解读源码的,类库,或者jar. 序 原本,类库和jar的系列准备写到逆向系列课程的,但是那个东西,在写了两篇,就没有后续了,现在也不知道从哪里开始了, 只能等后期 ...

随机推荐

  1. Mac PyCharm之.gitignore 安装设置

    1. 首先安装.ignore 点击 PyCharm >>> Preferences 点击Plugins >>> 在搜索框输入.ignore >>> ...

  2. Spring Cloud 系列之 Consul 注册中心(二)

    本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Consul 注册中心(一) 本篇文章讲解 Consul 集群环境的搭建. Consul 集群 上图是一个简单的 Co ...

  3. 聊一聊深拷贝和浅拷贝(JS)

    在 JS 中数据类型分为值类型和引用类型,对于值类型,变量中存放的是具体的值,而对于引用类型,变量中存放的是地址. 对于值类型: const a = 3; let b = a; b = 4; cons ...

  4. STC15W串口通信的一些梳理

    由于控制串口1进行通信移植到串口3出现了阻力,因此很有必要对串口通信进行更进一步的梳理>>>> 一 STC15W串口对应引脚: 由此我们得到四个串口引脚分别为:串口1:P3 . ...

  5. AJ学IOS(43)之网易彩票底部自定义TabBar实现切换

    AJ分享,必须精品 效果: 代码: NYTabBarController // // NYTabBarController.m // 彩票lottery // // Created by apple ...

  6. E - Farthest Nodes in a Tree

    Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. Th ...

  7. 掷骰子 dp

    B. 掷骰子 单点时限: 2.0 sec 内存限制: 512 MB 骰子,中国传统民间娱乐用来投掷的博具,早在战国时期就已经被发明. 现在给你 n 个骰子,求 n 个骰子掷出点数之和为 a 的概率是多 ...

  8. [javascript] js实现小数的算术运算方法

    /** ** 加法函数,用来得到精确的加法结果 ** 说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显.这个函数返回较为精确的加法结果. ** 调用:accAdd(arg ...

  9. [PHP] 调用微博API 发微博OAuth2.0

    在实际测试中出现很多问题, 第一就是按照文档调用ACCESS_TOKEN的时候费老劲啦,因为是编辑线上的,有好多中文空格,没有看出来!整了好久! 第二个就是在调用api发微博的时候出现乱码!必须把发送 ...

  10. python之pymysql库连接mysql实现增、删、改、查

    安装第三方库pymysql 命令行cmd下通过pip install pymysql进行安装,安装完成后自行pip list可查看对应的版本信息 建立连接 1 #导入pymysql库 2 import ...