andorid jar/库源码解析之okio
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的更多相关文章
- andorid jar/库源码解析之okhttp3
目录:andorid jar/库源码解析 Okhttp3: 作用: 用于网络编程(http,https)的快速开发. 栗子: // okHttpClient定义成全局静态,或者单例,不然重复new可能 ...
- andorid jar/库源码解析之Bolts
目录:andorid jar/库源码解析 Bolts: 作用: 用于链式执行跨线程代码,且传递数据 栗子: Task.call(new Callable<Boolean>() { @Ove ...
- andorid jar/库源码解析之EventBus
目录:andorid jar/库源码解析 EventBus: 作用: 用于不同Activity,Service等之间传递消息(数据). 栗子: A页面:onCreate定义 EventBus.ge ...
- andorid jar/库源码解析之Dagger/Dagger2
目录:andorid jar/库源码解析 Dagger.Dagger2: 作用: 1.用于解耦Activity和业务逻辑 2.在使用业务的时候,不需要重复编写new代码. 3.当业务变化的时候,不需要 ...
- andorid jar/库源码解析之retrofit2
目录:andorid jar/库源码解析 Retrofit2: 作用: 通过封装okhttp库,来进行web通讯,并且使用动态代理的方式,来调用接口地址,通过回调赋值结果. 栗子: 定义一个接口,用于 ...
- andorid jar/库源码解析之Butterknife
目录:andorid jar/库源码解析 Butterknife: 作用: 用于初始化界面控件,控件方法,通过注释进行绑定控件和控件方法 栗子: public class MainActivity e ...
- andorid jar/库源码解析之zxing
目录:andorid jar/库源码解析 Zxing: 作用: 生成和识别,二维码,条形码. 栗子: 生成二维码,赋值到ImageView上 QRCodeWriter qrCodeWriter = n ...
- andorid jar/库源码解析之错误提示
目录:andorid jar/库源码解析 错误: 错误1: Error: Static interface methods are only supported starting with Andro ...
- andorid jar/库源码解析
前言 本篇作为开篇,会大体上说明,需要解读源码的,类库,或者jar. 序 原本,类库和jar的系列准备写到逆向系列课程的,但是那个东西,在写了两篇,就没有后续了,现在也不知道从哪里开始了, 只能等后期 ...
随机推荐
- Python爬虫系列(四):Beautiful Soup解析HTML之把HTML转成Python对象
在前几篇文章,我们学会了如何获取html文档内容,就是从url下载网页.今天开始,我们将讨论如何将html转成python对象,用python代码对文档进行分析. (牛小妹在学校折腾了好几天,也没把h ...
- c++动态数组的优点,创建和删除
动态数组可以有两种使用方式: 1:不能预先知道数组的大小使用动态数组 传统数组(静态数组)是需要在程序运行前,就指定大小,比如说 int i = 10; int a[i]; 这种就是不合法的. 因为函 ...
- tf.nn.sigmoid_cross_entropy_with_logits 分类
tf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None,,labels=None,logits=None,name=None) logits和la ...
- Linux 下迁移 Nexus3
Nexus3 的迁移过程还是非常简单,复制整个目录到新服务器,启动即可. 备份 在原来服务器上将 nexus3 整体目录备份即可. $ tar -zcvf nexus3.tar.gz nexus3/ ...
- The Super Powers UVA - 11752
题目大意:将范围从1~pow(2,64)-1内的super power输出.super power的定义:一个数x至少存在两种x=pow(i,k),(k!=1). 题解: 注意数据范围2的64次方-1 ...
- F - Distinct Numbers
链接:https://atcoder.jp/contests/abc143/tasks/abc143_f 题解:开两个数组,其中一个arr用来保存每个元素出现的次数,同时再开一个数组crr用来保存出现 ...
- Numpy学习-(2)
我学习numpy过程的记录 1. 切片和索引 (1) 两种切片方式示例: (2) 多维数组: import numpy as np a = np.array([[1,2,3],[3,4,5],[4,5 ...
- Golang Web入门(4):如何设计API
摘要 在之前的几篇文章中,我们从如何实现最简单的HTTP服务器,到如何对路由进行改进,到如何增加中间件.总的来讲,我们已经把Web服务器相关的内容大概梳理了一遍了.在这一篇文章中,我们将从最简单的一个 ...
- JasperReports入门教程(二):中文打印
JasperReports入门教程(二):中文打印 背景 在上一篇中我们介绍了JasperReport的基本入门,也展示了一个报表.但是我们的示例都是使用的英文,如果我们把需要打印的数据改为中文会怎么 ...
- 微信小程序弹出层动画特效
.rules_modal_cont{ height:800rpx; width:200rpx; -webkit-animation: showZeroAlert .3s; animation: sho ...